使用 C++ 適用的 Cloud Storage 下載檔案

Cloud Storage for Firebase 可讓您從 Firebase 提供及管理的 Cloud Storage bucket 輕鬆快速下載檔案。

可建立參照

如要下載檔案,請先建立 Cloud Storage 參照,指向要下載的檔案。

您可以將子路徑附加至 Cloud Storage bucket 的根目錄,藉此建立參照,也可以從現有的 gs://https:// 網址建立參照,該網址參照 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");

下載檔案

取得參照後,您可以透過下列三種方式下載檔案:Cloud Storage

  1. 下載至記憶體中的緩衝區
  2. 下載到裝置上的特定路徑
  3. 產生代表線上檔案的字串網址

下載到記憶體

使用 GetBytes() 方法將檔案下載至記憶體中的位元組緩衝區。這是快速下載檔案最簡單的方法,但必須將檔案的完整內容載入記憶體。如果要求的檔案大於應用程式的可用記憶體,應用程式就會當機。為防範記憶體問題,請務必將大小上限設為應用程式可處理的值,或使用其他下載方法。

// 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);

此時已提出要求,但我們必須等待 Future 完成,才能讀取檔案。由於遊戲通常會以迴圈形式執行,且不像其他應用程式那樣以回呼為主,因此您通常會輪詢完成狀態。

// 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"
  }
}

下載至本機檔案

GetFile() 方法會將檔案直接下載到本機裝置。如果使用者想在離線時存取檔案,或透過其他應用程式分享檔案,請使用這項功能。

// 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() 會採用選用的 Controller 引數,您可以用這個引數管理下載作業。詳情請參閱「管理下載內容」。

產生下載網址

如果您已有以網址為基礎的下載基礎架構,或只是想共用網址,可以對 Cloud Storage 參照呼叫 GetDownloadUrl() 方法,取得檔案的下載網址。

// 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();
}

管理下載內容

除了啟動下載作業,您也可以使用 Controller 上的 Pause()Resume()Cancel() 方法,暫停、繼續及取消下載作業,您也可以選擇將這些方法傳遞至 GetBytes()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();

監控下載進度

您可以將監聽器附加至下載作業,藉此監控下載進度。

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);
}

處理錯誤

下載時發生錯誤的原因有很多,包括檔案不存在,或是使用者沒有存取所需檔案的權限。如要進一步瞭解錯誤,請參閱文件中的「處理錯誤」一節。

後續步驟

您也可以取得及更新儲存在 Cloud Storage 中的檔案中繼資料。