ওয়েবে ক্লাউড স্টোরেজ রেফারেন্স তৈরি করুন

আপনার ফাইল Cloud Storage বাকেটে স্টোর করা হয়। এই বাকেটের ফাইলগুলি একটি হায়ারার্কিক্যাল স্ট্রাকচারে উপস্থাপন করা হয়, ঠিক যেমন আপনার লোকাল হার্ড ডিস্কের ফাইল সিস্টেম বা Firebase Realtime Database-এর ডেটা। কোনও ফাইলের রেফারেন্স তৈরি করার মাধ্যমে, আপনার অ্যাপ সেটির অ্যাক্সেস পায়। এই রেফারেন্সগুলি তারপর ডেটা আপলোড বা ডাউনলোড করতে, মেটাডেটা পেতে বা আপডেট করতে অথবা ফাইলটি মুছতে ব্যবহার করা যেতে পারে। রেফারেন্স কোনও নির্দিষ্ট ফাইল বা শ্রেণিবিন্যাসের উচ্চ স্তরের নোডকে নির্দেশ করতে পারে।

আপনি Firebase Realtime Database ব্যবহার করে থাকলে, এই পাথগুলি আপনার কাছে খুব পরিচিত বলে মনে হবে। তবে, আপনার ফাইল ডেটা Cloud Storage-এ স্টোর করা হয়, Realtime Database-এ নয়।

রেফারেন্স তৈরি করা

ফাইল আপলোড বা ডাউনলোড করতে, ফাইল মুছে দিতে অথবা মেটাডেটা পেতে বা আপডেট করতে, আপনি যে ফাইলের উপর কাজ করতে চান সেটির একটি রেফারেন্স তৈরি করতে হবে। রেফারেন্সকে ক্লাউডে থাকা কোনও ফাইলের পয়েন্টার হিসেবে ভাবা যেতে পারে। রেফারেন্সগুলি লাইটওয়েট, তাই আপনি যতগুলি প্রয়োজন ততগুলি তৈরি করতে পারেন এবং সেগুলি একাধিক অপারেশনের জন্য পুনরায় ব্যবহারযোগ্য।

রেফারেন্স তৈরি করতে, getStorage() ব্যবহার করে Storage পরিষেবার ইনস্ট্যান্স পান এবং তারপর আর্গুমেন্ট হিসেবে পরিষেবার সাথে ref() কল করুন। এই রেফারেন্স আপনার Cloud Storage বাকেটের রুটকে নির্দেশ করে।

Web

import { getStorage, ref } from "firebase/storage";

// Get a reference to the storage service, which is used to create references in your storage bucket
const storage = getStorage();

// Create a storage reference from our storage service
const storageRef = ref(storage);

Web

// Get a reference to the storage service, which is used to create references in your storage bucket
var storage = firebase.storage();

// Create a storage reference from our storage service
var storageRef = storage.ref();

আপনি ট্রি-এর নিচের দিকে কোনও লোকেশনের রেফারেন্স তৈরি করতে পারেন, যেমন ref() কল করার সময় দ্বিতীয় আর্গুমেন্ট হিসেবে এই পাথ পাস করে 'images/space.jpg'।

Web

import { getStorage, ref } from "firebase/storage";

const storage = getStorage();

// Create a child reference
const imagesRef = ref(storage, 'images');
// imagesRef now points to 'images'

// Child references can also take paths delimited by '/'
const spaceRef = ref(storage, 'images/space.jpg');
// spaceRef now points to "images/space.jpg"
// imagesRef still points to "images"

Web

// Create a child reference
var imagesRef = storageRef.child('images');
// imagesRef now points to 'images'

// Child references can also take paths delimited by '/'
var spaceRef = storageRef.child('images/space.jpg');
// spaceRef now points to "images/space.jpg"
// imagesRef still points to "images"

এছাড়াও, আপনি parent এবং root প্রপার্টি ব্যবহার করে ফাইল হায়ারার্কির উপরের দিকে নেভিগেট করতে পারবেন। parent এক লেভেল উপরে নেভিগেট করে, যেখানে root একদম উপরে নেভিগেট করে।

Web

import { getStorage, ref } from "firebase/storage";

const storage = getStorage();
const spaceRef = ref(storage, 'images/space.jpg');

// Parent allows us to move to the parent of a reference
const imagesRef = spaceRef.parent;
// imagesRef now points to 'images'

// Root allows us to move all the way back to the top of our bucket
const rootRef = spaceRef.root;
// rootRef now points to the root

