簡體   English   中英

在 Unity 中下載整個 firebase 存儲文件夾的內容

[英]Downloading an entire firebase storage folder's content in Unity

您好,我正在嘗試將整個文件夾的內容從 firebase 下載到 android 設備中。

firebase 層次結構如下所示: 在此處輸入圖像描述

到目前為止,我只能使用以下代碼下載單個文件:

// ------------------------- FILE DOWNLOADING ------------------------------------- //
Debug.Log("Download Attempt...");

if (Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite))
{
    Debug.Log("STEP1...");
    //Firestore Reference
    storage = FirebaseStorage.DefaultInstance;
    storageReference = storage.GetReferenceFromUrl("gs://houdini-ac884.appspot.com");
    StorageReference riversRef = storageReference.Child("uploads/3895d968-65bf-4e2d-a964-763e22742fdf.meta");
    //StorageReference 
    //pathReference = storage.GetReference("uploads/3895d968-65bf-4e2d-a964-763e22742fdf.meta");
    // Create local filesystem URL
    Debug.Log("STEP2...");
    var Directory_path = ("SparseSpatialMap/" + "3895d968-65bf-4e2d-a964-763e22742fdf.meta");
    var path = (Application.persistentDataPath + "/" + Directory_path);   
    Debug.Log("STEP3...");
    // Download to the local filesystem
    //pathReference.GetFileAsync(path).ContinueWithOnMainThread(task => 
    //{
    riversRef.GetFileAsync(path).ContinueWithOnMainThread(task => 
    {
        if (!task.IsFaulted && !task.IsCanceled) 
        {
            Debug.Log("Finished downloading...");
            easyar.GUIPopup.EnqueueMessage("Download Completed", 5);
        }
        else
        {
            Debug.Log("DOWNLOAD FAILURE !!!!!!!!!!!!!");
            Debug.Log(task.Exception.ToString());
            easyar.GUIPopup.EnqueueMessage("FAIL EXCEPTION", 5);
        }

        Debug.Log("STEP4...");
    });
} 
else 
{
    Debug.Log("No Permissions");
    easyar.GUIPopup.EnqueueMessage("FAIL, No permissions", 5);
    Permission.RequestUserPermission(Permission.ExternalStorageWrite);
}

Debug.Log("End of Download Attempt...");
// ------------------------- FILE DOWNLOADING END ------------------------------------- //

據我了解,沒有 firebase function 可以下載文件夾中的所有文件,我將不得不使用其他東西。 任何幫助將不勝感激謝謝

有一個 REST API 來獲取文件夾中所有文件的元數據信息。

https://firebasestorage.googleapis.com/v0/b/YOUR_PROJECT_ID/o?preifx=path/to/folder

傳遞給上面 API 的參數是文件夾的路徑。

首先,您需要獲取文件夾中存在的文件列表。 以下方法獲取文件列表:

async Task<List<FileMetadata>> GetFilesInFolder(string path)
{
    const string baseUrl = "https://firebasestorage.googleapis.com/v0/b/";
    const string projectId = "PROJECT_ID";

    // Build the REST API URL
    string url = $"{baseUrl}{projectId}/o?prefix={path}";

    // Send a GET request to the URL
    using (HttpClient client = new HttpClient())
    using (HttpResponseMessage response = await client.GetAsync(url))
    using (HttpContent content = response.Content)
    {
        // Read the response body
        string responseText = await content.ReadAsStringAsync();

        // Deserialize the JSON response
        ListFilesResponse responseData = JsonConvert.DeserializeObject<ListFilesResponse>(responseText);

        // Return the list of files
        return responseData.Files;
    }
}

獲得文件的元數據列表后,開始下載每個文件。

List<FileMetadata> files = await GetFilesInFolder(folderPath);

// Download each file
foreach (FileMetadata file in files)
{
    // Get the file's download URL
    string downloadUrl = file.DownloadUrl;

    // Download the file using the URL
    using (HttpClient client = new HttpClient())
    using (HttpResponseMessage response = await client.GetAsync(downloadUrl))
    using (HttpContent content = response.Content)
    {
       
        byte[] fileData = await content.ReadAsByteArrayAsync();

        // Save the file to the device
    
    }
}

還要從 Firebase 的端點添加響應對象

class ListFilesResponse
{
    public List<FileMetadata> Files { get; set; }
}

// Class that represents metadata for a file in Firebase Storage
class FileMetadata
{
    public string Name { get; set; }
    public string DownloadUrl { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM