فایلهای شما در یک مخزن Cloud Storage Bucket) ذخیره میشوند. فایلهای موجود در این مخزن، درست مانند سیستم فایل روی هارد دیسک محلی شما یا دادههای موجود در Firebase Realtime Database ، در یک ساختار سلسله مراتبی ارائه میشوند. با ایجاد یک ارجاع به یک فایل، برنامه شما به آن دسترسی پیدا میکند. سپس میتوان از این ارجاعها برای آپلود یا دانلود دادهها، دریافت یا بهروزرسانی فراداده یا حذف فایل استفاده کرد. یک ارجاع میتواند به یک فایل خاص یا به یک گره سطح بالاتر در سلسله مراتب اشاره کند.
اگر از Firebase Realtime Database استفاده کرده باشید، این مسیرها باید برای شما بسیار آشنا به نظر برسند. با این حال، دادههای فایل شما در Cloud Storage ذخیره میشوند، نه در Realtime Database .
ایجاد یک مرجع
برای آپلود، دانلود یا حذف یک فایل، یا برای دریافت یا بهروزرسانی فرادادههای آن، یک مرجع ایجاد کنید. یک مرجع را میتوان به عنوان یک اشارهگر به یک فایل در ابر در نظر گرفت. مراجع سبک هستند، بنابراین میتوانید هر تعداد که نیاز دارید ایجاد کنید. همچنین آنها برای چندین عملیات قابل استفاده مجدد هستند.
ارجاعات از سرویس Firebase.Storage.FirebaseStorage در برنامه Firebase شما با فراخوانی متد GetReferenceFromUrl() و ارسال یک URL به شکل gs://<your-cloud-storage-bucket> ایجاد میشوند. میتوانید این URL را در بخش Storage کنسول 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 بهتر درک کنید. این ویژگیها مسیر کامل، نام و 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;
محدودیتهای مربوط به منابع
مسیرها و نامهای مرجع میتوانند شامل هر دنباله از کاراکترهای معتبر یونیکد باشند، اما محدودیتهای خاصی اعمال میشود، از جمله:
- طول کل
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 را بررسی خواهیم کرد.