// Create reference to the file whose metadata we want to retrieveStorageReferenceforestRef=storageRef.Child("images/forest.jpg");// Get metadata propertiesforestRef.GetMetadataAsync().ContinueWithOnMainThread(task=>{if(!task.IsFaulted && !task.IsCanceled){StorageMetadatameta=task.Result;// do stuff with meta}});
// Create reference to the file whose metadata we want to changeStorageReferenceforestRef=storageRef.Child("images/forest.jpg");// Create file metadata to updatevarnewMetadata=newMetadataChange();newMetadata.CacheControl="public,max-age=300";newMetadata.ContentType="image/jpeg";// Update metadata propertiesforestRef.UpdateMetadataAsync(newMetadata).ContinueWithOnMainThread(task=>{if(!task.IsFaulted && !task.IsCanceled){// access the updated meta dataStorageMetadatameta=task.Result;}});
您可以通过传递空字符串来删除可写元数据属性:
// Create file metadata to updatevarnewMetadata=newMetadataChange();newMetadata.ContentType="";// Update metadata propertiesforestRef.UpdateMetadataAsync(newMetadata).ContinueWithOnMainThread(task=>{if(!task.IsFaulted && !task.IsCanceled){StorageMetadatameta=task.Result;// meta.ContentType should be an empty string now}});
varnewMetadata=newMetadataChange{CustomMetadata=newDictionary<string,string>{{"location","Yosemite, CA, USA"},{"activity","Hiking"}}};// UpdateMetadataAsync
[null,null,["最后更新时间 (UTC):2025-08-12。"],[],[],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`, `SizeBytes`, and\n`ContentType` (often referred to as MIME type) in addition to some\nless common ones like `ContentDisposition` and `CreationTimeMillis`. This\nmetadata can be retrieved from a Cloud Storage reference using the\n`GetMetadataAsync` method. \n\n```c#\n// Create reference to the file whose metadata we want to retrieve\nStorageReference forestRef =\n storageRef.Child(\"images/forest.jpg\");\n\n// Get metadata properties\nforestRef.GetMetadataAsync().ContinueWithOnMainThread(task =\u003e {\n if (!task.IsFaulted && !task.IsCanceled) {\n StorageMetadata meta = task.Result;\n // do stuff with meta\n }\n});\n```\n\nUpdate File Metadata\n\nYou can update file metadata at any time after the file upload completes by\nusing the `UpdateMetadataAsync` method which takes a `MetadataChange` object.\nRefer to the [full list](#file_metadata_properties) for more information on what\nproperties can be updated. Only the properties specified in the metadata are\nupdated, all others are left unmodified. \n\n```c#\n// Create reference to the file whose metadata we want to change\nStorageReference forestRef = storageRef.Child(\"images/forest.jpg\");\n\n// Create file metadata to update\nvar newMetadata = new MetadataChange();\nnewMetadata.CacheControl = \"public,max-age=300\";\nnewMetadata.ContentType = \"image/jpeg\";\n\n// Update metadata properties\nforestRef.UpdateMetadataAsync(newMetadata).ContinueWithOnMainThread(task =\u003e {\n if (!task.IsFaulted && !task.IsCanceled) {\n // access the updated meta data\n StorageMetadata meta = task.Result;\n }\n});\n```\n\nYou can delete writable metadata properties by passing the empty string: \n\n```c#\n// Create file metadata to update\nvar newMetadata = new MetadataChange();\nnewMetadata.ContentType = \"\";\n\n// Update metadata properties\nforestRef.UpdateMetadataAsync(newMetadata).ContinueWithOnMainThread(task =\u003e {\n if (!task.IsFaulted && !task.IsCanceled) {\n StorageMetadata meta = task.Result;\n // meta.ContentType should be an empty string now\n }\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/unity/handle-errors)\nsection of the docs.\n\nCustom Metadata\n\nYou can specify custom metadata as a `Dictionary\u003cstring, string\u003e`. \n\n```c#\nvar newMetadata = new MetadataChange {\n CustomMetadata = new Dictionary\u003cstring, string\u003e {\n {\"location\", \"Yosemite, CA, USA\"},\n {\"activity\", \"Hiking\"}\n }\n};\n\n// UpdateMetadataAsync\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 | Modifyable in MetadataChange |\n|----------------------|-----------------------|------------------------------|\n| `Bucket` | `string` | NO |\n| `Generation` | `string` | NO |\n| `MetadataGeneration` | `string` | NO |\n| `Path` | `string` | NO |\n| `Name` | `string` | NO |\n| `SizeBytes` | `long` | NO |\n| `CreationTimeMillis` | `long` | NO |\n| `UpdatedTimeMillis` | `long` | NO |\n| `CacheControl` | `string` | YES |\n| `ContentDisposition` | `string` | YES |\n| `ContentEncoding` | `string` | YES |\n| `ContentLanguage` | `string` | YES |\n| `ContentType` | `string` | YES |\n| `DownloadUrl` | `Uri` | NO |\n| `DownloadUrls` | `IList\u003cUri\u003e` | NO |\n| `CustomMetadataKeys` | `IEnumerable\u003cstring\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/unity/delete-files)\nfrom Cloud Storage."]]