After uploading a file to Cloud Storage reference, you can also get and update the file metadata, for example to view or update the content type. Files can also store custom key/value pairs with additional file metadata.
دریافت متادیتای فایل
File metadata contains common properties such as name , size , and contentType (often referred to as MIME type) in addition to some less common ones like contentDisposition and timeCreated . This metadata can be retrieved from a Cloud Storage reference using the getMetadata() method.
Kotlin
// Create a storage reference from our app val storageRef = storage.reference // Get reference to the file val forestRef = storageRef.child("images/forest.jpg")
forestRef.metadata.addOnSuccessListener { metadata -> // Metadata now contains the metadata for 'images/forest.jpg' }.addOnFailureListener { // Uh-oh, an error occurred! }
Java
// Create a storage reference from our app StorageReference storageRef = storage.getReference(); // Get reference to the file StorageReference forestRef = storageRef.child("images/forest.jpg");
forestRef.getMetadata().addOnSuccessListener(new OnSuccessListener<StorageMetadata>() { @Override public void onSuccess(StorageMetadata storageMetadata) { // Metadata now contains the metadata for 'images/forest.jpg' } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Uh-oh, an error occurred! } });
بهروزرسانی فراداده فایل
You can update file metadata at any time after the file upload completes by using the updateMetadata() method. Refer to the full list for more information on what properties can be updated. Only the properties specified in the metadata are updated, all others are left unmodified.
Kotlin
// Create a storage reference from our app val storageRef = storage.reference // Get reference to the file val forestRef = storageRef.child("images/forest.jpg")
// Create file metadata including the content type val metadata = storageMetadata { contentType = "image/jpg" setCustomMetadata("myCustomProperty", "myValue") } // Update metadata properties forestRef.updateMetadata(metadata).addOnSuccessListener { updatedMetadata -> // Updated metadata is in updatedMetadata }.addOnFailureListener { // Uh-oh, an error occurred! }
Java
// Create a storage reference from our app StorageReference storageRef = storage.getReference(); // Get reference to the file StorageReference forestRef = storageRef.child("images/forest.jpg");
// Create file metadata including the content type StorageMetadata metadata = new StorageMetadata.Builder() .setContentType("image/jpg") .setCustomMetadata("myCustomProperty", "myValue") .build(); // Update metadata properties forestRef.updateMetadata(metadata) .addOnSuccessListener(new OnSuccessListener<StorageMetadata>() { @Override public void onSuccess(StorageMetadata storageMetadata) { // Updated metadata is in storageMetadata } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Uh-oh, an error occurred! } });
شما میتوانید با ارسال null ویژگیهای فراداده قابل نوشتن را حذف کنید:
Kotlin
// Create file metadata with property to delete val metadata = storageMetadata { contentType = null } // Delete the metadata property forestRef.updateMetadata(metadata).addOnSuccessListener { updatedMetadata -> // updatedMetadata.contentType should be null }.addOnFailureListener { // Uh-oh, an error occurred! }
Java
// Create file metadata with property to delete StorageMetadata metadata = new StorageMetadata.Builder() .setContentType(null) .build(); // Delete the metadata property forestRef.updateMetadata(metadata) .addOnSuccessListener(new OnSuccessListener<StorageMetadata>() { @Override public void onSuccess(StorageMetadata storageMetadata) { // metadata.contentType should be null } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Uh-oh, an error occurred! } });
مدیریت خطاها
There are a number of reasons why errors may occur on getting or updating metadata, including the file not existing, or the user not having permission to access the desired file. More information on errors can be found in the Handle Errors section of the docs.
فراداده سفارشی
شما میتوانید با استفاده از متد setCustomMetadata() در کلاس StorageMetadata.Builder متادیتای سفارشی تعیین کنید.
Kotlin
val metadata = storageMetadata { setCustomMetadata("location", "Yosemite, CA, USA") setCustomMetadata("activity", "Hiking") }
Java
StorageMetadata metadata = new StorageMetadata.Builder() .setCustomMetadata("location", "Yosemite, CA, USA") .setCustomMetadata("activity", "Hiking") .build();
شما میتوانید دادههای مختص برنامه را برای هر فایل در متادیتای سفارشی ذخیره کنید، اما ما اکیداً توصیه میکنیم از یک پایگاه داده (مانند Firebase Realtime Database ) برای ذخیره و همگامسازی این نوع دادهها استفاده کنید.
ویژگیهای فراداده فایل
لیست کاملی از ویژگیهای فراداده در یک فایل در زیر موجود است:
| دریافتکنندهی املاک | نوع | وجود دارد |
|---|---|---|
getBucket | String | خیر |
getGeneration | String | خیر |
getMetadataGeneration | String | خیر |
getPath | String | خیر |
getName | String | خیر |
getSizeBytes | long | خیر |
getCreationTimeMillis | long | خیر |
getUpdatedTimeMillis | long | خیر |
getMd5Hash | String | خیر |
getCacheControl | String | بله |
getContentDisposition | String | بله |
getContentEncoding | String | بله |
getContentLanguage | String | بله |
getContentType | String | بله |
getCustomMetadata | String | بله |
getCustomMetadataKeys | Set<String> | خیر |
آپلود، دانلود و بهروزرسانی فایلها مهم است، اما توانایی حذف آنها نیز به همان اندازه مهم است. بیایید یاد بگیریم که چگونه فایلها را از Cloud Storage حذف کنیم.