Cloud Storage for Firebase की मदद से, Firebase की ओर से उपलब्ध कराए गए और मैनेज किए जाने वाले Cloud Storage बकेट से, फ़ाइलें तुरंत और आसानी से डाउनलोड की जा सकती हैं.
रेफ़रंस बनाना
कोई फ़ाइल डाउनलोड करने के लिए, सबसे पहले उस फ़ाइल का Cloud Storage रेफ़रंस बनाएं जिसे डाउनलोड करना है.
आप अपने
Cloud Storage बकेट के रूट में चाइल्ड पाथ जोड़कर रेफ़रंस बना सकते हैं या किसी मौजूदा
gs:// या https:// यूआरएल से रेफ़रंस बना सकते हैं जो Cloud Storage में किसी ऑब्जेक्ट को रेफ़रंस करता है.
Web
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
// 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() तरीके को कॉल करके, किसी फ़ाइल का डाउनलोड यूआरएल पाया जा सकता है.Cloud Storage
Web
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
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 });
SDK टूल से सीधे डेटा डाउनलोड करना
वर्शन 9.5 और उसके बाद के वर्शन में, SDK टूल सीधे डाउनलोड करने के लिए ये फ़ंक्शन उपलब्ध कराता है:
इन फ़ंक्शन का इस्तेमाल करके, यूआरएल से डाउनलोड करने के बजाय, अपने कोड में डेटा वापस पाया जा सकता है. इससे, ऐक्सेस कंट्रोल को ज़्यादा बेहतर तरीके से कंट्रोल किया जा सकता है via Firebase Security Rules.
सीओआरएस कॉन्फ़िगरेशन
ब्राउज़र में सीधे डेटा डाउनलोड करने के लिए, आपको अपने
Cloud Storage बकेट को ऑरिजिन के बीच ऐक्सेस (सीओआरएस) के लिए कॉन्फ़िगर करना होगा. यह काम, gsutil कमांड लाइन टूल की मदद से किया जा सकता है. इसे
यहां से इंस्टॉल किया जा सकता है.
अगर आपको डोमेन के आधार पर कोई पाबंदी नहीं लगानी है (यह सबसे आम स्थिति है), तो इस JSON को cors.json नाम की किसी फ़ाइल में कॉपी करें:
[
{
"origin": ["*"],
"method": ["GET"],
"maxAgeSeconds": 3600
}
]इन पाबंदियों को लागू करने के लिए, gsutil cors set cors.json gs://<your-cloud-storage-bucket> चलाएं.
ज़्यादा जानकारी के लिए, Google Cloud Storage दस्तावेज़ देखें.
गड़बड़ियां ठीक करना
डाउनलोड करने पर गड़बड़ियां होने की कई वजहें हो सकती हैं. इनमें, फ़ाइल का मौजूद न होना या उपयोगकर्ता के पास, अपनी पसंद की फ़ाइल को ऐक्सेस करने की अनुमति न होना शामिल है. गड़बड़ियों के बारे में ज़्यादा जानकारी, दस्तावेज़ों के गड़बड़ियां ठीक करना सेक्शन में देखी जा सकती है.
पूरी जानकारी वाला उदाहरण
गड़बड़ी ठीक करने की सुविधा के साथ डाउनलोड करने का पूरा उदाहरण यहां दिया गया है:
Web
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
// 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; } });
आप मेटाडेटा पा या अपडेट कर सकते हैं उन फ़ाइलों के लिए जो Cloud Storage में सेव की गई हैं.