ওয়েবে Cloud Storage-এর মাধ্যমে ফাইল আপলোড করা

Cloud Storage for Firebase আপনাকে 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() জাভাস্ক্রিপ্ট File এবং Blob API-এর মাধ্যমে ফাইল নেয় এবং সেগুলি 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()-ও Cloud Storage-এ Uint8Array আপলোড করতে পারবে।

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 উপলভ্য না থাকলে, আপনি uploadString() পদ্ধতি ব্যবহার করে Cloud Storage-এ র, base64, base64url বা data_url এনকোড করা স্ট্রিং আপলোড করতে পারবেন।

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 এই টাস্কটি হল একটি স্ন্যাপশট, যা টাস্কটি `পজ`, `আবার চালু` বা `বাতিল` করতে ব্যবহার করা যেতে পারে।
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);
    });
  }
);

সমস্যা ম্যানেজ করা

আপলোড করার সময় বিভিন্ন কারণে সমস্যা হতে পারে, যেমন লোকাল ফাইল না থাকা অথবা ব্যবহারকারীর কাছে কাঙ্খিত ফাইল আপলোড করার অনুমতি না থাকা। সমস্যা সম্পর্কে আরও তথ্য ডকুমেন্টের সমস্যা ম্যানেজ করুন বিভাগে পাওয়া যাবে।

সম্পূর্ণ উদাহরণ

প্রগ্রেস মনিটর করা ও সমস্যা সমাধান করার সাথে আপলোড করার সম্পূর্ণ উদাহরণ নিচে দেখানো হল:

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 থেকে সেগুলি কীভাবে ডাউনলোড করবেন তা জেনে নিন।