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 pathReference = storage.GetReference("images/stars.jpg"); // Create a reference from a Google Cloud Storage URI StorageReference gsReference = storage.GetReferenceFromUrl("gs://bucket/images/stars.jpg"); // Create a reference from an HTTPS URL // Note that in the URL, characters are URL escaped! StorageReference httpsReference = storage.GetReferenceFromUrl("https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg");
下載檔案
有了參考資源後,你就能從「Cloud Storage」下載檔案 分為四種:
- 從網址下載
- 下載至位元組陣列
- 透過串流下載
- 下載至本機檔案
擷取檔案的方法取決於你希望的方式 遊戲中消耗的資料。
從網址下載
如要將網址搭配 Unity 的 WWW
或 UnityWebRequest
,您可以
呼叫 GetDownloadUrlAsync()
以取得檔案的下載網址。
// Fetch the download URL reference.GetDownloadUrlAsync().ContinueWithOnMainThread(task => { if (!task.IsFaulted && !task.IsCanceled) { Debug.Log("Download URL: " + task.Result); // ... now download the file via WWW or UnityWebRequest. } });
下載至位元組陣列
您可以使用 GetBytesAsync()
方法,將檔案下載到記憶體中的位元組緩衝區。
這個方法會將整個檔案的內容載入記憶體。
如果您要求的檔案大於應用程式可用記憶體,應用程式就會當機。
為了避免記憶體問題,請務必將大小上限設為
或其他下載方式。
// Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes) const long maxAllowedSize = 1 * 1024 * 1024; reference.GetBytesAsync(maxAllowedSize).ContinueWithOnMainThread(task => { if (task.IsFaulted || task.IsCanceled) { Debug.LogException(task.Exception); // Uh-oh, an error occurred! } else { byte[] fileContents = task.Result; Debug.Log("Finished downloading!"); } });
透過訊息串下載
使用串流下載檔案後,您就可以在資料載入時處理資料。
如此一來,您便能擁有最大程度的下載彈性。致電
GetStreamAsync()
,並傳遞您自己的串流處理器做為第一個引數。
這個委派代表會在串流的背景執行緒上呼叫,
能讓您執行需耗費大量延遲時間的作業或運算
以及將內容儲存至磁碟
// Download via a Stream reference.GetStreamAsync(stream => { // Do something with the stream here. // // This code runs on a background thread which reduces the impact // to your framerate. // // If you want to do something on the main thread, you can do that in the // progress eventhandler (second argument) or ContinueWith to execute it // at task completion. }, null, CancellationToken.None);
GetStreamAsync()
會在串流處理器後方使用選用引數
可讓您取消作業或接收進度通知。
下載至本機檔案
GetFileAsync()
方法會將檔案直接下載到本機裝置。適用情況
使用者希望在離線時存取檔案,或是在
不同的應用程式
// Create local filesystem URL string localUrl = "file:///local/images/island.jpg"; // Download to the local filesystem reference.GetFileAsync(localUrl).ContinueWithOnMainThread(task => { if (!task.IsFaulted && !task.IsCanceled) { Debug.Log("File downloaded."); } });
您可以將事件監聽器附加到下載內容,以便監控
下載。事件監聽器遵循標準 System.IProgress<T>
存取 API您可以使用 StorageProgress
類別的執行個體來提供
您自己的 Action<T>
做為進度滴答的回呼。
// Create local filesystem URL string localUrl = "file:///local/images/island.jpg"; // Start downloading a file Task task = reference.GetFileAsync(localFile, new StorageProgress<DownloadState>(state => { // called periodically during the download Debug.Log(String.Format( "Progress: {0} of {1} bytes transferred.", state.BytesTransferred, state.TotalByteCount )); }), CancellationToken.None); task.ContinueWithOnMainThread(resultTask => { if (!resultTask.IsFaulted && !resultTask.IsCanceled) { Debug.Log("Download finished."); } });
處理錯誤
下載時可能會發生錯誤的原因有很多,包括 檔案不存在,或使用者沒有存取所需檔案的權限。 如要進一步瞭解錯誤,請前往 處理錯誤 一節。
後續步驟
您也可以取得及更新中繼資料 針對儲存在 Cloud Storage 中的檔案