ওয়েবে ক্লাউড স্টোরেজ দিয়ে ফাইল ডাউনলোড করুন

Firebase-এর জন্য ক্লাউড স্টোরেজ আপনাকে Firebase দ্বারা প্রদত্ত এবং পরিচালিত ক্লাউড স্টোরেজ বাকেট থেকে দ্রুত এবং সহজে ফাইল ডাউনলোড করতে দেয়।

একটি রেফারেন্স তৈরি করুন

একটি ফাইল ডাউনলোড করতে, প্রথমে আপনি যে ফাইলটি ডাউনলোড করতে চান তার একটি ক্লাউড স্টোরেজ রেফারেন্স তৈরি করুন

আপনি আপনার ক্লাউড স্টোরেজ বাকেটের রুটে চাইল্ড পাথ যুক্ত করে একটি রেফারেন্স তৈরি করতে পারেন, অথবা আপনি ক্লাউড স্টোরেজের একটি অবজেক্টকে উল্লেখ করে বিদ্যমান gs:// বা https:// URL থেকে একটি রেফারেন্স তৈরি করতে পারেন।

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

URL এর মাধ্যমে ডেটা ডাউনলোড করুন

আপনি ক্লাউড স্টোরেজ রেফারেন্সে getDownloadURL() পদ্ধতিতে কল করে একটি ফাইলের জন্য ডাউনলোড URL পেতে পারেন।

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

SDK থেকে সরাসরি ডেটা ডাউনলোড করুন

সংস্করণ 9.5 এবং উচ্চতর থেকে, SDK সরাসরি ডাউনলোডের জন্য এই ফাংশনগুলি প্রদান করে:

এই ফাংশনগুলি ব্যবহার করে, আপনি একটি URL থেকে ডাউনলোড বাইপাস করতে পারেন এবং পরিবর্তে আপনার কোডে ডেটা ফেরত দিতে পারেন৷ এটি ফায়ারবেস নিরাপত্তা বিধিগুলির মাধ্যমে সূক্ষ্মভাবে অ্যাক্সেস নিয়ন্ত্রণের অনুমতি দেয়৷

CORS কনফিগারেশন

ব্রাউজারে সরাসরি ডেটা ডাউনলোড করতে, আপনাকে অবশ্যই ক্রস-অরিজিন অ্যাক্সেস (CORS) এর জন্য আপনার ক্লাউড স্টোরেজ বাকেট কনফিগার করতে হবে। এটি 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;
  }
});

এছাড়াও আপনি ক্লাউড স্টোরেজে সঞ্চিত ফাইলগুলির জন্য মেটাডেটা পেতে বা আপডেট করতে পারেন৷