Cloud Storage for Firebase की मदद से, फ़ाइलें Cloud Storage बकेट में तुरंत और आसानी से अपलोड की जा सकती हैं. यह बकेट Firebase उपलब्ध कराता है और इसे मैनेज भी करता है.
रेफ़रंस बनाना
कोई फ़ाइल अपलोड करने के लिए, सबसे पहले एक Cloud Storage रेफ़रंस उस फ़ाइल के लिए बनाएं जिसे अपलोड करना है.
आप अपने
Cloud Storage बकेट के रूट में चाइल्ड पाथ जोड़कर रेफ़रंस बना सकते हैं या किसी मौजूदा
gs:// या https:// यूआरएल से रेफ़रंस बना सकते हैं जो Cloud Storage में किसी ऑब्जेक्ट को रेफ़र कर रहा हो.
// 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 पर दो तरीकों से फ़ाइलें अपलोड की जा सकती हैं:
- मेमोरी में मौजूद बाइट ऐरे से अपलोड करना
- डिवाइस पर मौजूद किसी फ़ाइल को दिखाने वाले फ़ाइल पाथ से अपलोड करना
मेमोरी में मौजूद डेटा से अपलोड करना
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.