فایلهای شما در یک مخزن Cloud Storage Bucket) ذخیره میشوند. فایلهای موجود در این مخزن، درست مانند سیستم فایل روی هارد دیسک محلی شما یا دادههای موجود در Firebase Realtime Database ، در یک ساختار سلسله مراتبی ارائه میشوند. با ایجاد یک ارجاع به یک فایل، برنامه شما به آن دسترسی پیدا میکند. سپس میتوان از این ارجاعها برای آپلود یا دانلود دادهها، دریافت یا بهروزرسانی فراداده یا حذف فایل استفاده کرد. یک ارجاع میتواند به یک فایل خاص یا به یک گره سطح بالاتر در سلسله مراتب اشاره کند.
اگر از Firebase Realtime Database استفاده کرده باشید، این مسیرها باید برای شما بسیار آشنا به نظر برسند. با این حال، دادههای فایل شما در Cloud Storage ذخیره میشوند، نه در Realtime Database .
ایجاد یک مرجع
برای آپلود، دانلود یا حذف یک فایل، یا برای دریافت یا بهروزرسانی فرادادههای آن، یک مرجع ایجاد کنید. یک مرجع را میتوان به عنوان یک اشارهگر به یک فایل در ابر در نظر گرفت. مراجع سبک هستند، بنابراین میتوانید هر تعداد که نیاز دارید ایجاد کنید. همچنین آنها برای چندین عملیات قابل استفاده مجدد هستند.
ارجاعات از سرویس storage در برنامه Firebase شما با فراخوانی متد GetReferenceFromUrl() و ارسال یک URL به شکل gs://<your-cloud-storage-bucket> ایجاد میشوند. میتوانید این URL را در بخش Storage کنسول 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 ، ارجاعات را بررسی کنید تا فایلهایی را که به آنها اشاره میکنند، بهتر بشناسید. این متدها مسیر کامل، نام و 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();
محدودیتهای مربوط به منابع
مسیرها و نامهای مرجع میتوانند شامل هر دنباله از کاراکترهای معتبر یونیکد باشند، اما محدودیتهای خاصی اعمال میشود، از جمله:
- طول کل reference.fullPath هنگام کدگذاری UTF-8 باید بین ۱ تا ۱۰۲۴ بایت باشد.
- بدون کاراکترهای بازگشت به خط یا تغذیه خط.
- از استفاده از
#،[،]،*یا?خودداری کنید، زیرا این علائم با ابزارهای دیگری مانند 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 را بررسی خواهیم کرد.