Cloud Storage for Firebase की मदद से, Firebase की ओर से उपलब्ध कराए गए और मैनेज किए गए Cloud Storage बकेट में फ़ाइलें आसानी से और तेज़ी से अपलोड की जा सकती हैं.
पहचान फ़ाइल बनाना
कोई फ़ाइल अपलोड करने के लिए, सबसे पहले उस फ़ाइल का 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 में फ़ाइलें अपलोड की जा सकती हैं. इसके लिए, ये दो तरीके अपनाए जा सकते हैं:
- मेमोरी में मौजूद बाइट ऐरे से अपलोड करना
- डिवाइस पर मौजूद किसी फ़ाइल के पाथ से अपलोड करना
मेमोरी में मौजूद डेटा से अपलोड करना
PutBytesAsync()
में फ़ाइल अपलोड करने का सबसे आसान तरीका PutBytesAsync()
है.Cloud Storage PutBytesAsync()
, byte[] लेता है और 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
क्लास या अपनी क्लास का इस्तेमाल किया जा सकता है. इसके लिए, PutFileAsync()
या PutBytesAsync()
तरीकों का इस्तेमाल किया जा सकता है.IProgress<UploadState>
ज़्यादा जानकारी के लिए, अपलोड मैनेज करना लेख पढ़ें.
फ़ाइल का मेटाडेटा जोड़ना
फ़ाइलें अपलोड करते समय, मेटाडेटा भी शामिल किया जा सकता है. इस मेटाडेटा में, फ़ाइल के मेटाडेटा की सामान्य प्रॉपर्टी होती हैं. जैसे, 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 से उन्हें डाउनलोड कैसे करें.