فایلهای شما در یک مخزن Cloud Storage Bucket) ذخیره میشوند. فایلهای موجود در این مخزن، درست مانند سیستم فایل روی هارد دیسک محلی شما یا دادههای موجود در Firebase Realtime Database ، در یک ساختار سلسله مراتبی ارائه میشوند. با ایجاد یک ارجاع به یک فایل، برنامه شما به آن دسترسی پیدا میکند. سپس میتوان از این ارجاعها برای آپلود یا دانلود دادهها، دریافت یا بهروزرسانی فراداده یا حذف فایل استفاده کرد. یک ارجاع میتواند به یک فایل خاص یا به یک گره سطح بالاتر در سلسله مراتب اشاره کند.
اگر از Firebase Realtime Database استفاده کرده باشید، این مسیرها باید برای شما بسیار آشنا به نظر برسند. با این حال، دادههای فایل شما در Cloud Storage ذخیره میشوند، نه در Realtime Database .
ایجاد یک مرجع
برای آپلود، دانلود یا حذف یک فایل، یا برای دریافت یا بهروزرسانی فرادادههای آن، یک مرجع ایجاد کنید. یک مرجع را میتوان به عنوان یک اشارهگر به یک فایل در ابر در نظر گرفت. مراجع سبک هستند، بنابراین میتوانید هر تعداد که نیاز دارید ایجاد کنید. همچنین آنها برای چندین عملیات قابل استفاده مجدد هستند.
با استفاده از نمونهی تکلایه FirebaseStorage
و فراخوانی متد getReference()
آن، یک مرجع ایجاد کنید.
Kotlin
// 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
// 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
// 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
// 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
// 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();
محدودیتهای مربوط به منابع
مسیرها و نامهای مرجع میتوانند شامل هر دنباله از کاراکترهای معتبر یونیکد باشند، اما محدودیتهای خاصی اعمال میشود، از جمله:
- طول کل reference.fullPath هنگام کدگذاری UTF-8 باید بین ۱ تا ۱۰۲۴ بایت باشد.
- بدون کاراکترهای بازگشت به خط یا تغذیه خط.
- از استفاده از
#
،[
،]
،*
یا?
خودداری کنید، زیرا این علائم با ابزارهای دیگری مانند Firebase Realtime Database یا gsutil به خوبی کار نمیکنند.
مثال کامل
Kotlin
// 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();
در ادامه، نحوه آپلود فایلها در Cloud Storage را بررسی خواهیم کرد.