Cloud Storage for Firebase से आपको तुरंत और आसानी से डाउनलोड करने की सुविधा मिलती है Cloud Storage की फ़ाइलें Firebase उपलब्ध कराने और मैनेज करने के लिए बकेट.
रेफ़रंस बनाना
फ़ाइल डाउनलोड करने के लिए, सबसे पहले Cloud Storage पहचान फ़ाइल बनाएं उस फ़ाइल को डाउनलोड करें जिसे आपको डाउनलोड करना है.
आप अपने चैनल के रूट में चाइल्ड पाथ को जोड़कर रेफ़रंस बना सकते हैं
Cloud Storage बकेट या किसी मौजूदा बकेट से पहचान फ़ाइल बनाई जा सकती है
Cloud Storage में किसी ऑब्जेक्ट का रेफ़रंस देने वाला gs://
या https://
यूआरएल.
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');
यूआरएल की मदद से डेटा डाउनलोड करना
फ़ाइल डाउनलोड करने का यूआरएल पाने के लिए,
Cloud Storage रेफ़रंस पर getDownloadURL()
तरीका.
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 टूल इन फ़ंक्शन को सीधे डाउनलोड:
इन फ़ंक्शन का इस्तेमाल करके, किसी यूआरएल से डेटा डाउनलोड करने की प्रोसेस को बायपास किया जा सकता है. इसके बजाय, कोड में डेटा दिखाया जा सकता है. इससे आपको, इनके ज़रिए ज़्यादा सटीक ऐक्सेस कंट्रोल मिलता है 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 में सेव की गई फ़ाइलों के लिए.