Web

// Parent allows us to move to the parent of a reference
var imagesRef = spaceRef.parent;
// imagesRef now points to 'images'

// Root allows us to move all the way back to the top of our bucket
var rootRef = spaceRef.root;
// rootRef now points to the root

child(), parent এবং root একাধিকবার একসাথে চেইন করা যেতে পারে, কারণ প্রতিটি একটি রেফারেন্স রিটার্ন করে। ব্যতিক্রম হল root-এর parent, যা হল null।

Web

import { getStorage, ref } from "firebase/storage";

const storage = getStorage();
const spaceRef = ref(storage, 'images/space.jpg');

// References can be chained together multiple times
const earthRef = ref(spaceRef.parent, 'earth.jpg');
// earthRef points to 'images/earth.jpg'

// nullRef is null, since the parent of root is null
const nullRef = spaceRef.root.parent;

Web

// References can be chained together multiple times
var earthRef = spaceRef.parent.child('earth.jpg');
// earthRef points to 'images/earth.jpg'

// nullRef is null, since the parent of root is null
var nullRef = spaceRef.root.parent;

রেফারেন্স প্রপার্টি

fullPath, name এবং bucket প্রপার্টি ব্যবহার করে রেফারেন্সগুলি যে ফাইলগুলিকে নির্দেশ করে সেগুলি আরও ভালভাবে বুঝতে আপনি রেফারেন্সগুলি পরীক্ষা করতে পারেন। এইসব প্রপার্টি ফাইলের সম্পূর্ণ পাথ, ফাইলের নাম এবং ফাইলটি যে বাকেটে স্টোর করা আছে সেটি পায়।

Web

import { getStorage, ref } from "firebase/storage";

const storage = getStorage();
const spaceRef = ref(storage, 'images/space.jpg');

// Reference's path is: 'images/space.jpg'
// This is analogous to a file path on disk
spaceRef.fullPath;

// Reference's name is the last segment of the full path: 'space.jpg'
// This is analogous to the file name
spaceRef.name;

// Reference's bucket is the name of the storage bucket where files are stored
spaceRef.bucket;

Web

// Reference's path is: 'images/space.jpg'
// This is analogous to a file path on disk
spaceRef.fullPath;

// Reference's name is the last segment of the full path: 'space.jpg'
// This is analogous to the file name
spaceRef.name;

// Reference's bucket is the name of the storage bucket where files are stored
spaceRef.bucket;

রেফারেন্সের ক্ষেত্রে সীমাবদ্ধতা

রেফারেন্স পাথ ও নামে সঠিক ইউনিকোড অক্ষরের যেকোনও ক্রম থাকতে পারে, তবে কিছু বিধিনিষেধ আরোপ করা হয়, যার মধ্যে রয়েছে:

  1. UTF-8 এনকোড করা হলে reference.fullPath-এর মোট দৈর্ঘ্য ১ থেকে ১০২৪ বাইটের মধ্যে হতে হবে।
  2. কোনও নতুন লাইন বা লাইন ফিড অক্ষর নেই।
  3. #, [, ], * বা ? ব্যবহার করা এড়িয়ে চলুন, কারণ এগুলি Firebase Realtime Database বা gsutil-এর মতো অন্যান্য টুলের সাথে ভালভাবে কাজ করে না।

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

Web

import { getStorage, ref } from "firebase/storage";

const storage = getStorage();

// Points to the root reference
const storageRef = ref(storage);

// Points to 'images'
const imagesRef = ref(storageRef, 'images');

// Points to 'images/space.jpg'
// Note that you can use variables to create child values
const fileName = 'space.jpg';
const spaceRef = ref(imagesRef, fileName);

// File path is 'images/space.jpg'
const path = spaceRef.fullPath;

// File name is 'space.jpg'
const name = spaceRef.name;

// Points to 'images'
const imagesRefAgain = spaceRef.parent;

Web

// Points to the root reference
var storageRef = firebase.storage().ref();

// Points to 'images'
var imagesRef = storageRef.child('images');

// Points to 'images/space.jpg'
// Note that you can use variables to create child values
var fileName = 'space.jpg';
var spaceRef = imagesRef.child(fileName);

// File path is 'images/space.jpg'
var path = spaceRef.fullPath;

// File name is 'space.jpg'
var name = spaceRef.name;

// Points to 'images'
var imagesRef = spaceRef.parent;

এরপর, Cloud Storage-এ কীভাবে ফাইল আপলোড করবেন তা জেনে নিন।