Cloud Storage के साथ Unity के लिए फ़ाइलें डाउनलोड करना

Firebase के लिए Cloud Storage आपको जल्दी और आसानी से डाउनलोड करने देता है Cloud Storage में मौजूद फ़ाइलें Firebase उपलब्ध कराने और मैनेज करने के लिए बकेट.

रेफ़रंस बनाना

फ़ाइल डाउनलोड करने के लिए, सबसे पहले Cloud Storage के लिए रेफ़रंस बनाना उस फ़ाइल को डाउनलोड करें जिसे आपको डाउनलोड करना है.

आप अपने चैनल के रूट में चाइल्ड पाथ को जोड़कर रेफ़रंस बना सकते हैं Cloud Storage बकेट या किसी मौजूदा रिपोर्ट से पहचान फ़ाइल बनाई जा सकती है Cloud Storage में किसी ऑब्जेक्ट का रेफ़रंस देने वाला gs:// या https:// यूआरएल.

// 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 से फ़ाइलें डाउनलोड की जा सकती हैं चार तरीकों से:

  1. किसी यूआरएल से डाउनलोड करें
  2. बाइट अरे में डाउनलोड करें
  3. स्ट्रीम के साथ डाउनलोड करें
  4. डिवाइस पर मौजूद फ़ाइल में डाउनलोड करें

फ़ाइलें वापस पाने के लिए इस्तेमाल किया जाने वाला तरीका, इस बात पर निर्भर करेगा कि आपकी फ़ाइलों को वापस कैसे लाया जाए इस्तेमाल करने में मदद करता है.

किसी यूआरएल से डाउनलोड करें

अगर आपको 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> का पालन करता है इंटरफ़ेस पर कॉपी करने की सुविधा मिलती है. 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 में सेव की गई फ़ाइलों के लिए.