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

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

আপনি যদি ফায়ারবেস রিয়েলটাইম ডেটাবেস ব্যবহার করে থাকেন তবে এই পথগুলি আপনার কাছে খুব পরিচিত বলে মনে হবে। যাইহোক, আপনার ফাইল ডেটা ক্লাউড স্টোরেজে সংরক্ষণ করা হয়, রিয়েলটাইম ডেটাবেসে নয়

একটি রেফারেন্স তৈরি করুন

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

FirebaseStorage singleton উদাহরণ ব্যবহার করে একটি রেফারেন্স তৈরি করুন এবং এর getReference() পদ্ধতিতে কল করুন।

Kotlin+KTX

// Create a storage reference from our app
var storageRef = storage.reference

Java

// Create a storage reference from our app
StorageReference storageRef = storage.getReference();

এর পরে, আপনি গাছের নীচে একটি অবস্থানের একটি রেফারেন্স তৈরি করতে পারেন, একটি বিদ্যমান রেফারেন্সে child() পদ্ধতি ব্যবহার করে "images/space.jpg" বলুন।

Kotlin+KTX

// Create a child reference
// imagesRef now points to "images"
var imagesRef: StorageReference? = storageRef.child("images")

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

Java

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

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

আপনি আমাদের ফাইলের অনুক্রমে নেভিগেট করতে getParent() এবং getRoot() পদ্ধতিগুলিও ব্যবহার করতে পারেন। getParent() একটি স্তরে নেভিগেট করে, যখন getRoot() শীর্ষে সমস্ত পথ নেভিগেট করে।

Kotlin+KTX

// parent allows us to move our reference to a parent node
// imagesRef now points to 'images'
imagesRef = spaceRef.parent

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

Java

// getParent allows us to move our reference to a parent node
// imagesRef now points to 'images'
imagesRef = spaceRef.getParent();

// getRoot allows us to move all the way back to the top of our bucket
// rootRef now points to the root
StorageReference rootRef = spaceRef.getRoot();

child() , getParent() , এবং getRoot() একাধিকবার একসাথে চেইন করা যেতে পারে, কারণ প্রতিটি একটি রেফারেন্স প্রদান করে। কিন্তু getRoot().getParent() কল করলে null ফিরে আসে।

Kotlin+KTX

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

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

Java

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

// nullRef is null, since the parent of root is null
StorageReference nullRef = spaceRef.getRoot().getParent();

রেফারেন্স বৈশিষ্ট্য

getPath() , getName() , এবং getBucket() পদ্ধতি ব্যবহার করে তারা যে ফাইলগুলি নির্দেশ করে তা আরও ভালভাবে বোঝার জন্য আপনি রেফারেন্সগুলি পরীক্ষা করতে পারেন। এই পদ্ধতিগুলি ফাইলের সম্পূর্ণ পথ, নাম এবং বালতি পায়।

Kotlin+KTX

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

// 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 that the files are stored in
spaceRef.bucket

Java

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

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

// Reference's bucket is the name of the storage bucket that the files are stored in
spaceRef.getBucket();

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

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

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

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

Kotlin+KTX

// Points to the root reference
storageRef = storage.reference

// Points to "images"
imagesRef = storageRef.child("images")

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

// File path is "images/space.jpg"
val path = spaceRef.path

// File name is "space.jpg"
val name = spaceRef.name

// Points to "images"
imagesRef = spaceRef.parent

Java

// Points to the root reference
storageRef = storage.getReference();

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

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

// File path is "images/space.jpg"
String path = spaceRef.getPath();

// File name is "space.jpg"
String name = spaceRef.getName();

// Points to "images"
imagesRef = spaceRef.getParent();

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