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