फायरबेस के लिए क्लाउड स्टोरेज आपको फायरबेस द्वारा प्रदान और प्रबंधित क्लाउड स्टोरेज बकेट में फ़ाइलों को जल्दी और आसानी से अपलोड करने की अनुमति देता है।
एक संदर्भ बनाएँ
फ़ाइल अपलोड करने के लिए, पहले उस फ़ाइल का क्लाउड स्टोरेज संदर्भ बनाएं जिसे आप अपलोड करना चाहते हैं।
आप अपने क्लाउड स्टोरेज बकेट के रूट में चाइल्ड पाथ जोड़कर एक संदर्भ बना सकते हैं, या आप क्लाउड स्टोरेज में किसी ऑब्जेक्ट को संदर्भित करने वाले मौजूदा 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);
आप अपने क्लाउड स्टोरेज बकेट के रूट के संदर्भ में डेटा अपलोड नहीं कर सकते। आपका संदर्भ किसी चाइल्ड यूआरएल की ओर इंगित करना चाहिए।
फाइलें अपलोड करें
एक बार आपके पास संदर्भ हो जाने पर, आप दो तरीकों से क्लाउड स्टोरेज पर फ़ाइलें अपलोड कर सकते हैं:
- मेमोरी में बाइट सरणी से अपलोड करें
- डिवाइस पर फ़ाइल का प्रतिनिधित्व करने वाले फ़ाइल पथ से अपलोड करें
मेमोरी में डेटा से अपलोड करें
किसी फ़ाइल को क्लाउड स्टोरेज पर अपलोड करने का सबसे सरल तरीका PutBytesAsync()
विधि है। 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
प्रदान नहीं करते हैं और क्लाउड स्टोरेज फ़ाइल एक्सटेंशन से डिफ़ॉल्ट का अनुमान नहीं लगा सकता है, तो क्लाउड स्टोरेज 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."); } });
त्रुटि प्रबंधन
अपलोड करते समय त्रुटियाँ होने के कई कारण हो सकते हैं, जिनमें स्थानीय फ़ाइल का मौजूदा न होना, या उपयोगकर्ता के पास वांछित फ़ाइल अपलोड करने की अनुमति न होना शामिल है। आप डॉक्स के हैंडल एरर्स अनुभाग में त्रुटियों के बारे में अधिक जानकारी पा सकते हैं।
अगले कदम
अब जब आपने फ़ाइलें अपलोड कर दी हैं, तो आइए जानें कि उन्हें क्लाउड स्टोरेज से कैसे डाउनलोड किया जाए ।