फायरबेस के लिए क्लाउड स्टोरेज आपको फायरबेस द्वारा प्रदान और प्रबंधित क्लाउड स्टोरेज बकेट से फ़ाइलों को जल्दी और आसानी से डाउनलोड करने की अनुमति देता है।
एक संदर्भ बनाएँ
किसी फ़ाइल को डाउनलोड करने के लिए, पहले उस फ़ाइल का क्लाउड स्टोरेज संदर्भ बनाएं जिसे आप डाउनलोड करना चाहते हैं।
आप अपने क्लाउड स्टोरेज बकेट के रूट में चाइल्ड पाथ जोड़कर एक संदर्भ बना सकते हैं, या आप क्लाउड स्टोरेज में किसी ऑब्जेक्ट को संदर्भित करने वाले मौजूदा gs://
या https://
यूआरएल से एक संदर्भ बना सकते हैं।
Web modular API
import { getStorage, ref } from "firebase/storage"; // Create a reference with an initial file path and name const storage = getStorage(); const pathReference = ref(storage, 'images/stars.jpg'); // Create a reference from a Google Cloud Storage URI const gsReference = ref(storage, 'gs://bucket/images/stars.jpg'); // Create a reference from an HTTPS URL // Note that in the URL, characters are URL escaped! const httpsReference = ref(storage, 'https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg');
Web namespaced API
// Create a reference with an initial file path and name var storage = firebase.storage(); var pathReference = storage.ref('images/stars.jpg'); // Create a reference from a Google Cloud Storage URI var gsReference = storage.refFromURL('gs://bucket/images/stars.jpg'); // Create a reference from an HTTPS URL // Note that in the URL, characters are URL escaped! var httpsReference = storage.refFromURL('https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg');
यूआरएल के माध्यम से डेटा डाउनलोड करें
आप क्लाउड स्टोरेज संदर्भ पर getDownloadURL()
विधि को कॉल करके किसी फ़ाइल के लिए डाउनलोड यूआरएल प्राप्त कर सकते हैं।
Web modular API
import { getStorage, ref, getDownloadURL } from "firebase/storage"; const storage = getStorage(); getDownloadURL(ref(storage, 'images/stars.jpg')) .then((url) => { // `url` is the download URL for 'images/stars.jpg' // This can be downloaded directly: const xhr = new XMLHttpRequest(); xhr.responseType = 'blob'; xhr.onload = (event) => { const blob = xhr.response; }; xhr.open('GET', url); xhr.send(); // Or inserted into an <img> element const img = document.getElementById('myimg'); img.setAttribute('src', url); }) .catch((error) => { // Handle any errors });
Web namespaced API
storageRef.child('images/stars.jpg').getDownloadURL() .then((url) => { // `url` is the download URL for 'images/stars.jpg' // This can be downloaded directly: var xhr = new XMLHttpRequest(); xhr.responseType = 'blob'; xhr.onload = (event) => { var blob = xhr.response; }; xhr.open('GET', url); xhr.send(); // Or inserted into an <img> element var img = document.getElementById('myimg'); img.setAttribute('src', url); }) .catch((error) => { // Handle any errors });
एसडीके से सीधे डेटा डाउनलोड करें
संस्करण 9.5 और उच्चतर से, एसडीके सीधे डाउनलोड के लिए ये फ़ंक्शन प्रदान करता है:
इन फ़ंक्शंस का उपयोग करके, आप किसी URL से डाउनलोडिंग को बायपास कर सकते हैं, और इसके बजाय अपने कोड में डेटा लौटा सकते हैं। यह फायरबेस सुरक्षा नियमों के माध्यम से बेहतर पहुंच नियंत्रण की अनुमति देता है।
सीओआरएस कॉन्फ़िगरेशन
ब्राउज़र में सीधे डेटा डाउनलोड करने के लिए, आपको क्रॉस-ऑरिजिन एक्सेस (सीओआरएस) के लिए अपने क्लाउड स्टोरेज बकेट को कॉन्फ़िगर करना होगा। यह gsutil
कमांड लाइन टूल से किया जा सकता है, जिसे आप यहां से इंस्टॉल कर सकते हैं।
यदि आप कोई डोमेन-आधारित प्रतिबंध (सबसे आम परिदृश्य) नहीं चाहते हैं, तो इस JSON को cors.json
नाम की फ़ाइल में कॉपी करें:
[ { "origin": ["*"], "method": ["GET"], "maxAgeSeconds": 3600 } ]
इन प्रतिबंधों को तैनात करने के लिए gsutil cors set cors.json gs://<your-cloud-storage-bucket>
चलाएँ।
अधिक जानकारी के लिए, Google क्लाउड स्टोरेज दस्तावेज़ देखें।
त्रुटियाँ संभालें
ऐसे कई कारण हैं जिनकी वजह से डाउनलोड पर त्रुटियां हो सकती हैं, जिनमें फ़ाइल का मौजूदा न होना, या उपयोगकर्ता के पास वांछित फ़ाइल तक पहुंचने की अनुमति न होना शामिल है। त्रुटियों के बारे में अधिक जानकारी डॉक्स के हैंडल एरर्स अनुभाग में पाई जा सकती है।
पूर्ण उदाहरण
त्रुटि प्रबंधन के साथ डाउनलोड का एक पूरा उदाहरण नीचे दिखाया गया है:
Web modular API
import { getStorage, ref, getDownloadURL } from "firebase/storage"; // Create a reference to the file we want to download const storage = getStorage(); const starsRef = ref(storage, 'images/stars.jpg'); // Get the download URL getDownloadURL(starsRef) .then((url) => { // Insert url into an <img> tag to "download" }) .catch((error) => { // A full list of error codes is available at // https://firebase.google.com/docs/storage/web/handle-errors switch (error.code) { case 'storage/object-not-found': // File doesn't exist break; 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 the server response break; } });
Web namespaced API
// Create a reference to the file we want to download var starsRef = storageRef.child('images/stars.jpg'); // Get the download URL starsRef.getDownloadURL() .then((url) => { // Insert url into an <img> tag to "download" }) .catch((error) => { // A full list of error codes is available at // https://firebase.google.com/docs/storage/web/handle-errors switch (error.code) { case 'storage/object-not-found': // File doesn't exist break; 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 the server response break; } });
आप क्लाउड स्टोरेज में संग्रहीत फ़ाइलों के लिए मेटाडेटा भी प्राप्त या अपडेट कर सकते हैं।