یک مرجع Cloud Storage در پلتفرم های اپل ایجاد کنید

فایل های شما در یک سطل ذخیره سازی ابری ذخیره می شوند. فایل های این سطل در یک ساختار سلسله مراتبی ارائه می شوند، درست مانند سیستم فایل روی هارد دیسک محلی شما یا داده های پایگاه داده بیدرنگ Firebase. با ایجاد یک مرجع به یک فایل، برنامه شما به آن دسترسی پیدا می کند. سپس می توان از این مراجع برای آپلود یا دانلود داده ها، دریافت یا به روز رسانی ابرداده یا حذف فایل استفاده کرد. یک مرجع می تواند به یک فایل خاص یا به یک گره سطح بالاتر در سلسله مراتب اشاره کند.

اگر از پایگاه داده بیدرنگ Firebase استفاده کرده اید، این مسیرها برای شما بسیار آشنا به نظر می رسند. با این حال، داده های فایل شما در Cloud Storage ذخیره می شود، نه در پایگاه داده بیدرنگ.

یک مرجع ایجاد کنید

یک مرجع برای آپلود، دانلود یا حذف یک فایل یا دریافت یا به روز رسانی ابرداده آن ایجاد کنید. یک مرجع را می توان به عنوان یک اشاره گر به یک فایل در فضای ابری در نظر گرفت. منابع سبک وزن هستند، بنابراین می توانید هر تعداد که نیاز دارید ایجاد کنید. آنها همچنین برای چندین عملیات قابل استفاده مجدد هستند.

مراجع با استفاده از سرویس 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()
    

هدف-C

// 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)
    

هدف-C

// 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()
    

هدف-C

// 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()
    

هدف-C

// 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 بررسی کنید. این ویژگی ها مسیر کامل، نام و سطل فایل را دریافت می کنند.

سریع

// 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
    

هدف-C

// 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;
    

محدودیت در مراجع

مسیرهای مرجع و نام‌ها می‌توانند حاوی هر دنباله‌ای از کاراکترهای معتبر یونیکد باشند، اما محدودیت‌های خاصی اعمال می‌شود از جمله:

  1. طول کل reference.fullPath باید بین 1 تا 1024 بایت باشد که UTF-8 کدگذاری شود.
  2. بدون کاراکترهای Carriage Return یا Line Feed.
  3. از استفاده از # , [ , ] , * یا ? ، زیرا این ابزارها با ابزارهای دیگر مانند پایگاه داده بیدرنگ Firebase یا 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()
    

هدف-C

// 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];
    

در مرحله بعد، بیایید نحوه آپلود فایل ها در فضای ذخیره سازی ابری را بیاموزیم.