Cloud Storage for Firebase cho phép bạn nhanh chóng và dễ dàng tải xuống tệp từ một Cloud Storage nhóm do Firebase cung cấp và quản lý.
Tạo tệp đối chiếu
Để tải một tệp xuống, trước tiên hãy tạo một tệp đối chiếu Cloud Storagecho tệp mà bạn muốn tải xuống.
Bạn có thể tạo một tệp đối chiếu bằng cách thêm các đường dẫn con vào thư mục gốc của nhóm
Cloud Storage hoặc tạo một tệp đối chiếu từ một URL
gs:// hoặc https:// hiện có tham chiếu đến một đối tượng trong Cloud Storage.
// Create a reference with an initial file path and name StorageReference path_reference = storage->GetReference("images/stars.jpg"); // Create a reference from a Cloud Storage URI StorageReference gs_reference = storage->GetReferenceFromUrl("gs://bucket/images/stars.jpg"); // Create a reference from an HTTPS URL // Note that in the URL, characters are URL escaped! StorageReference https_reference = storage->GetReferenceFromUrl("https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg");
Tải tệp xuống
Sau khi có tệp đối chiếu, bạn có thể tải tệp xuống từ Cloud Storage theo 3 cách:
- Tải xuống bộ đệm trong bộ nhớ
- Tải xuống một đường dẫn cụ thể trên thiết bị
- Tạo một URL chuỗi đại diện cho tệp trực tuyến
Tải xuống trong bộ nhớ
Tải tệp xuống bộ đệm byte trong bộ nhớ bằng phương thức GetBytes(). Đây là cách dễ nhất để nhanh chóng tải một tệp xuống, nhưng phải tải toàn bộ nội dung của tệp vào bộ nhớ. Nếu bạn yêu cầu một tệp lớn hơn bộ nhớ có sẵn của ứng dụng, thì ứng dụng sẽ gặp sự cố. Để tránh các vấn đề về bộ nhớ, hãy nhớ đặt kích thước tối đa thành một giá trị mà bạn biết ứng dụng có thể xử lý hoặc sử dụng một phương thức tải xuống khác.
// Create a reference to the file you want to download StorageReference island_ref = storage_ref.Child("images/island.jpg"); // Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes) const size_t kMaxAllowedSize = 1 * 1024 * 1024 int8_t byte_buffer[kMaxAllowedSize]; firebase::Future<size_t> future = island_ref.GetBytes(byte_buffer, kMaxAllowedSize);
Tại thời điểm yêu cầu được thực hiện, nhưng chúng ta phải đợi Future hoàn tất thì mới có thể đọc tệp. Vì trò chơi thường chạy trong một vòng lặp và ít dựa trên lệnh gọi lại hơn các ứng dụng khác, nên bạn thường sẽ thăm dò để hoàn tất.
// In the game loop that polls for the result... if (future.status() != firebase::kFutureStatusPending) { if (future.status() != firebase::kFutureStatusComplete) { LogMessage("ERROR: GetBytes() returned an invalid future."); // Handle the error... } else if (future.Error() != firebase::storage::kErrorNone) { LogMessage("ERROR: GetBytes() returned error %d: %s", future.Error(), future.error_message()); // Handle the error... } else { // byte_buffer is now populated with data for "images/island.jpg" } }
Tải xuống một tệp cục bộ
Phương thức GetFile() tải một tệp xuống trực tiếp thiết bị cục bộ. Hãy sử dụng phương thức này nếu người dùng muốn có quyền truy cập vào tệp khi không có mạng hoặc chia sẻ trong một ứng dụng khác.
// Create a reference to the file you want to download StorageReference islandRef = storage_ref.Child("images/island.jpg"]; // Create local filesystem URL const char* local_url = "file:///local/images/island.jpg"; // Download to the local filesystem Future<size_t> future = islandRef.GetFile(local_url); // Wait for Future to complete... if (future.Error() != firebase::storage::kErrorNone) { // Uh-oh, an error occurred! } else { // The file has been downloaded to local file URL "images/island.jpg" }
GetFile() nhận một đối số Controller không bắt buộc mà bạn có thể dùng để quản lý quá trình tải xuống. Hãy xem bài viết Quản lý tệp tải xuống
để biết thêm thông tin.
Tạo URL tải xuống
Nếu đã có cơ sở hạ tầng tải xuống dựa trên URL hoặc chỉ muốn
có một URL để chia sẻ, bạn có thể lấy URL tải xuống cho một tệp bằng cách gọi phương thức
GetDownloadUrl() trên một tệp đối chiếu Cloud Storage.
// Create a reference to the file you want to download StorageReference stars_ref = storage_ref.Child("images/stars.jpg"); // Fetch the download URL firebase::Future<std::string> future = stars_ref.GetDownloadUrl(); // Wait for Future to complete... if (future.Error() != firebase::storage::kErrorNone) { // Uh-oh, an error occurred! } else { // Get the download URL for 'images/stars.jpg' std::string download_url = future.Result(); }
Quản lý tệp tải xuống
Ngoài việc bắt đầu tải xuống, bạn có thể tạm dừng, tiếp tục và huỷ quá trình tải xuống bằng các phương thức Pause(), Resume() và Cancel() trên Controller. Bạn có thể tuỳ ý chuyển các phương thức này đến phương thức GetBytes() hoặc GetFile().
// Start downloading a file Controller controller; storage_ref.Child("images/mountains.jpg").GetFile(local_file, nullptr, &controller); // Pause the download controller.Pause(); // Resume the download controller.Resume(); // Cancel the download controller.Cancel();
Theo dõi tiến trình tải xuống
Bạn có thể đính kèm trình nghe vào các tệp tải xuống để theo dõi tiến trình tải xuống.
class MyListener : public firebase::storage::Listener { public: virtual void OnProgress(firebase::storage::Controller* controller) { // A progress event occurred } }; { // Start uploading a file MyEventListener my_listener; storage_ref.Child("images/mountains.jpg").GetFile(local_file, my_listener); }
Xử lý lỗi
Có một số lý do khiến lỗi có thể xảy ra khi tải xuống, bao gồm cả việc tệp không tồn tại hoặc người dùng không có quyền truy cập vào tệp mong muốn. Bạn có thể tìm thêm thông tin về lỗi trong phần Xử lý lỗi của tài liệu.
Các bước tiếp theo
Bạn cũng có thể lấy và cập nhật siêu dữ liệu cho các tệp được lưu trữ trong Cloud Storage.