इस पेज पर, Cloud Storage for Firebase SDK टूल का इस्तेमाल करके, 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() तरीका, 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 (आम तौर पर, इसे एमआईएमई टाइप कहा जाता है). 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 से उन्हें डाउनलोड करने का तरीका जानें.