ایجاد مرجع 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();

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

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

  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 آشنا شویم.