فایلهای شما در یک مخزن Cloud Storage Bucket) ذخیره میشوند. فایلهای موجود در این مخزن، درست مانند سیستم فایل روی هارد دیسک محلی شما یا دادههای موجود در Firebase Realtime Database ، در یک ساختار سلسله مراتبی ارائه میشوند. با ایجاد یک ارجاع به یک فایل، برنامه شما به آن دسترسی پیدا میکند. سپس میتوان از این ارجاعها برای آپلود یا دانلود دادهها، دریافت یا بهروزرسانی فراداده یا حذف فایل استفاده کرد. یک ارجاع میتواند به یک فایل خاص یا به یک گره سطح بالاتر در سلسله مراتب اشاره کند.
اگر از Firebase Realtime Database استفاده کرده باشید، این مسیرها باید برای شما بسیار آشنا به نظر برسند. با این حال، دادههای فایل شما در Cloud Storage ذخیره میشوند، نه در Realtime Database .
ایجاد یک مرجع
برای آپلود، دانلود یا حذف یک فایل، یا برای دریافت یا بهروزرسانی فرادادههای آن، یک مرجع ایجاد کنید. یک مرجع را میتوان به عنوان یک اشارهگر به یک فایل در ابر در نظر گرفت. مراجع سبک هستند، بنابراین میتوانید هر تعداد که نیاز دارید ایجاد کنید. همچنین آنها برای چندین عملیات قابل استفاده مجدد هستند.
ارجاعات با استفاده از سرویس FirebaseStorage
و فراخوانی متد reference
آن ایجاد میشوند.
سویفت
// Get a reference to the storage service using the default Firebase App let storage = Storage.storage() // Create a storage reference from our storage service let storageRef = storage.reference()
هدف-سی
// Get a reference to the storage service using the default Firebase App FIRStorage *storage = [FIRStorage storage]; // Create a storage reference from our storage service FIRStorageReference *storageRef = [storage reference];
شما میتوانید با استفاده از متد child
روی یک مرجع موجود، به مکانی پایینتر در درخت، مثلاً 'images/space.jpg'
، ارجاع دهید.
سویفت
// Create a child reference // imagesRef now points to "images" let imagesRef = storageRef.child("images") // Child references can also take paths delimited by '/' // spaceRef now points to "images/space.jpg" // imagesRef still points to "images" var spaceRef = storageRef.child("images/space.jpg") // This is equivalent to creating the full reference let storagePath = "\(your_firebase_storage_bucket)/images/space.jpg" spaceRef = storage.reference(forURL: storagePath)
هدف-سی
// Create a child reference // imagesRef now points to "images" FIRStorageReference *imagesRef = [storageRef child:@"images"]; // Child references can also take paths delimited by '/' // spaceRef now points to "images/space.jpg" // imagesRef still points to "images" FIRStorageReference *spaceRef = [storageRef child:@"images/space.jpg"]; // This is equivalent to creating the full reference spaceRef = [storage referenceForURL:@"gs://<your-firebase-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' let imagesRef = spaceRef.parent() // Root allows us to move all the way back to the top of our bucket // rootRef now points to the root let rootRef = spaceRef.root()
هدف-سی
// Parent allows us to move to the parent of a reference // 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 FIRStorageReference *rootRef = [spaceRef root];
child
، parent
و root
میتوانند چندین بار به هم زنجیر شوند، زیرا هر کدام یک ارجاع برمیگردانند. استثنا، parent
root
است که nil
است.
سویفت
// References can be chained together multiple times // earthRef points to "images/earth.jpg" let earthRef = spaceRef.parent()?.child("earth.jpg") // nilRef is nil, since the parent of root is nil let nilRef = spaceRef.root().parent()
هدف-سی
// References can be chained together multiple times // earthRef points to "images/earth.jpg" FIRStorageReference *earthRef = [[spaceRef parent] child:@"earth.jpg"]; // nilRef is nil, since the parent of root is nil FIRStorageReference *nilRef = [[spaceRef root] parent];
ویژگیهای مرجع
شما میتوانید ارجاعها را بررسی کنید تا فایلهایی را که به آنها اشاره میکنند با استفاده از ویژگیهای fullPath
، name
و bucket
بهتر درک کنید. این ویژگیها مسیر کامل، نام و bucket فایل را دریافت میکنند.
سویفت
// Reference's path is: "images/space.jpg" // This is analogous to a file path on disk spaceRef.fullPath // 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 where files are stored spaceRef.bucket
هدف-سی
// Reference's path is: "images/space.jpg" // This is analogous to a file path on disk spaceRef.fullPath; // 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 where files are stored spaceRef.bucket;
محدودیتهای مربوط به منابع
مسیرها و نامهای مرجع میتوانند شامل هر دنباله از کاراکترهای معتبر یونیکد باشند، اما محدودیتهای خاصی اعمال میشود، از جمله:
- طول کل reference.fullPath هنگام کدگذاری UTF-8 باید بین ۱ تا ۱۰۲۴ بایت باشد.
- بدون کاراکترهای بازگشت به خط یا تغذیه خط.
- از استفاده از
#
،[
،]
،*
یا?
خودداری کنید، زیرا این علائم با ابزارهای دیگری مانند Firebase Realtime Database یا gsutil به خوبی کار نمیکنند.
مثال کامل
سویفت
// Points to the root reference let storageRef = Storage.storage().reference() // Points to "images" let imagesRef = storageRef.child("images") // Points to "images/space.jpg" // Note that you can use variables to create child values let fileName = "space.jpg" let spaceRef = imagesRef.child(fileName) // File path is "images/space.jpg" let path = spaceRef.fullPath // File name is "space.jpg" let name = spaceRef.name // Points to "images" let images = spaceRef.parent()
هدف-سی
// Points to the root reference FIRStorageReference *storageRef = [[FIRStorage storage] reference]; // Points to "images" FIRStorageReference *imagesRef = [storageRef child:@"images"]; // Points to "images/space.jpg" // Note that you can use variables to create child values NSString *fileName = @"space.jpg"; FIRStorageReference *spaceRef = [imagesRef child:fileName]; // File path is "images/space.jpg" NSString *path = spaceRef.fullPath; // File name is "space.jpg" NSString *name = spaceRef.name; // Points to "images" imagesRef = [spaceRef parent];
در ادامه، نحوه آپلود فایلها در Cloud Storage را بررسی خواهیم کرد.