یک مرجع Cloud Storage با Cloud Storage for Unity ایجاد کنید

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

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

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

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

مراجع از سرویس Firebase.Storage.FirebaseStorage در برنامه Firebase شما با فراخوانی متد GetReferenceFromUrl() و ارسال یک URL به شکل gs://<your-cloud-storage-bucket> ایجاد می‌شوند. می‌توانید این URL را در بخش فضای ذخیره‌سازی کنسول 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 of 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;

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

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

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

مراحل بعدی

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