فایلهایتان در باکت Cloud Storage ذخیره شده است. فایلهای موجود در این سطل در ساختاری سلسلهمراتبی ارائه میشوند، درست مانند سیستم فایل در دیسک سخت محلی شما یا دادههای موجود در Firebase Realtime Database. با ایجاد ارجاع به فایل، برنامهتان به آن دسترسی پیدا میکند. سپس میتوان از این مراجع برای بارگذاری یا بارگیری دادهها، دریافت یا بهروزرسانی فراداده، یا حذف فایل استفاده کرد. مرجع میتواند به فایل خاصی یا به گره سطح بالاتری در سلسلهمراتب اشاره کند.
اگر از Firebase Realtime Database استفاده کرده باشید، این مسیرها باید برایتان بسیار آشنا باشد. بااینحال، دادههای فایل شما در Cloud Storage ذخیره میشود، نه در Realtime Database.
ایجاد مرجع
مرجعی برای بارگذاری، بارگیری، یا حذف فایل، یا دریافت یا بهروزرسانی فراداده آن ایجاد کنید. مرجع را میتوان بهعنوان اشارهگری به فایل در فضای ابری درنظر گرفت. مرجعها سبک هستند، بنابراین میتوانید هر تعداد که نیاز دارید ایجاد کنید. همچنین برای چندین عملیات قابلاستفاده مجدد هستند.
مرجعها از سرویس Firebase.Storage.FirebaseStorage در برنامه Firebase شما با فراخوانی روش GetReferenceFromUrl() و ارسال نشانی وب فرم gs://<your-cloud-storage-bucket> ایجاد میشوند. میتوانید این نشانی وب را در
بخش فضای ذخیرهسازی در کنسول Firebase پیدا کنید.
// Get a reference to the storage service, using the default Firebase App FirebaseStorage storage = FirebaseStorage.DefaultInstance; // Create a storage reference from our storage service StorageReference storageRef = storage.GetReferenceFromUrl("gs://<your-cloud-storage-bucket>");
میتوانید بااستفاده از روش child در مرجع موجود، مرجعی برای مکان پایینتر در درخت ایجاد کنید، برای مثال
'images/space.jpg'.
// Create a child reference // imagesRef now points to "images" StorageReference imagesRef = storageRef.Child("images"); // Child references can also take paths delimited by '/' such as: // "images/space.jpg". StorageReference spaceRef = imagesRef.Child("space.jpg"); // spaceRef now points to "images/space.jpg" // imagesRef still points to "images" // This is equivalent to creating the full referenced StorageReference spaceRefFull = storage.GetReferenceFromUrl( "gs://<your-cloud-storage-bucket>/images/space.jpg");
پیمایش با مراجع
همچنین میتوانید از روشهای Parent و Root برای پیمایش به بالا در سلسلهمراتب فایل ما استفاده کنید. Parent یک سطح به بالا پیمایش میکند، درحالیکه Root تا بالاترین سطح پیمایش میکند.
// Parent allows us to move to the parent of a reference // imagesRef now points to 'images' StorageReference imagesRef = spaceRef.Parent; // Root allows us to move all the way back to the top of our bucket // rootRef now points to the root StorageReference rootRef = spaceRef.Root;
Child، Parent، و Root را میتوان چندینبار بههم زنجیر کرد، زیرا هرکدام یک مرجع برمیگردانند. استثنا Parent از Root است که
StorageReference نامعتبری است.
// References can be chained together multiple times // earthRef points to "images/earth.jpg" StorageReference earthRef = spaceRef.Parent.Child("earth.jpg"); // nullRef is null since the parent of root is an invalid StorageReference StorageReference nullRef = spaceRef.Root.Parent;
روشهای مرجع
میتوانید بااستفاده از ویژگیهای Path، Name، و Bucket، مراجع را بررسی کنید تا فایلهایی را که به آنها اشاره میکنند بهتر درک کنید. این ویژگیها مسیر کامل، نام، و سطل فایل را دریافت میکنند.
// Reference's path is: "images/space.jpg" // This is analogous to a file path on disk string path = spaceRef.Path; // Reference's name is the last segment of the full path: "space.jpg" // This is analogous to the file name string name = spaceRef.Name; // Reference's bucket is the name of the storage bucket where files are stored string bucket = spaceRef.Bucket;
محدودیتهای مرجعها
مسیرها و نامهای مرجع میتوانند شامل هر توالی از نویسههای معتبر Unicode باشند، اما محدودیتهای خاصی اعمال میشود، ازجمله:
- طول کل
reference.Pathباید بین ۱ تا ۱۰۲۴ بایت با کدبندی UTF-8 باشد. - بدون نویسههای «بازگشت نورد» یا «تعویض خط».
- از
#،[،]،*، یا?استفاده نکنید، زیرا این موارد با ابزارهای دیگر مانند Firebase Realtime Database یا gsutil بهخوبی کار نمیکنند.
نمونه کامل
FirebaseStorage storage = FirebaseStorage.DefaultInstance; // Points to the root reference StorageReference storageRef = storage.GetReferenceFromUrl("gs://<your-bucket-name>"); // Points to "images" StorageReference imagesRef = storageRef.Child("images"); // Points to "images/space.jpg" // Note that you can use variables to create child values string filename = "space.jpg"; StorageReference spaceRef = imagesRef.Child(filename); // File path is "images/space.jpg" string path = spaceRef.Path; // File name is "space.jpg" string name = spaceRef.Name; // Points to "images" StorageReference imagesRef = spaceRef.Parent;
مراحل بعدی
در ادامه، بیایید با نحوه بارگذاری فایل در Cloud Storage آشنا شویم.