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

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

可建立參照

如要下載檔案,請先為要下載的檔案建立 Cloud Storage 參考資料

您可以將子項路徑附加至 Cloud Storage 值區的根目錄,藉此建立參照,或是透過參照 Cloud Storage 中物件的現有 gs://https:// 網址建立參照。

// 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 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 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 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 的檔案取得及更新中繼資料