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

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

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 টাইপে থাকে। ফাইল মেটাডেটা সম্পর্কে আরও তথ্য Use File Metadata বিভাগে পাওয়া যাবে।

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