Cloud Storage for Firebase 讓您可以快速輕鬆地將檔案上傳到由 Firebase 提供和管理的Cloud Storage 儲存桶。
建立參考
若要上傳文件,請先建立要上傳的文件的 Cloud Storage 引用。
您可以透過將子路徑附加到 Cloud Storage 儲存桶的根來建立引用,也可以從引用 Cloud Storage 中物件的現有gs://
或https://
URL 建立參考。
// Create a root reference StorageReference storageRef = storage.RootReference; // Create a reference to "mountains.jpg" StorageReference mountainsRef = storageRef.Child("mountains.jpg"); // Create a reference to 'images/mountains.jpg' StorageReference mountainImagesRef = storageRef.Child("images/mountains.jpg"); // While the file names are the same, the references point to different files Assert.AreEqual(mountainsRef.Name, mountainImagesRef.Name); Assert.AreNotEqual(mountainsRef.Path, mountainImagesRef.Path);
您無法上傳引用 Cloud Storage 儲存分區根的資料。您的引用必須指向子 URL。
上傳文件
獲得參考後,您可以透過兩種方式將檔案上傳到 Cloud Storage:
- 從記憶體中的位元組數組上傳
- 從代表裝置上檔案的檔案路徑上傳
從記憶體中的資料上傳
PutBytesAsync()
方法是將檔案上傳到 Cloud Storage 的最簡單方法。 PutBytesAsync()
接受一個 byte[] 並傳回一個System.Task<Firebase.Storage.StorageMetadata>
,其中將包含任務完成時有關檔案的資訊。您可以選擇使用IProgress<UploadState>
(通常是StorageProgress<UploadState>
)來監控您的上傳狀態。
// Data in memory var customBytes = new byte[] { /*...*/ }; // Create a reference to the file you want to upload StorageReference riversRef = storageRef.Child("images/rivers.jpg"); // Upload the file to the path "images/rivers.jpg" riversRef.PutBytesAsync(customBytes) .ContinueWith((Task<StorageMetadata> task) => { if (task.IsFaulted || task.IsCanceled) { Debug.Log(task.Exception.ToString()); // Uh-oh, an error occurred! } else { // Metadata contains file metadata such as size, content-type, and md5hash. StorageMetadata metadata = task.Result; string md5Hash = metadata.Md5Hash; Debug.Log("Finished uploading..."); Debug.Log("md5 hash = " + md5Hash); } });
從本機檔案上傳
您可以使用PutFileAsync()
方法上傳裝置上的本機文件,例如相機中的照片和影片。 PutFileAsync()
接受一個表示檔案路徑的string
,並傳回一個System.Task<Firebase.Storage.StorageMetadata>
,其中將包含任務完成時有關檔案的資訊。您可以選擇使用IProgress<UploadState>
(通常是StorageProgress<UploadState>
)來監控您的上傳狀態。
// File located on disk string localFile = "..."; // Create a reference to the file you want to upload StorageReference riversRef = storageRef.Child("images/rivers.jpg"); // Upload the file to the path "images/rivers.jpg" riversRef.PutFileAsync(localFile) .ContinueWith((Task<StorageMetadata> task) => { if (task.IsFaulted || task.IsCanceled) { Debug.Log(task.Exception.ToString()); // Uh-oh, an error occurred! } else { // Metadata contains file metadata such as size, content-type, and download URL. StorageMetadata metadata = task.Result; string md5Hash = metadata.Md5Hash; Debug.Log("Finished uploading..."); Debug.Log("md5 hash = " + md5Hash); } });
如果要主動監視上傳,可以使用StorageProgress
類別或您自己的實作IProgress<UploadState>
類,以及PutFileAsync()
或PutBytesAsync()
方法。有關更多信息,請參閱管理上傳。
新增檔案元數據
您也可以在上傳文件時包含元資料。此元資料包含典型的檔案元資料屬性,例如Name
、 Size
和ContentType
(通常稱為 MIME 類型)。 PutFileAsync()
方法會自動從檔案副檔名推斷內容類型,但您可以透過在元資料中指定ContentType
來覆寫自動偵測到的類型。如果您不提供ContentType
且 Cloud Storage 無法從檔案副檔名推斷出預設值,則 Cloud Storage 將使用application/octet-stream
。有關文件元資料的更多信息,請參閱使用文件元資料部分。
// Create storage reference StorageReference mountainsRef = storageRef.Child("images/mountains.jpg"); byte[] customBytes = new byte[] { /*...*/ }; string localFile = "..."; // Create file metadata including the content type var newMetadata = new MetadataChange(); newMetadata.ContentType = "image/jpeg"; // Upload data and metadata mountainsRef.PutBytesAsync(customBytes, newMetadata, null, CancellationToken.None); // .ContinueWithOnMainThread(... // Upload file and metadata mountainsRef.PutFileAsync(localFile, newMetadata, null, CancellationToken.None); // .ContinueWithOnMainThread(...
監控上傳進度
您可以將偵聽器附加到上傳以監視上傳進度。偵聽器遵循標準System.IProgress<T>
介面。您可以使用StorageProgress
類別的實例來提供您自己的Action<T>
作為進度刻度的回呼。
// Start uploading a file var task = storageRef.Child("images/mountains.jpg") .PutFileAsync(localFile, null, new StorageProgress<UploadState>(state => { // called periodically during the upload Debug.Log(String.Format("Progress: {0} of {1} bytes transferred.", state.BytesTransferred, state.TotalByteCount)); }), CancellationToken.None, null); task.ContinueWithOnMainThread(resultTask => { if (!resultTask.IsFaulted && !resultTask.IsCanceled) { Debug.Log("Upload finished."); } });
錯誤處理
上傳錯誤的原因有很多,包括本機檔案不存在,或是使用者沒有上傳所需檔案的權限。您可以在文件的處理錯誤部分找到有關錯誤的更多資訊。
下一步
現在您已經上傳了文件,讓我們了解如何從 Cloud Storage下載它們。