Tải xuống tệp bằng Cloud Storage cho C++

Cloud Storage cho Firebase cho phép bạn tải xuống tệp nhanh chóng và dễ dàng từ nhóm Cloud Storage do Firebase cung cấp và quản lý.

Tạo một tài liệu tham khảo

Để tải xuống tệp, trước tiên hãy tạo tham chiếu Cloud Storage cho tệp bạn muốn tải xuống.

Bạn có thể tạo tham 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 bạn có thể tạo tham chiếu 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 tin

Sau khi đã có tài liệu tham khảo, bạn có thể tải file từ Cloud Storage theo ba cách:

  1. Tải xuống bộ đệm trong bộ nhớ
  2. Tải xuống một đường dẫn cụ thể trên thiết bị
  3. Tạo một chuỗi URL đạ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 xuống một tệp nhưng nó 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 tệp lớn hơn bộ nhớ khả dụng của ứng dụng, ứng dụng của bạn sẽ gặp sự cố. Để bảo vệ khỏi các vấn đề về bộ nhớ, hãy đảm bảo đặt kích thước tối đa thành kích thước mà bạn biết ứng dụng của mình có thể xử lý hoặc sử dụng phương pháp 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 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 tôi phải đợi Tương lai hoàn thành trước khi chúng tôi có thể đọc tệp. Vì trò chơi thường chạy theo vòng lặp và ít được điều khiển 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 thành.

// 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 tin cục bộ

Phương thức GetFile() tải tệp trực tiếp xuống thiết bị cục bộ. Sử dụng tùy chọn này nếu người dùng của bạn muốn có quyền truy cập vào tệp khi ngoại tuyến 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 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 tùy chọn mà bạn có thể sử dụng để quản lý quá trình tải xuống của mình. Xem Quản lý tải xuống để biết thêm thông tin.

Tạo URL tải xuống

Nếu bạn đã có cơ sở hạ tầng tải xuống dựa trên URL hoặc chỉ muốn chia sẻ URL, bạn có thể lấy URL tải xuống cho tệp bằng cách gọi phương thức GetDownloadUrl() trên tham 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 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ả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à hủy tải xuống bằng cách sử dụng các phương thức Pause() , Resume()Cancel() trên Controller mà bạn có thể tùy ý chuyển sang các 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 bản 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ả 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.

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.