فایل های شما در یک سطل ذخیره سازی ابری ذخیره می شوند. فایل های این سطل در یک ساختار سلسله مراتبی ارائه می شوند، درست مانند سیستم فایل روی هارد دیسک محلی شما یا داده های پایگاه داده بیدرنگ Firebase. با ایجاد یک مرجع به یک فایل، برنامه شما به آن دسترسی پیدا می کند. سپس می توان از این مراجع برای آپلود یا دانلود داده ها، دریافت یا به روز رسانی ابرداده یا حذف فایل استفاده کرد. یک مرجع می تواند به یک فایل خاص یا به یک گره سطح بالاتر در سلسله مراتب اشاره کند.
اگر از پایگاه داده بیدرنگ Firebase استفاده کرده اید، این مسیرها برای شما بسیار آشنا به نظر می رسند. با این حال، داده های فایل شما در Cloud Storage ذخیره می شود، نه در پایگاه داده بیدرنگ.
یک مرجع ایجاد کنید
یک مرجع برای آپلود، دانلود یا حذف یک فایل یا دریافت یا به روز رسانی ابرداده آن ایجاد کنید. یک مرجع را می توان به عنوان یک اشاره گر به یک فایل در فضای ابری در نظر گرفت. منابع سبک وزن هستند، بنابراین می توانید هر تعداد که نیاز دارید ایجاد کنید. آنها همچنین برای چندین عملیات قابل استفاده مجدد هستند.
با استفاده از نمونه FirebaseStorage
و فراخوانی getReference()
یک مرجع ایجاد کنید.
Java
// Create a storage reference from our app StorageReference storageRef = storage.getReference();
Kotlin+KTX
// Create a storage reference from our app var storageRef = storage.reference
در مرحله بعد، می توانید با استفاده از متد 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");
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")
با مراجع حرکت کنید
همچنین می توانید از getParent()
و getRoot()
برای پیمایش به بالا در سلسله مراتب فایل ما استفاده کنید. getParent()
یک سطح به سمت بالا حرکت می کند، در حالی که getRoot()
تمام راه را به سمت بالا هدایت می کند.
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();
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
child()
، getParent()
و getRoot()
را می توان چندین بار به هم متصل کرد، زیرا هر کدام یک مرجع را برمی گرداند. اما فراخوانی getRoot().getParent()
null
را برمی گرداند.
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();
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
خواص مرجع
میتوانید مراجع را برای درک بهتر فایلهایی که به آنها اشاره میکنند با استفاده از getPath()
، getName()
و getBucket()
کنید. این روشها مسیر کامل، نام و سطل فایل را دریافت میکنند.
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();
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
محدودیت در مراجع
مسیرهای مرجع و نامها میتوانند حاوی هر دنبالهای از کاراکترهای معتبر یونیکد باشند، اما محدودیتهای خاصی اعمال میشود از جمله:
- طول کل reference.fullPath باید بین 1 تا 1024 بایت در هنگام کدگذاری UTF-8 باشد.
- بدون کاراکترهای Carriage Return یا Line Feed.
- از استفاده از
#
،[
،]
،*
، یا?
، زیرا این ابزارها با ابزارهای دیگر مانند پایگاه داده بیدرنگ Firebase یا gsutil به خوبی کار نمی کنند.
مثال کامل
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();
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
در مرحله بعد، بیایید نحوه آپلود فایل ها در فضای ذخیره سازی ابری را بیاموزیم.