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

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

ফাইল আপলোড

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

Web modular API

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 namespaced API

// 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-এর মাধ্যমে ফাইল নেয় এবং ক্লাউড স্টোরেজে আপলোড করে।

Web modular API

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 namespaced API

// 'file' comes from the Blob or File API
ref.put(file).then((snapshot) => {
  console.log('Uploaded a blob or file!');
});

একটি বাইট অ্যারে থেকে আপলোড করুন

File এবং Blob প্রকারগুলি ছাড়াও, uploadBytes() ক্লাউড স্টোরেজে একটি Uint8Array আপলোড করতে পারে।

Web modular API

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 namespaced API

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 উপলব্ধ না হয়, তাহলে আপনি ক্লাউড স্টোরেজে একটি কাঁচা, base64 , base64url বা data_url এনকোড করা স্ট্রিং আপলোড করতে uploadString() পদ্ধতি ব্যবহার করতে পারেন৷

Web modular API

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 namespaced API

// 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 প্রকার হিসাবে উল্লেখ করা হয়)। ক্লাউড স্টোরেজ স্বয়ংক্রিয়ভাবে ফাইল এক্সটেনশন থেকে বিষয়বস্তুর ধরন অনুমান করে যেখানে ফাইলটি ডিস্কে সংরক্ষণ করা হয়, কিন্তু আপনি যদি মেটাডেটাতে একটি contentType উল্লেখ করেন তবে এটি স্বয়ংক্রিয়ভাবে সনাক্ত হওয়া প্রকারকে ওভাররাইড করবে। যদি কোনো contentType মেটাডেটা নির্দিষ্ট করা না থাকে এবং ফাইলের কোনো ফাইল এক্সটেনশন না থাকে, তাহলে ক্লাউড স্টোরেজ টাইপ application/octet-stream ডিফল্ট হয়। ফাইল মেটাডেটা সম্পর্কে আরও তথ্য পাওয়া যাবে ফাইল মেটাডেটা ব্যবহার করুন বিভাগে।

Web modular API

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 namespaced API

// 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 modular API

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 namespaced API

// 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 modular API

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 namespaced API

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 modular API

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 namespaced API

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

এখন যেহেতু আপনি ফাইলগুলি আপলোড করেছেন, আসুন শিখি কিভাবে সেগুলিকে ক্লাউড স্টোরেজ থেকে ডাউনলোড করতে হয়