Cloud Storage for Firebase 讓您可以快速輕鬆地將檔案上傳到由 Firebase 提供和管理的Cloud Storage 儲存桶。
建立參考
若要上傳文件,請先建立對 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 儲存分區根的資料。您的引用必須指向子 URL。
上傳文件
獲得參考後,您可以透過兩種方式將檔案上傳到 Cloud Storage:
- 從記憶體中的位元組緩衝區上傳
- 從代表裝置上檔案的檔案路徑上傳
從記憶體中的資料上傳
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" Futurefuture = 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" Futurefuture = 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(); }
如果您想要主動管理上傳,可以向PutFile()
或PutBytes()
方法提供Controller
。這允許您使用控制器觀察正在進行的上傳操作。有關更多信息,請參閱管理上傳。
新增檔案元數據
您也可以在上傳文件時包含元資料。此元資料包含典型的檔案元資料屬性,例如name
、 size
和content_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);
管理上傳
除了開始上傳之外,您還可以使用Controller
上的Pause()
、 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下載它們。