ایجاد مرجع Cloud Storage با Cloud Storage برای C++‎

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

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

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

ایجاد مرجع

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

مرجع‌ها از سرویس storage در برنامه Firebase شما با فراخوانی روش GetReferenceFromUrl() و ارسال نشانی وب فرم gs://<your-cloud-storage-bucket> ایجاد می‌شوند. می‌توانید این نشانی وب را در بخش فضای ذخیره‌سازی در کنسول Firebase پیدا کنید.

// Get a reference to the storage service, using the default Firebase App
Storage* storage = Storage::GetInstance(app);

// Create a Cloud Storage reference from the storage service
StorageReference storage_ref = storage->GetReferenceFromUrl("gs://<your-cloud-storage-bucket>");

می‌توانید بااستفاده از روش child در مرجع موجود، مرجعی برای مکان پایین‌تر در درخت ایجاد کنید، مثلاً 'images/space.jpg'.

// Create a child reference
// images_ref now points to "images"
StorageReference images_ref = storage_ref.Child("images");

// Child references can also take paths delimited by '/'
// space_ref now points to "images/space.jpg"
// images_ref still points to "images"
StorageReference space_ref = storage_ref.Child("images/space.jpg");

// This is equivalent to creating the full reference
StorageReference space_ref = storage.GetReferenceFromUrl("gs://<your-cloud-storage-bucket>/images/space.jpg");

همچنین می‌توانید از روش‌های Parent و Root برای پیمایش به بالا در سلسله‌مراتب فایل خود استفاده کنید. Parent یک سطح به بالا پیمایش می‌کند، درحالی‌که Root تا بالاترین سطح پیمایش می‌کند.

// Parent lets you move to the parent of a reference
// images_ref now points to 'images'
StorageReference images_ref = space_ref.Parent();

// Root lets you move all the way back to the top of our bucket
// root_ref now points to the root
StorageReference root_ref = space_ref.Root();

‫Child،‏ Parent، و Root را می‌توان چندین‌بار به‌هم زنجیر کرد، زیرا هرکدام یک مرجع برمی‌گردانند. استثنا Parent از Root است که StorageReference نامعتبری است.

// References can be chained together multiple times
// earth_ref points to "images/earth.jpg"
StorageReference earth_ref = space_ref.Parent().Child("earth.jpg");

// null_ref is null, since the parent of root is an invalid StorageReference
StorageReference null_ref = space_ref.Root().Parent();

روش‌های مرجع

بااستفاده از روش‌های full_path،‏ name، و bucket می‌توانید ارجاع‌ها را بررسی کنید تا فایل‌هایی را که به آن‌ها اشاره می‌کنند بهتر درک کنید. این روش‌ها مسیر کامل، نام، و سطل فایل را دریافت می‌کنند.

// Reference's path is: "images/space.jpg"
// This is analogous to a file path on disk
space_ref.full_path();

// Reference's name is the last segment of the full path: "space.jpg"
// This is analogous to the file name
space_ref.name();

// Reference's bucket is the name of the Cloud Storage bucket where files are stored
space_ref.bucket();

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

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

  1. طول کل reference.fullPath باید بین ۱ تا ۱۰۲۴ بایت باشد وقتی‌که UTF-8 کدگذاری شده باشد.
  2. بدون نویسه‌های «بازگشت نورد» یا «تعویض خط».
  3. از #، [، ]، *، یا ? استفاده نکنید، زیرا این موارد با ابزارهای دیگر مانند Firebase Realtime Database یا gsutil به‌خوبی کار نمی‌کنند.

نمونه کامل

Storage* storage = Storage::GetInstance(app);

// Points to the root reference
StorageReference storage_ref = storage->GetReferenceFromUrl("gs://<your-bucket-name>");

// Points to "images"
StorageReference images_ref = storage_ref.Child("images");

// Points to "images/space.jpg"
// Note that you can use variables to create child values
std::string filename = "space.jpg";
StorageReference space_ref = images_ref.Child(filename);

// File path is "images/space.jpg"
std::string path = space_ref.full_path()

// File name is "space.jpg"
std::string name = space_ref.name()

// Points to "images"
StorageReference images_ref = space_ref.Parent();

مراحل بعدی

سپس، با نحوه بارگذاری فایل در Cloud Storage آشنا شوید.