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

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

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

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

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

مراجع از سرویس storage در برنامه Firebase شما با فراخوانی متد GetReferenceFromUrl() و ارسال یک URL به شکل gs://<your-cloud-storage-bucket> ایجاد می شود. می‌توانید این URL را در بخش فضای ذخیره‌سازی کنسول 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 of 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 باید بین 1 تا 1024 بایت باشد که UTF-8 کدگذاری شود.
  2. بدون کاراکترهای Carriage Return یا Line Feed.
  3. از استفاده از # , [ , ] , * یا ? ، زیرا این ابزارها با ابزارهای دیگر مانند پایگاه داده بیدرنگ Firebase یا 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();

مراحل بعدی

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