使用適用於 C++ 的 Cloud Storage 下載文件

Cloud Storage for Firebase 讓您可以快速輕鬆地從 Firebase 提供和管理的Cloud Storage 儲存桶下載檔案。

建立參考

若要下載文件,請先建立要下載的文件的 Cloud Storage 引用

您可以透過將子路徑附加到 Cloud Storage 儲存桶的根來建立引用,也可以從引用 Cloud Storage 中物件的現有gs://https:// URL 建立參考。

// 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. 產生代表線上文件的字串 URL

下載到記憶體中

使用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參數,您可以使用它來管理下載。有關詳細信息,請參閱管理下載

產生下載網址

如果您已經擁有基於 URL 的下載基礎架構,或者只是想要共用 URL,則可以透過對 Cloud Storage 引用呼叫GetDownloadUrl()方法來取得檔案的下載 URL。

// 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 中的檔案的元資料