ایجاد مرجع Cloud Storage با Cloud Storage برای Unity

فایل‌هایتان در باکت Cloud Storage for Firebase ذخیره شده است. فایل‌های موجود در این سبد در ساختاری سلسله‌مراتبی ارائه می‌شوند، درست مانند سیستم فایل در دیسک سخت محلی شما.

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

این صفحه نحوه استفاده از کیت توسعه نرم‌افزار Cloud Storage for Firebase را برای ایجاد و کار با ارجاع‌ها شرح می‌دهد.

ایجاد مرجع

برای انجام عملیات روی فایل، ابتدا باید مرجعی برای فایل ایجاد کنید. مرجع‌ها سبک هستند، بنابراین می‌توانید هر تعداد که نیاز دارید ایجاد کنید. همچنین برای چندین عملیات قابل‌استفاده مجدد هستند.

ارجاع‌ها از سرویس 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 باشند، اما محدودیت‌های خاصی اعمال می‌شود، ازجمله:

  1. طول کل reference.Path باید بین ۱ تا ۱۰۲۴ بایت باشد وقتی‌که با UTF-8 کدگذاری شده باشد.
  2. بدون نویسه‌های «بازگشت نورد» یا «تعویض خط».
  3. از #، [، ]، *، یا ? استفاده نکنید، زیرا این موارد با ابزارهای دیگر مانند 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 آشنا شوید.