از ابرداده فایل با Cloud Storage برای C++ استفاده کنید
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
پس از آپلود فایل در مرجع Cloud Storage ، همچنین می توانید متادیتای فایل را دریافت و به روز کنید، به عنوان مثال برای به روز رسانی نوع محتوا. فایلها همچنین میتوانند جفتهای کلید/مقدار سفارشی را با فراداده فایل اضافی ذخیره کنند.
فراداده فایل را دریافت کنید
فراداده فایل حاوی ویژگیهای رایجی مانند name ، size ، و content_type (اغلب به عنوان نوع MIME نامیده میشود) علاوه بر برخی موارد کمتر رایج مانند content_disposition و time_created . این ابرداده را می توان با استفاده از روش GetMetadata از یک مرجع Cloud Storage بازیابی کرد.
// Create reference to the file whose metadata we want to retrieveStorageReferenceforest_ref=storage_ref.Child("images/forest.jpg");// Get metadata propertiesFuturefuture=forest_ref.GetMetadata();// Wait for Future to complete...if(future.Error()!=firebase::storage::kErrorNone){// Uh-oh, an error occurred!}else{// We can now retrieve the metadata for 'images/forest.jpg'Metadata*metadata=future.Result();}
به روز رسانی متادیتا فایل
با استفاده از روش UpdateMetadata میتوانید در هر زمانی پس از اتمام آپلود فایل، فراداده فایل را بهروزرسانی کنید. برای اطلاعات بیشتر در مورد ویژگی هایی که می توانند به روز شوند به لیست کامل مراجعه کنید. فقط ویژگیهای مشخصشده در فراداده بهروزرسانی میشوند، بقیه خصوصیات بدون تغییر باقی میمانند.
// Create reference to the file whose metadata we want to changefirebase::storage::StorageReferenceforest_ref=storage_ref.child("images/forest.jpg");// Create file metadata to updateMetadatanew_metadata;newMetadata.set_cache_control("public,max-age=300");newMetadata.set_content_type("image/jpeg");// Update metadata propertiesFuturefuture=forest_ref.UpdateMetadata(new_metadata);// Wait for Future to complete...if(future.Error()!=firebase::storage::kErrorNone){// Uh-oh, an error occurred!}else{// We can now retrieve the updated metadata for 'images/forest.jpg'Metadata*metadata=future.Result();}
میتوانید ویژگیهای فراداده قابل نوشتن را با عبور دادن رشته خالی حذف کنید:
// Create file metadata with property to deleteStorageMetadatanew_metadata;new_metadata.set_content_type("");// Delete the metadata propertyFuturefuture=forest_ref.UpdateMetadata(new_metadata);// Wait for Future to complete...if(future.Error()!=0){// Uh-oh, an error occurred!}else{// metadata.content_type() should be an empty stringMetadata*metadata=future.Result();}
رسیدگی به خطاها
دلایل متعددی وجود دارد که ممکن است هنگام دریافت یا بهروزرسانی فراداده، از جمله فایل موجود نباشد یا کاربر اجازه دسترسی به فایل مورد نظر را نداشته باشد، خطا رخ دهد. اطلاعات بیشتر در مورد خطاها را می توانید در بخش Handle Errors در اسناد پیدا کنید.
فراداده سفارشی
شما می توانید متادیتای سفارشی را به عنوان std::map حاوی ویژگی های std::string تعیین کنید.
std::map<std::string,std::string>*custom_metadata=metadata.custom_metadata();custom_metadata->insert(std::make_pair("location","Yosemite, CA, USA");custom_metadata->insert(std::make_pair("activity","Hiking");
شما می توانید داده های خاص برنامه را برای هر فایل در ابرداده های سفارشی ذخیره کنید، اما ما به شدت توصیه می کنیم از یک پایگاه داده (مانند Firebase Realtime Database ) برای ذخیره و همگام سازی این نوع داده ها استفاده کنید.
ویژگی های فراداده فایل
لیست کاملی از ویژگی های ابرداده در یک فایل در زیر موجود است:
اموال
تایپ کنید
قابل نوشتن
bucket
const char*
نه
generation
const char*
نه
metageneration
const char*
نه
full_path
const char*
نه
name
const char*
نه
size
int64_t
نه
time_created
int64_t
نه
updated
int64_t
نه
cache_control
const char*
بله
content_disposition
const char*
بله
content_encoding
const char*
بله
content_language
const char*
بله
content_type
const char*
بله
download_urls
std:: vector<std::string>
نه
custom_metadata
std::map<std::string، std::string>
بله
مراحل بعدی
آپلود، دانلود و بهروزرسانی فایلها مهم است، اما حذف آنها نیز مهم است. بیایید یاد بگیریم که چگونه فایلها را از Cloud Storage حذف کنیم.
تاریخ آخرین بهروزرسانی 2025-09-06 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2025-09-06 بهوقت ساعت هماهنگ جهانی."],[],[],null,["\u003cbr /\u003e\n\nAfter uploading a file to Cloud Storage reference, you can also get\nand update the file metadata, for example to update the content type. Files\ncan also store custom key/value pairs with additional file metadata.\n| **Note:** By default, a Cloud Storage for Firebase bucket requires Firebase Authentication to perform any action on the bucket's data or files. You can change your Firebase Security Rules for Cloud Storage to [allow unauthenticated access for specific situations](/docs/storage/security/rules-conditions#public). However, for most situations, we strongly recommend [restricting access and setting up robust security rules](/docs/storage/security/get-started) (especially for production apps). Note that if you use Google App Engine and have a default Cloud Storage bucket with a name format of `*.appspot.com`, you may need to consider [how your security rules impact access to App Engine files](/docs/storage/gcp-integration#security-rules-and-app-engine-files).\n\nGet File Metadata\n\nFile metadata contains common properties such as `name`, `size`, and\n`content_type` (often referred to as MIME type) in addition to some less common\nones like `content_disposition` and `time_created`. This metadata can be\nretrieved from a Cloud Storage reference using the `GetMetadata`\nmethod. \n\n```c++\n// Create reference to the file whose metadata we want to retrieve\nStorageReference forest_ref = storage_ref.Child(\"images/forest.jpg\");\n\n// Get metadata properties\nFuture future = forest_ref.GetMetadata();\n\n// Wait for Future to complete...\n\nif (future.Error() != firebase::storage::kErrorNone) {\n // Uh-oh, an error occurred!\n} else {\n // We can now retrieve the metadata for 'images/forest.jpg'\n Metadata* metadata = future.Result();\n}\n```\n\nUpdate File Metadata\n\nYou can update file metadata at any time after the file upload completes by\nusing the `UpdateMetadata` method. Refer to the\n[full list](#file_metadata_properties) for more information on what properties\ncan be updated. Only the properties specified in the metadata are updated,\nall others are left unmodified. \n\n```c++\n// Create reference to the file whose metadata we want to change\nfirebase::storage::StorageReference forest_ref = storage_ref.child(\"images/forest.jpg\");\n\n// Create file metadata to update\nMetadata new_metadata;\nnewMetadata.set_cache_control(\"public,max-age=300\");\nnewMetadata.set_content_type(\"image/jpeg\");\n\n// Update metadata properties\nFuture future = forest_ref.UpdateMetadata(new_metadata);\n\n// Wait for Future to complete...\n\nif (future.Error() != firebase::storage::kErrorNone) {\n // Uh-oh, an error occurred!\n} else {\n // We can now retrieve the updated metadata for 'images/forest.jpg'\n Metadata* metadata = future.Result();\n}\n```\n\nYou can delete writable metadata properties by passing the empty string: \n\n```c++\n// Create file metadata with property to delete\nStorageMetadata new_metadata;\nnew_metadata.set_content_type(\"\");\n\n// Delete the metadata property\nFuture future = forest_ref.UpdateMetadata(new_metadata);\n\n// Wait for Future to complete...\n\nif (future.Error() != 0) {\n // Uh-oh, an error occurred!\n} else {\n // metadata.content_type() should be an empty string\n Metadata* metadata = future.Result();\n}\n```\n\nHandle Errors\n\nThere are a number of reasons why errors may occur on getting or updating\nmetadata, including the file not existing, or the user not having permission\nto access the desired file. More information on errors can be found in the\n[Handle Errors](/docs/storage/cpp/handle-errors)\nsection of the docs.\n\nCustom Metadata\n\nYou can specify custom metadata as an `std::map` containing `std::string`\nproperties. \n\n```c++\nstd::map\u003cstd::string, std::string\u003e* custom_metadata = metadata.custom_metadata();\ncustom_metadata-\u003einsert(std::make_pair(\"location\", \"Yosemite, CA, USA\");\ncustom_metadata-\u003einsert(std::make_pair(\"activity\", \"Hiking\");\n```\n\nYou can store app-specific data for each file in custom metadata, but we highly\nrecommend using a database (such as the\n[Firebase Realtime Database](/docs/database)) to store and synchronize this type of\ndata.\n\nFile Metadata Properties\n\nA full list of metadata properties on a file is available below:\n\n| Property | Type | Writable |\n|-----------------------|--------------------------------------|----------|\n| `bucket` | const char\\* | NO |\n| `generation` | const char\\* | NO |\n| `metageneration` | const char\\* | NO |\n| `full_path` | const char\\* | NO |\n| `name` | const char\\* | NO |\n| `size` | int64_t | NO |\n| `time_created` | int64_t | NO |\n| `updated` | int64_t | NO |\n| `cache_control` | const char\\* | YES |\n| `content_disposition` | const char\\* | YES |\n| `content_encoding` | const char\\* | YES |\n| `content_language` | const char\\* | YES |\n| `content_type` | const char\\* | YES |\n| `download_urls` | std::vector\\\u003cstd::string\\\u003e | NO |\n| `custom_metadata` | std::map\\\u003cstd::string, std::string\\\u003e | YES |\n\nNext Steps\n\nUploading, downloading, and updating files is important, but so is being able\nto remove them. Let's learn how to\n[delete files](/docs/storage/cpp/delete-files)\nfrom Cloud Storage."]]