Cloud Storage for Firebase আপনাকে ফায়ারবেস দ্বারা সরবরাহকৃত ও পরিচালিত একটি Cloud Storage বাকেটে দ্রুত এবং সহজে ফাইল আপলোড করার সুযোগ দেয়।
ফাইল আপলোড করুন
Cloud Storage কোনো ফাইল আপলোড করতে হলে, প্রথমে ফাইলের নামসহ সেটির সম্পূর্ণ পাথের একটি রেফারেন্স তৈরি করতে হবে।
Web
import { getStorage, ref } from "firebase/storage"; // Create a root reference const storage = getStorage(); // Create a reference to 'mountains.jpg' const mountainsRef = ref(storage, 'mountains.jpg'); // Create a reference to 'images/mountains.jpg' const mountainImagesRef = ref(storage, 'images/mountains.jpg'); // While the file names are the same, the references point to different files mountainsRef.name === mountainImagesRef.name; // true mountainsRef.fullPath === mountainImagesRef.fullPath; // false
Web
// Create a root reference var storageRef = firebase.storage().ref(); // Create a reference to 'mountains.jpg' var mountainsRef = storageRef.child('mountains.jpg'); // Create a reference to 'images/mountains.jpg' var mountainImagesRef = storageRef.child('images/mountains.jpg'); // While the file names are the same, the references point to different files mountainsRef.name === mountainImagesRef.name; // true mountainsRef.fullPath === mountainImagesRef.fullPath; // false
Blob বা File থেকে আপলোড করুন
একবার আপনি একটি উপযুক্ত রেফারেন্স তৈরি করে নিলে, এরপর আপনাকে uploadBytes() মেথডটি কল করতে হবে। uploadBytes() জাভাস্ক্রিপ্টের ফাইল এবং ব্লব এপিআই ব্যবহার করে ফাইল গ্রহণ করে এবং সেগুলোকে Cloud Storage আপলোড করে।
Web
import { getStorage, ref, uploadBytes } from "firebase/storage"; const storage = getStorage(); const storageRef = ref(storage, 'some-child'); // 'file' comes from the Blob or File API uploadBytes(storageRef, file).then((snapshot) => { console.log('Uploaded a blob or file!'); });
Web
// 'file' comes from the Blob or File API ref.put(file).then((snapshot) => { console.log('Uploaded a blob or file!'); });
বাইট অ্যারে থেকে আপলোড
File এবং Blob টাইপের পাশাপাশি, uploadBytes() একটি Uint8Array ও Cloud Storage আপলোড করতে পারে।
Web
import { getStorage, ref, uploadBytes } from "firebase/storage"; const storage = getStorage(); const storageRef = ref(storage, 'some-child'); const bytes = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21]); uploadBytes(storageRef, bytes).then((snapshot) => { console.log('Uploaded an array!'); });
Web
var bytes = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21]); ref.put(bytes).then((snapshot) => { console.log('Uploaded an array!'); });
একটি স্ট্রিং থেকে আপলোড করুন
যদি কোনো Blob , File , বা Uint8Array উপলব্ধ না থাকে, তাহলে আপনি Cloud Storage এ একটি raw, base64 , base64url , বা data_url এনকোডেড স্ট্রিং আপলোড করতে uploadString() মেথডটি ব্যবহার করতে পারেন।
Web
import { getStorage, ref, uploadString } from "firebase/storage"; const storage = getStorage(); const storageRef = ref(storage, 'some-child'); // Raw string is the default if no format is provided const message = 'This is my message.'; uploadString(storageRef, message).then((snapshot) => { console.log('Uploaded a raw string!'); }); // Base64 formatted string const message2 = '5b6p5Y+344GX44G+44GX44Gf77yB44GK44KB44Gn44Go44GG77yB'; uploadString(storageRef, message2, 'base64').then((snapshot) => { console.log('Uploaded a base64 string!'); }); // Base64url formatted string const message3 = '5b6p5Y-344GX44G-44GX44Gf77yB44GK44KB44Gn44Go44GG77yB'; uploadString(storageRef, message3, 'base64url').then((snapshot) => { console.log('Uploaded a base64url string!'); }); // Data URL string const message4 = 'data:text/plain;base64,5b6p5Y+344GX44G+44GX44Gf77yB44GK44KB44Gn44Go44GG77yB'; uploadString(storageRef, message4, 'data_url').then((snapshot) => { console.log('Uploaded a data_url string!'); });
Web
// Raw string is the default if no format is provided var message = 'This is my message.'; ref.putString(message).then((snapshot) => { console.log('Uploaded a raw string!'); }); // Base64 formatted string var message = '5b6p5Y+344GX44G+44GX44Gf77yB44GK44KB44Gn44Go44GG77yB'; ref.putString(message, 'base64').then((snapshot) => { console.log('Uploaded a base64 string!'); }); // Base64url formatted string var message = '5b6p5Y-344GX44G-44GX44Gf77yB44GK44KB44Gn44Go44GG77yB'; ref.putString(message, 'base64url').then((snapshot) => { console.log('Uploaded a base64url string!'); }); // Data URL string var message = 'data:text/plain;base64,5b6p5Y+344GX44G+44GX44Gf77yB44GK44KB44Gn44Go44GG77yB'; ref.putString(message, 'data_url').then((snapshot) => { console.log('Uploaded a data_url string!'); });
যেহেতু রেফারেন্সটি ফাইলের সম্পূর্ণ পাথ নির্দেশ করে, তাই নিশ্চিত করুন যে আপনি একটি খালি নয় এমন পাথে আপলোড করছেন।
ফাইল মেটাডেটা যোগ করুন
ফাইল আপলোড করার সময়, আপনি সেই ফাইলের জন্য মেটাডেটাও নির্দিষ্ট করতে পারেন। এই মেটাডেটাতে ফাইলের সাধারণ মেটাডেটা প্রোপার্টি যেমন name , size এবং contentType (যা সাধারণত MIME টাইপ নামে পরিচিত) থাকে। Cloud Storage ডিস্কে ফাইলটি যেখানে সংরক্ষিত থাকে, সেখানকার ফাইল এক্সটেনশন থেকে স্বয়ংক্রিয়ভাবে কন্টেন্ট টাইপ অনুমান করে নেয়, কিন্তু আপনি যদি মেটাডেটাতে একটি contentType নির্দিষ্ট করেন, তবে এটি স্বয়ংক্রিয়ভাবে শনাক্ত করা টাইপটিকে ওভাররাইড করবে। যদি কোনো contentType মেটাডেটা নির্দিষ্ট করা না থাকে এবং ফাইলটির কোনো ফাইল এক্সটেনশন না থাকে, তবে Cloud Storage ডিফল্টভাবে application/octet-stream টাইপটি ব্যবহার করে। ফাইল মেটাডেটা সম্পর্কে আরও তথ্য "ফাইল মেটাডেটা ব্যবহার করুন" বিভাগে পাওয়া যাবে।
Web
import { getStorage, ref, uploadBytes } from "firebase/storage"; const storage = getStorage(); const storageRef = ref(storage, 'images/mountains.jpg'); // Create file metadata including the content type /** @type {any} */ const metadata = { contentType: 'image/jpeg', }; // Upload the file and metadata const uploadTask = uploadBytes(storageRef, file, metadata);
Web
// Create file metadata including the content type var metadata = { contentType: 'image/jpeg', }; // Upload the file and metadata var uploadTask = storageRef.child('images/mountains.jpg').put(file, metadata);
আপলোডগুলি পরিচালনা করুন
আপলোড শুরু করার পাশাপাশি, আপনি pause() , resume() , এবং cancel() মেথডগুলো ব্যবহার করে আপলোড থামাতে, পুনরায় শুরু করতে এবং বাতিল করতে পারেন। pause() বা resume() কল করলে আপলোডটি pause যাবে বা running অবস্থায় চলে যাবে। cancel() মেথড কল করলে আপলোডটি ব্যর্থ হবে এবং একটি এরর রিটার্ন করবে, যা নির্দেশ করবে যে আপলোডটি বাতিল করা হয়েছে।
Web
import { getStorage, ref, uploadBytesResumable } from "firebase/storage"; const storage = getStorage(); const storageRef = ref(storage, 'images/mountains.jpg'); // Upload the file and metadata const uploadTask = uploadBytesResumable(storageRef, file); // Pause the upload uploadTask.pause(); // Resume the upload uploadTask.resume(); // Cancel the upload uploadTask.cancel();
Web
// Upload the file and metadata var uploadTask = storageRef.child('images/mountains.jpg').put(file); // Pause the upload uploadTask.pause(); // Resume the upload uploadTask.resume(); // Cancel the upload uploadTask.cancel();
আপলোড অগ্রগতি নিরীক্ষণ করুন
আপলোড করার সময়, আপলোড টাস্কটি state_changed অবজারভার-এ অগ্রগতি ইভেন্ট তৈরি করতে পারে, যেমন:
| ইভেন্টের ধরণ | সাধারণ ব্যবহার |
|---|---|
running | টাস্কটি আপলোড শুরু বা পুনরায় শুরু করলে এই ইভেন্টটি ফায়ার হয় এবং এটি প্রায়শই pause ইভেন্টের সাথে একত্রে ব্যবহৃত হয়। বড় আকারের আপলোডের ক্ষেত্রে, অগ্রগতি আপডেট হিসেবে এই ইভেন্টটি একাধিকবার ফায়ার হতে পারে। |
pause | আপলোড পজ করা হলেই এই ইভেন্টটি ট্রিগার হয় এবং এটি প্রায়শই running ইভেন্টের সাথে একত্রে ব্যবহৃত হয়। |
যখন কোনো ইভেন্ট ঘটে, তখন একটি TaskSnapshot অবজেক্ট ফেরত পাঠানো হয়। এই স্ন্যাপশটটি হলো ইভেন্টটি ঘটার সময়কার টাস্কটির একটি অপরিবর্তনীয় চিত্র। এই অবজেক্টটিতে নিম্নলিখিত প্রোপার্টিগুলো থাকে:
| সম্পত্তি | প্রকার | বর্ণনা |
|---|---|---|
bytesTransferred | Number | এই স্ন্যাপশটটি নেওয়ার সময় স্থানান্তরিত মোট বাইটের সংখ্যা। |
totalBytes | Number | মোট যত বাইট আপলোড হওয়ার কথা, তার পরিমাণ। |
state | firebase.storage.TaskState | আপলোডের বর্তমান অবস্থা। |
metadata | firebaseStorage.Metadata | আপলোড সম্পন্ন হওয়ার আগে মেটাডেটা সার্ভারে পাঠানো হয়। আপলোড সম্পন্ন হওয়ার পরে মেটাডেটা সার্ভার থেকে ফেরত পাঠানো হয়। |
task | firebaseStorage.UploadTask | এটি টাস্কটির একটি স্ন্যাপশট, যা ব্যবহার করে টাস্কটি `pause`, `resume` বা `cancel` করা যায়। |
ref | firebaseStorage.Reference | এই কাজটি যে সূত্র থেকে নেওয়া হয়েছে। |
অবস্থার এই পরিবর্তনগুলো, TaskSnapshot এর বৈশিষ্ট্যগুলোর সাথে মিলিত হয়ে, আপলোড ইভেন্টগুলো নিরীক্ষণ করার একটি সহজ অথচ শক্তিশালী উপায় প্রদান করে।
Web
import { getStorage, ref, uploadBytesResumable, getDownloadURL } from "firebase/storage"; const storage = getStorage(); const storageRef = ref(storage, 'images/rivers.jpg'); const uploadTask = uploadBytesResumable(storageRef, file); // Register three observers: // 1. 'state_changed' observer, called any time the state changes // 2. Error observer, called on failure // 3. Completion observer, called on successful completion uploadTask.on('state_changed', (snapshot) => { // Observe state change events such as progress, pause, and resume // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100; console.log('Upload is ' + progress + '% done'); switch (snapshot.state) { case 'paused': console.log('Upload is paused'); break; case 'running': console.log('Upload is running'); break; } }, (error) => { // Handle unsuccessful uploads }, () => { // Handle successful uploads on complete // For instance, get the download URL: https://firebasestorage.googleapis.com/... getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => { console.log('File available at', downloadURL); }); } );
Web
var uploadTask = storageRef.child('images/rivers.jpg').put(file); // Register three observers: // 1. 'state_changed' observer, called any time the state changes // 2. Error observer, called on failure // 3. Completion observer, called on successful completion uploadTask.on('state_changed', (snapshot) => { // Observe state change events such as progress, pause, and resume // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100; console.log('Upload is ' + progress + '% done'); switch (snapshot.state) { case firebase.storage.TaskState.PAUSED: // or 'paused' console.log('Upload is paused'); break; case firebase.storage.TaskState.RUNNING: // or 'running' console.log('Upload is running'); break; } }, (error) => { // Handle unsuccessful uploads }, () => { // Handle successful uploads on complete // For instance, get the download URL: https://firebasestorage.googleapis.com/... uploadTask.snapshot.ref.getDownloadURL().then((downloadURL) => { console.log('File available at', downloadURL); }); } );
ত্রুটি পরিচালনা
আপলোড করার সময় বিভিন্ন কারণে ত্রুটি ঘটতে পারে, যার মধ্যে রয়েছে স্থানীয়ভাবে ফাইলটির অস্তিত্ব না থাকা, অথবা কাঙ্ক্ষিত ফাইলটি আপলোড করার জন্য ব্যবহারকারীর অনুমতি না থাকা। ত্রুটি সম্পর্কে আরও তথ্য ডকুমেন্টেশনের ' Handle Errors' বিভাগে পাওয়া যাবে।
সম্পূর্ণ উদাহরণ
অগ্রগতি পর্যবেক্ষণ এবং ত্রুটি পরিচালনা সহ একটি আপলোডের সম্পূর্ণ উদাহরণ নিচে দেখানো হলো:
Web
import { getStorage, ref, uploadBytesResumable, getDownloadURL } from "firebase/storage"; const storage = getStorage(); // Create the file metadata /** @type {any} */ const metadata = { contentType: 'image/jpeg' }; // Upload file and metadata to the object 'images/mountains.jpg' const storageRef = ref(storage, 'images/' + file.name); const uploadTask = uploadBytesResumable(storageRef, file, metadata); // Listen for state changes, errors, and completion of the upload. uploadTask.on('state_changed', (snapshot) => { // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100; console.log('Upload is ' + progress + '% done'); switch (snapshot.state) { case 'paused': console.log('Upload is paused'); break; case 'running': console.log('Upload is running'); break; } }, (error) => { // A full list of error codes is available at // https://firebase.google.com/docs/storage/web/handle-errors switch (error.code) { 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 error.serverResponse break; } }, () => { // Upload completed successfully, now we can get the download URL getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => { console.log('File available at', downloadURL); }); } );
Web
// Create the file metadata var metadata = { contentType: 'image/jpeg' }; // Upload file and metadata to the object 'images/mountains.jpg' var uploadTask = storageRef.child('images/' + file.name).put(file, metadata); // Listen for state changes, errors, and completion of the upload. uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, // or 'state_changed' (snapshot) => { // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100; console.log('Upload is ' + progress + '% done'); switch (snapshot.state) { case firebase.storage.TaskState.PAUSED: // or 'paused' console.log('Upload is paused'); break; case firebase.storage.TaskState.RUNNING: // or 'running' console.log('Upload is running'); break; } }, (error) => { // A full list of error codes is available at // https://firebase.google.com/docs/storage/web/handle-errors switch (error.code) { 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 error.serverResponse break; } }, () => { // Upload completed successfully, now we can get the download URL uploadTask.snapshot.ref.getDownloadURL().then((downloadURL) => { console.log('File available at', downloadURL); }); } );
এখন যেহেতু আপনি ফাইলগুলো আপলোড করেছেন, চলুন জেনে নিই Cloud Storage থেকে কীভাবে সেগুলো ডাউনলোড করতে হয়।