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

'Firebase के लिए Cloud Storage' की मदद से, फ़ाइलों को तेज़ी और आसानी से Cloud Storage बकेट में अपलोड किया जा सकता है. यह बकेट, Firebase से मैनेज की जाती है और दी जाती है.

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

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

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

// 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 बकेट के रूट के रेफ़रंस के साथ, डेटा अपलोड नहीं किया जा सकता. आपका संदर्भ किसी चाइल्ड यूआरएल पर ले जाना चाहिए.

फ़ाइलें अपलोड करें

रेफ़रंस फ़ाइल मिलने के बाद, Cloud Storage में दो तरीकों से फ़ाइलें अपलोड की जा सकती हैं:

  1. मेमोरी में बाइट कलेक्शन से अपलोड करें
  2. डिवाइस पर मौजूद फ़ाइल दिखाने वाले फ़ाइल पाथ से अपलोड करें

मेमोरी में मौजूद डेटा से अपलोड करें

PutBytesAsync() तरीका, किसी फ़ाइल को Cloud Storage में अपलोड करने का सबसे आसान तरीका है. PutBytesAsync() एक बाइट[] लेता है और एक 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> इंटरफ़ेस का पालन करता है. अपने Action<T> को प्रोग्रेस टिक के लिए कॉलबैक के तौर पर देने के लिए, StorageProgress क्लास के इंस्टेंस का इस्तेमाल किया जा सकता है.

// 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 से उन्हें डाउनलोड करने का तरीका जानें.