使用 C++ 適用的 Cloud Storage 上傳檔案

Cloud Storage for Firebase 可讓您輕鬆快速地將檔案上傳至 提供 Cloud Storage 值區 並由 Firebase 管理

可建立參照

如要上傳檔案,請先 建立 Cloud Storage 參考資料 拖曳到 Cloud Storage 中要上傳檔案的位置。

您可以在 Cloud Storage 值區:

// Create a root reference
StorageReference storage_ref = storage->GetReference();

// Create a reference to "mountains.jpg"
StorageReference mountains_ref = storage_ref.Child("mountains.jpg");

// Create a reference to 'images/mountains.jpg'
StorageReference mountain_images_ref = storage_ref.Child("images/mountains.jpg");

// While the file names are the same, the references point to different files
mountains_ref.name() == mountain_images_ref.name();           // true
mountains_ref.full_path() == mountain_images_ref.full_path(); // false

您無法上傳任何參照 Cloud Storage 值區您的參照必須指向子網址。

上傳檔案

取得參考資訊後,您可以將檔案上傳至 Cloud Storage 有兩種做法:

  1. 從記憶體中的位元組緩衝區上傳
  2. 從代表裝置上檔案的檔案路徑上傳

從記憶體中的資料上傳

如要將檔案上傳至PutData() 一起來探索這個決策樹 協助您找到合適的 Cloud Storage 儲存空間級別PutData() 會擷取位元組緩衝區,並傳回 含有檔案相關資訊的 Future<Metadata> Future 完成後。您可以使用 Controller 管理上傳內容和 來監控備份狀態

// Data in memory
const size_t kByteBufferSize = ...
uint8_t byte_buffer[kByteBufferSize] = { ... };

// Create a reference to the file you want to upload
StorageReference rivers_ref = storage_ref.Child("images/rivers.jpg");

// Upload the file to the path "images/rivers.jpg"
Future future = rivers_ref.PutBytes(byte_buffer, kByteBufferSize);

此時提出要求,但我們必須等待 Future 完成上傳由於遊戲通常會以迴圈方式執行,且 相較於其他應用程式,您的應用程式促成的回呼通常減少,一般會輪詢 完成。

if (future.status() != firebase::kFutureStatusPending) {
  if (future.status() != firebase::kFutureStatusComplete) {
    LogMessage("ERROR: GetData() returned an invalid future.");
    // Handle the error...
  } else if (future.Error() != firebase::storage::kErrorNone) {
    LogMessage("ERROR: GetData() returned error %d: %s", future.Error(),
               future.error_message());
    // Handle the error...
    }
  } else {
    // Metadata contains file metadata such as size, content-type, and download URL.
    Metadata* metadata = future.Result();
    std::string download_url = metadata->download_url();
  }
}

從本機檔案上傳

您可以上傳裝置上的本機檔案,例如相片和影片的本機檔案 相機使用 PutFile() 方法。PutFile()可以使用 std::string 代表檔案路徑並傳回 Future<Metadata>,其中會包含 Future 完成後的檔案相關資訊。您可以使用 Controller可管理上傳內容並監控上傳狀態。

// File located on disk
std::string local_file = ...

// Create a reference to the file you want to upload
StorageReference rivers_ref = storage_ref.Child("images/rivers.jpg");

// Upload the file to the path "images/rivers.jpg"
Future future = rivers_ref.PutFile(localFile);

// Wait for Future to complete...

if (future.Error() != firebase::storage::kErrorNone) {
  // Uh-oh, an error occurred!
} else {
  // Metadata contains file metadata such as size, content-type, and download URL.
  Metadata* metadata = future.Result();
  std::string download_url = metadata->download_url();
}

如要主動管理上傳內容,可以將 Controller 提供給 PutFile()PutBytes() 方法。這樣您即可使用控制器 並觀察正在進行的上傳作業。想瞭解如何管理上傳項目,請參閱這篇文章 瞭解詳情

新增檔案中繼資料

您也可以在上傳檔案時加入中繼資料。這項中繼資料含有 一般檔案中繼資料屬性,例如 namesizecontent_type (通常稱為 MIME 類型)。PutFile() 方法會自動推論 作為檔案名稱副檔名的內容類型,但您可以覆寫 透過在中繼資料中指定 content_type 來自動偵測類型。如果您不 提供 content_type,且 Cloud Storage 無法從 Cloud Storage 使用的是 application/octet-stream,詳情請見 「使用檔案中繼資料」 一節,進一步瞭解檔案中繼資料。

// Create storage reference
StorageReference mountains_ref = storage_ref.Child("images/mountains.jpg");

// Create file metadata including the content type
StorageMetadata metadata;
metadata.set_content_type("image/jpeg");

// Upload data and metadata
mountains_ref.PutBytes(data, metadata);

// Upload file and metadata
mountains_ref.PutFile(local_file, metadata);

管理上傳項目

除了開始上傳之外,您還可以使用 ControllerPause()Resume()Cancel() 方法,您可以 視需要傳遞至 PutBytes()PutFile() 方法。

// Start uploading a file
firebase::storage::Controller controller;
storage_ref.Child("images/mountains.jpg").PutFile(local_file, nullptr, &controller);

// Pause the upload
controller.Pause();

// Resume the upload
controller.Resume();

// Cancel the upload
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").PutFile(local_file, my_listener);
}

處理錯誤

導致上傳發生錯誤的原因有很多種,包括 本機檔案不存在,或使用者無權上傳 指定要上傳的檔案如要進一步瞭解錯誤,請參閱 處理錯誤 一節。

後續步驟

上傳檔案後,我們接著來學習如何 下載 從 Cloud Storage 執行