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

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

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

ایجاد مرجع

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

مرجع‌ها از سرویس 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 our 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 allows us to move to the parent of a reference
// images_ref now points to 'images'
StorageReference images_ref = space_ref.Parent();

// Root allows us to 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 آشنا شویم.