এই পৃষ্ঠায় বর্ণনা করা হয়েছে যে কীভাবে Cloud Storage for Firebase SDK ব্যবহার করে Cloud Storage বাকেট থেকে ফাইল ডাউনলোড করতে হয়।
রেফারেন্স তৈরি করা
কোনও ফাইল ডাউনলোড করতে, প্রথমে আপনি যে ফাইল ডাউনলোড করতে চান সেটির জন্য একটি Cloud Storage রেফারেন্স তৈরি করুন।
আপনি নিজের Cloud Storage বাকেটের রুটে চাইল্ড পাথ যোগ করে রেফারেন্স তৈরি করতে পারেন অথবা Cloud Storage-এ কোনও অবজেক্টকে রেফারেন্স করা আগে থেকে থাকা gs:// বা https:// URL থেকে রেফারেন্স তৈরি করতে পারেন।
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');
URL-এর মাধ্যমে ডেটা ডাউনলোড করুন
আপনি Cloud Storage রেফারেন্সে getDownloadURL() পদ্ধতি কল করে কোনও ফাইলের ডাউনলোড URL পেতে পারেন।
Web
import { getStorage, ref, getDownloadURL } from "firebase/storage"; const storage = getStorage(); const starsRef = ref(storage, 'images/stars.jpg'); getDownloadURL(starsRef) .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 সরাসরি ডাউনলোডের জন্য এইসব ফাংশন প্রদান করে:
এইসব ফাংশন ব্যবহার করে, আপনি URL থেকে ডাউনলোড করার প্রসেস বাইপাস করতে পারবেন এবং এর পরিবর্তে আপনার কোডে ডেটা ফেরত পাবেন। এর ফলে Firebase Security Rules-এর মাধ্যমে আরও সূক্ষ্মভাবে অ্যাক্সেস কন্ট্রোল করা যায়।
CORS কনফিগারেশন
সরাসরি ব্রাউজারে ডেটা ডাউনলোড করতে, আপনাকে অবশ্যই ক্রস-অরিজিন অ্যাক্সেসের (CORS) জন্য
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 download 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 download break; // ... case 'storage/unknown': // Unknown error occurred, inspect the server response break; } });
এছাড়াও, আপনি Cloud Storage-এ স্টোর করা ফাইলের জন্য মেটাডেটা পেতে বা আপডেট করতে পারবেন।