'Firebase के लिए Cloud Storage' की मदद से, फ़ाइलों को तेज़ी और आसानी से Cloud Storage बकेट में अपलोड किया जा सकता है. यह बकेट, Firebase से मैनेज की जाती है और दी जाती है.
फ़ाइलें अपलोड करें
Cloud Storage में कोई फ़ाइल अपलोड करने के लिए, आपको सबसे पहले फ़ाइल के पूरे पाथ का रेफ़रंस बनाना होगा. इसमें फ़ाइल का नाम भी शामिल होना चाहिए.
Web
import { getStorage, ref } from "firebase/storage"; // Create a root reference const storage = getStorage(); // Create a reference to 'mountains.jpg' const mountainsRef = ref(storage, 'mountains.jpg'); // Create a reference to 'images/mountains.jpg' const mountainImagesRef = ref(storage, 'images/mountains.jpg'); // While the file names are the same, the references point to different files mountainsRef.name === mountainImagesRef.name; // true mountainsRef.fullPath === mountainImagesRef.fullPath; // false
Web
// Create a root reference var storageRef = firebase.storage().ref(); // Create a reference to 'mountains.jpg' var mountainsRef = storageRef.child('mountains.jpg'); // Create a reference to 'images/mountains.jpg' var mountainImagesRef = storageRef.child('images/mountains.jpg'); // While the file names are the same, the references point to different files mountainsRef.name === mountainImagesRef.name; // true mountainsRef.fullPath === mountainImagesRef.fullPath; // false
Blob
या File
से अपलोड करें
सही पहचान फ़ाइल बनाने के बाद, uploadBytes()
तरीके को कॉल करें. uploadBytes()
, JavaScript फ़ाइल और Blob एपीआई के ज़रिए फ़ाइलें ले जाता है और उन्हें Cloud Storage में अपलोड करता है.
Web
import { getStorage, ref, uploadBytes } from "firebase/storage"; const storage = getStorage(); const storageRef = ref(storage, 'some-child'); // 'file' comes from the Blob or File API uploadBytes(storageRef, file).then((snapshot) => { console.log('Uploaded a blob or file!'); });
Web
// 'file' comes from the Blob or File API ref.put(file).then((snapshot) => { console.log('Uploaded a blob or file!'); });
बाइट कलेक्शन से अपलोड करें
File
और Blob
टाइप के अलावा, uploadBytes()
Cloud Storage पर Uint8Array
भी अपलोड कर सकता है.
Web
import { getStorage, ref, uploadBytes } from "firebase/storage"; const storage = getStorage(); const storageRef = ref(storage, 'some-child'); const bytes = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21]); uploadBytes(storageRef, bytes).then((snapshot) => { console.log('Uploaded an array!'); });
Web
var bytes = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21]); ref.put(bytes).then((snapshot) => { console.log('Uploaded an array!'); });
किसी स्ट्रिंग से अपलोड करें
अगर Blob
, File
या Uint8Array
उपलब्ध नहीं है, तो uploadString()
तरीके का इस्तेमाल करके Cloud Storage में, रॉ, base64
, base64url
या data_url
कोड में बदली गई स्ट्रिंग अपलोड करें.
Web
import { getStorage, ref, uploadString } from "firebase/storage"; const storage = getStorage(); const storageRef = ref(storage, 'some-child'); // Raw string is the default if no format is provided const message = 'This is my message.'; uploadString(storageRef, message).then((snapshot) => { console.log('Uploaded a raw string!'); }); // Base64 formatted string const message2 = '5b6p5Y+344GX44G+44GX44Gf77yB44GK44KB44Gn44Go44GG77yB'; uploadString(storageRef, message2, 'base64').then((snapshot) => { console.log('Uploaded a base64 string!'); }); // Base64url formatted string const message3 = '5b6p5Y-344GX44G-44GX44Gf77yB44GK44KB44Gn44Go44GG77yB'; uploadString(storageRef, message3, 'base64url').then((snapshot) => { console.log('Uploaded a base64url string!'); }); // Data URL string const message4 = 'data:text/plain;base64,5b6p5Y+344GX44G+44GX44Gf77yB44GK44KB44Gn44Go44GG77yB'; uploadString(storageRef, message4, 'data_url').then((snapshot) => { console.log('Uploaded a data_url string!'); });
Web
// Raw string is the default if no format is provided var message = 'This is my message.'; ref.putString(message).then((snapshot) => { console.log('Uploaded a raw string!'); }); // Base64 formatted string var message = '5b6p5Y+344GX44G+44GX44Gf77yB44GK44KB44Gn44Go44GG77yB'; ref.putString(message, 'base64').then((snapshot) => { console.log('Uploaded a base64 string!'); }); // Base64url formatted string var message = '5b6p5Y-344GX44G-44GX44Gf77yB44GK44KB44Gn44Go44GG77yB'; ref.putString(message, 'base64url').then((snapshot) => { console.log('Uploaded a base64url string!'); }); // Data URL string var message = 'data:text/plain;base64,5b6p5Y+344GX44G+44GX44Gf77yB44GK44KB44Gn44Go44GG77yB'; ref.putString(message, 'data_url').then((snapshot) => { console.log('Uploaded a data_url string!'); });
पहचान फ़ाइल में, फ़ाइल का पूरा पाथ बताया जाता है. इसलिए, पक्का करें कि फ़ाइल को ऐसे पाथ पर अपलोड किया जा रहा हो जो खाली न हो.
फ़ाइल मेटाडेटा जोड़ें
किसी फ़ाइल को अपलोड करते समय, उसका मेटाडेटा भी तय किया जा सकता है.
इस मेटाडेटा में फ़ाइल मेटाडेटा की खास प्रॉपर्टी होती हैं. जैसे, name
, size
, और contentType
(इसे आम तौर पर MIME टाइप कहा जाता है). Cloud Storage, उस फ़ाइल एक्सटेंशन से कॉन्टेंट टाइप का अपने-आप अनुमान लगाता है जहां फ़ाइल को डिस्क पर सेव किया गया है. हालांकि, अगर मेटाडेटा में contentType
के बारे में बताया जाता है, तो अपने-आप पहचानी गई फ़ाइल के टाइप की जगह Cloud Storage लागू हो जाएगा. अगर कोई contentType
मेटाडेटा नहीं दिया गया है और
फ़ाइल का कोई फ़ाइल एक्सटेंशन नहीं है, तो Cloud Storage, डिफ़ॉल्ट रूप से application/octet-stream
टाइप पर सेट हो जाता है. फ़ाइल के मेटाडेटा के बारे में ज़्यादा जानकारी फ़ाइल मेटाडेटा का इस्तेमाल करें सेक्शन में मिल सकती है.
Web
import { getStorage, ref, uploadBytes } from "firebase/storage"; const storage = getStorage(); const storageRef = ref(storage, 'images/mountains.jpg'); // Create file metadata including the content type /** @type {any} */ const metadata = { contentType: 'image/jpeg', }; // Upload the file and metadata const uploadTask = uploadBytes(storageRef, file, metadata);
Web
// Create file metadata including the content type var metadata = { contentType: 'image/jpeg', }; // Upload the file and metadata var uploadTask = storageRef.child('images/mountains.jpg').put(file, metadata);
अपलोड प्रबंधित करें
अपलोड शुरू करने के अलावा, आपके पास pause()
, resume()
, और cancel()
तरीके इस्तेमाल करके, अपलोड को रोकने, फिर से शुरू करने, और रद्द करने का विकल्प होता है. pause()
या resume()
को कॉल करने पर, pause
या running
के स्टेटस में बदलाव होंगे. cancel()
तरीके को कॉल करने से, अपलोड नहीं हो पाता है और गड़बड़ी का मैसेज मिलता है, जिससे पता चलता है कि अपलोड रद्द कर दिया गया है.
Web
import { getStorage, ref, uploadBytesResumable } from "firebase/storage"; const storage = getStorage(); const storageRef = ref(storage, 'images/mountains.jpg'); // Upload the file and metadata const uploadTask = uploadBytesResumable(storageRef, file); // Pause the upload uploadTask.pause(); // Resume the upload uploadTask.resume(); // Cancel the upload uploadTask.cancel();
Web
// Upload the file and metadata var uploadTask = storageRef.child('images/mountains.jpg').put(file); // Pause the upload uploadTask.pause(); // Resume the upload uploadTask.resume(); // Cancel the upload uploadTask.cancel();
अपलोड की स्थिति पर नज़र रखें
अपलोड करते समय, अपलोड टास्क state_changed
ऑब्ज़र्वर में प्रोग्रेस इवेंट को बढ़ा सकता है, जैसे कि:
इवेंट टाइप | आम तौर पर इस्तेमाल किए जाने वाले तरीके |
---|---|
running |
यह इवेंट, टास्क के अपलोड होने पर शुरू होता है या फिर से अपलोड होने पर ट्रिगर होता है. आम तौर पर, इसका इस्तेमाल pause इवेंट के साथ किया जाता है. बड़े
अपलोड के लिए यह इवेंट, प्रोग्रेस अपडेट के तौर पर कई बार ट्रिगर हो सकता है. |
pause |
जब भी अपलोड को रोका जाता है, यह इवेंट फ़ायर हो जाता है. साथ ही, आम तौर पर running इवेंट के साथ इस इवेंट का इस्तेमाल किया जाता है. |
कोई इवेंट होने पर, TaskSnapshot
ऑब्जेक्ट को वापस पास किया जाता है. यह स्नैपशॉट, इवेंट के समय टास्क का नहीं बदला जा सकने वाला व्यू है.
इस ऑब्जेक्ट में ये प्रॉपर्टी शामिल हैं:
प्रॉपर्टी | टाइप | जानकारी |
---|---|---|
bytesTransferred |
Number |
इस स्नैपशॉट को लेने के दौरान, ट्रांसफ़र किए गए बाइट की कुल संख्या. |
totalBytes |
Number |
अपलोड किए जाने वाले बाइट की कुल संख्या. |
state |
firebase.storage.TaskState |
अपलोड की मौजूदा स्थिति. |
metadata |
firebaseStorage.Metadata |
अपलोड पूरा होने से पहले, सर्वर को मेटाडेटा भेजा जाता है. अपलोड पूरा होने के बाद, वह मेटाडेटा जिसे सर्वर ने वापस भेजा. |
task |
firebaseStorage.UploadTask |
यह टास्क का स्नैपशॉट है. इसका इस्तेमाल टास्क को `रोकें`, `फिर से शुरू करें` या `रद्द करें` के लिए किया जा सकता है. |
ref |
firebaseStorage.Reference |
यह टास्क, किस रेफ़रंस से मिला है. |
TaskSnapshot
की प्रॉपर्टी के साथ-साथ इन बदलावों की वजह से, अपलोड इवेंट को मॉनिटर करने का आसान और असरदार तरीका मिल जाता है.
Web
import { getStorage, ref, uploadBytesResumable, getDownloadURL } from "firebase/storage"; const storage = getStorage(); const storageRef = ref(storage, 'images/rivers.jpg'); const uploadTask = uploadBytesResumable(storageRef, file); // Register three observers: // 1. 'state_changed' observer, called any time the state changes // 2. Error observer, called on failure // 3. Completion observer, called on successful completion uploadTask.on('state_changed', (snapshot) => { // Observe state change events such as progress, pause, and resume // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100; console.log('Upload is ' + progress + '% done'); switch (snapshot.state) { case 'paused': console.log('Upload is paused'); break; case 'running': console.log('Upload is running'); break; } }, (error) => { // Handle unsuccessful uploads }, () => { // Handle successful uploads on complete // For instance, get the download URL: https://firebasestorage.googleapis.com/... getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => { console.log('File available at', downloadURL); }); } );
Web
var uploadTask = storageRef.child('images/rivers.jpg').put(file); // Register three observers: // 1. 'state_changed' observer, called any time the state changes // 2. Error observer, called on failure // 3. Completion observer, called on successful completion uploadTask.on('state_changed', (snapshot) => { // Observe state change events such as progress, pause, and resume // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100; console.log('Upload is ' + progress + '% done'); switch (snapshot.state) { case firebase.storage.TaskState.PAUSED: // or 'paused' console.log('Upload is paused'); break; case firebase.storage.TaskState.RUNNING: // or 'running' console.log('Upload is running'); break; } }, (error) => { // Handle unsuccessful uploads }, () => { // Handle successful uploads on complete // For instance, get the download URL: https://firebasestorage.googleapis.com/... uploadTask.snapshot.ref.getDownloadURL().then((downloadURL) => { console.log('File available at', downloadURL); }); } );
गड़बड़ी ठीक करना
अपलोड करते समय गड़बड़ियां होने की कई वजहें हो सकती हैं. जैसे, डिवाइस पर मौजूद फ़ाइल का मौजूद न होना या उपयोगकर्ता को अपनी पसंद की फ़ाइल अपलोड करने की अनुमति न होना. गड़बड़ियों के बारे में ज़्यादा जानकारी, दस्तावेज़ों के गड़बड़ियां मैनेज करना सेक्शन में मिल सकती है.
पूरा उदाहरण
प्रोग्रेस मॉनिटर करने और गड़बड़ियों को ठीक करने के तरीके के साथ अपलोड का पूरा उदाहरण नीचे दिया गया है:
Web
import { getStorage, ref, uploadBytesResumable, getDownloadURL } from "firebase/storage"; const storage = getStorage(); // Create the file metadata /** @type {any} */ const metadata = { contentType: 'image/jpeg' }; // Upload file and metadata to the object 'images/mountains.jpg' const storageRef = ref(storage, 'images/' + file.name); const uploadTask = uploadBytesResumable(storageRef, file, metadata); // Listen for state changes, errors, and completion of the upload. uploadTask.on('state_changed', (snapshot) => { // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100; console.log('Upload is ' + progress + '% done'); switch (snapshot.state) { case 'paused': console.log('Upload is paused'); break; case 'running': console.log('Upload is running'); break; } }, (error) => { // A full list of error codes is available at // https://firebase.google.com/docs/storage/web/handle-errors switch (error.code) { case 'storage/unauthorized': // User doesn't have permission to access the object break; case 'storage/canceled': // User canceled the upload break; // ... case 'storage/unknown': // Unknown error occurred, inspect error.serverResponse break; } }, () => { // Upload completed successfully, now we can get the download URL getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => { console.log('File available at', downloadURL); }); } );
Web
// Create the file metadata var metadata = { contentType: 'image/jpeg' }; // Upload file and metadata to the object 'images/mountains.jpg' var uploadTask = storageRef.child('images/' + file.name).put(file, metadata); // Listen for state changes, errors, and completion of the upload. uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, // or 'state_changed' (snapshot) => { // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100; console.log('Upload is ' + progress + '% done'); switch (snapshot.state) { case firebase.storage.TaskState.PAUSED: // or 'paused' console.log('Upload is paused'); break; case firebase.storage.TaskState.RUNNING: // or 'running' console.log('Upload is running'); break; } }, (error) => { // A full list of error codes is available at // https://firebase.google.com/docs/storage/web/handle-errors switch (error.code) { case 'storage/unauthorized': // User doesn't have permission to access the object break; case 'storage/canceled': // User canceled the upload break; // ... case 'storage/unknown': // Unknown error occurred, inspect error.serverResponse break; } }, () => { // Upload completed successfully, now we can get the download URL uploadTask.snapshot.ref.getDownloadURL().then((downloadURL) => { console.log('File available at', downloadURL); }); } );
अब जब आपने फ़ाइलें अपलोड कर ली हैं, तो आइए अब Cloud Storage से उन्हें डाउनलोड करने का तरीका जानें.