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

Cloud Storage for Firebase 可讓您輕鬆快速地下載 從 Cloud Storage 由 Firebase 提供並管理的值區

可建立參照

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

您可以在 Cloud Storage 值區,或者您也可以從現有 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 下載檔案 部署 AI 技術

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

管理下載內容

除了開始下載,你還能暫停、繼續及取消下載 以及在以下裝置上執行的 Pause()Resume()Cancel() 方法 Controller,您可以視需要將其傳遞至 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 中的檔案