वेब पर Cloud Storage की मदद से फ़ाइलें डाउनलोड करना

'Firebase के लिए Cloud Storage' की मदद से, Firebase की ओर से उपलब्ध कराए गए और मैनेज किए जा रहे Cloud Storage बकेट से फ़ाइलों को तेज़ी और आसानी से डाउनलोड किया जा सकता है.

रेफ़रंस बनाना

किसी फ़ाइल को डाउनलोड करने के लिए, पहले उस फ़ाइल के लिए Cloud Storage रेफ़रंस बनाएं जिसे आपको डाउनलोड करना है.

अपने Cloud Storage बकेट के रूट में चाइल्ड पाथ जोड़कर, रेफ़रंस बनाया जा सकता है. इसके अलावा, Cloud Storage में मौजूद किसी ऑब्जेक्ट के बारे में बताने वाले मौजूदा gs:// या https:// यूआरएल से भी रेफ़रंस बनाया जा सकता है.

वेब मॉड्यूलर एपीआई

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');  

वेब नेमस्पेसेड एपीआई

// 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() तरीके का इस्तेमाल करें.

वेब मॉड्यूलर एपीआई

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
  });

वेब नेमस्पेसेड एपीआई

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 के सुरक्षा नियमों के ज़रिए, बेहतर ऐक्सेस कंट्रोल दिया जा सकता है.

सीओआरएस कॉन्फ़िगरेशन

डेटा को सीधे ब्राउज़र में डाउनलोड करने के लिए, आपको क्रॉस-ऑरिजिन ऐक्सेस (सीओआरएस) के लिए अपनी Cloud Storage बकेट कॉन्फ़िगर करनी होगी. यह काम gsutil कमांड लाइन टूल की मदद से किया जा सकता है. इस टूल को यहां से इंस्टॉल किया जा सकता है.

अगर आपको डोमेन पर आधारित कोई पाबंदी (सबसे आम स्थिति) नहीं चाहिए, तो इस JSON को cors.json नाम की फ़ाइल में कॉपी करें:

[
  {
    "origin": ["*"],
    "method": ["GET"],
    "maxAgeSeconds": 3600
  }
]

इन पाबंदियों को लागू करने के लिए, gsutil cors set cors.json gs://<your-cloud-storage-bucket> चलाएं.

ज़्यादा जानकारी के लिए, Google Cloud Storage के दस्तावेज़ देखें.

गड़बड़ियां ठीक करना

डाउनलोड करते समय गड़बड़ियां होने की कई वजहें हो सकती हैं. इनमें, फ़ाइल का मौजूद न होना या उपयोगकर्ता को अपनी पसंद की फ़ाइल ऐक्सेस करने की अनुमति न होना शामिल है. गड़बड़ियों के बारे में ज़्यादा जानकारी, दस्तावेज़ों के गड़बड़ियां मैनेज करना सेक्शन में मिल सकती है.

पूरा उदाहरण

गड़बड़ी ठीक करने के तरीके के साथ डाउनलोड का पूरा उदाहरण नीचे दिखाया गया है:

वेब मॉड्यूलर एपीआई

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;
    }
  });

वेब नेमस्पेसेड एपीआई

// 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 में सेव की गई फ़ाइलों के लिए, मेटाडेटा मिल सकता है या उसे अपडेट किया जा सकता है.