फायरबेस के लिए क्लाउड स्टोरेज आपको फायरबेस द्वारा प्रदान और प्रबंधित क्लाउड स्टोरेज बकेट में फाइलों को जल्दी और आसानी से अपलोड करने की अनुमति देता है।
फाइल अपलोड करो
क्लाउड स्टोरेज में फ़ाइल अपलोड करने के लिए, आप पहले फ़ाइल के नाम सहित फ़ाइल के पूर्ण पथ का संदर्भ बनाते हैं।
Web version 9
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 version 8
// 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()
जावास्क्रिप्ट फ़ाइल और ब्लॉब एपीआई के माध्यम से फाइलें लेता है और उन्हें क्लाउड स्टोरेज पर अपलोड करता है।
Web version 9
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 version 8
// 'file' comes from the Blob or File API ref.put(file).then((snapshot) => { console.log('Uploaded a blob or file!'); });
बाइट ऐरे से अपलोड करें
File
और Blob
प्रकारों के अलावा, uploadBytes()
एक Uint8Array
को क्लाउड स्टोरेज पर भी अपलोड कर सकता है।
Web version 9
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 version 8
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()
विधि का उपयोग करके क्लाउड स्टोरेज में एक कच्चा, base64
, बेस base64url
, या data_url
एन्कोडेड स्ट्रिंग अपलोड कर सकते हैं।
Web version 9
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 version 8
// 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!'); });
UploadTask
uploadString()
एक अपलोड टास्क देता है, जिसे आप एक वादे के रूप में उपयोग कर सकते हैं या अपलोड की स्थिति को प्रबंधित और मॉनिटर करने के लिए उपयोग कर सकते हैं।
चूंकि संदर्भ फ़ाइल के पूर्ण पथ को परिभाषित करता है, इसलिए सुनिश्चित करें कि आप एक गैर-रिक्त पथ पर अपलोड कर रहे हैं।
फ़ाइल मेटाडेटा जोड़ें
फ़ाइल अपलोड करते समय, आप उस फ़ाइल के लिए मेटाडेटा भी निर्दिष्ट कर सकते हैं। इस मेटाडेटा में विशिष्ट फ़ाइल मेटाडेटा गुण जैसे name
, size
और contentType
(आमतौर पर MIME प्रकार के रूप में संदर्भित) शामिल हैं। क्लाउड स्टोरेज स्वचालित रूप से उस फ़ाइल एक्सटेंशन से सामग्री प्रकार का अनुमान लगाता है जहां फ़ाइल डिस्क पर संग्रहीत होती है, लेकिन यदि आप contentType
में सामग्री प्रकार निर्दिष्ट करते हैं तो यह स्वतः-पता लगाने वाले प्रकार को ओवरराइड कर देगा। यदि कोई सामग्री प्रकार मेटाडेटा निर्दिष्ट नहीं है और फ़ाइल में फ़ाइल एक्सटेंशन नहीं है, तो क्लाउड संग्रहण डिफ़ॉल्ट रूप से application/octet-stream
प्रकार पर contentType
करता है। फ़ाइल मेटाडेटा के बारे में अधिक जानकारी फ़ाइल मेटाडेटा का उपयोग करें अनुभाग में पाई जा सकती है।
Web version 9
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 version 8
// 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 version 9
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 version 8
// 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 version 9
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 version 8
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 version 9
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 version 8
// 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); }); } );
अब जब आपने फ़ाइलें अपलोड कर दी हैं, तो आइए जानें कि उन्हें क्लाउड स्टोरेज से कैसे डाउनलोड किया जाए ।