پس از آپلود فایل در مرجع Cloud Storage ، همچنین می توانید متادیتای فایل را دریافت یا به روز کنید، به عنوان مثال برای به روز رسانی نوع محتوا. فایلها همچنین میتوانند جفتهای کلید/مقدار سفارشی را با فراداده فایل اضافی ذخیره کنند.
فراداده فایل را دریافت کنید
name
، size
، و contentType
(که اغلب به عنوان نوع MIME شناخته میشود) علاوه بر برخی موارد کمتر رایج مانند contentDisposition
و timeCreated
. این ابرداده را می توان با استفاده از متد getMetadata()
از یک مرجع Cloud Storage بازیابی کرد. getMetadata()
یک Promise
حاوی فراداده کامل یا یک خطا در صورت رد شدن Promise
برمی گرداند.
import { getStorage, ref, getMetadata } from "firebase/storage"; // Create a reference to the file whose metadata we want to retrieve const storage = getStorage(); const forestRef = ref(storage, 'images/forest.jpg'); // Get metadata properties getMetadata(forestRef) .then((metadata) => { // Metadata now contains the metadata for 'images/forest.jpg' }) .catch((error) => { // Uh-oh, an error occurred! });
// Create a reference to the file whose metadata we want to retrieve var forestRef = storageRef.child('images/forest.jpg'); // Get metadata properties forestRef.getMetadata() .then((metadata) => { // Metadata now contains the metadata for 'images/forest.jpg' }) .catch((error) => { // Uh-oh, an error occurred! });
به روز رسانی متادیتا فایل
updateMetadata()
میتوانید پس از اتمام آپلود فایل، ابردادههای فایل را بهروزرسانی کنید. برای اطلاعات بیشتر در مورد ویژگی هایی که می توانند به روز شوند به لیست کامل مراجعه کنید. فقط ویژگیهای مشخصشده در فراداده بهروزرسانی میشوند، بقیه خصوصیات بدون تغییر باقی میمانند. updateMetadata()
یک Promise
حاوی فراداده کامل یا یک خطا در صورت رد شدن Promise
برمی گرداند.
import { getStorage, ref, updateMetadata } from "firebase/storage"; // Create a reference to the file whose metadata we want to change const storage = getStorage(); const forestRef = ref(storage, 'images/forest.jpg'); // Create file metadata to update const newMetadata = { cacheControl: 'public,max-age=300', contentType: 'image/jpeg' }; // Update metadata properties updateMetadata(forestRef, newMetadata) .then((metadata) => { // Updated metadata for 'images/forest.jpg' is returned in the Promise }).catch((error) => { // Uh-oh, an error occurred! });
// Create a reference to the file whose metadata we want to change var forestRef = storageRef.child('images/forest.jpg'); // Create file metadata to update var newMetadata = { cacheControl: 'public,max-age=300', contentType: 'image/jpeg' }; // Update metadata properties forestRef.updateMetadata(newMetadata) .then((metadata) => { // Updated metadata for 'images/forest.jpg' is returned in the Promise }).catch((error) => { // Uh-oh, an error occurred! });
null
حذف کنید:
import { getStorage, ref, updateMetadata } from "firebase/storage"; const storage = getStorage(); const forestRef = ref(storage, 'images/forest.jpg'); // Create file metadata with property to delete const deleteMetadata = { contentType: null }; // Delete the metadata property updateMetadata(forestRef, deleteMetadata) .then((metadata) => { // metadata.contentType should be null }).catch((error) => { // Uh-oh, an error occurred! });
// Create file metadata with property to delete var deleteMetadata = { contentType: null }; // Delete the metadata property forestRef.updateMetadata(deleteMetadata) .then((metadata) => { // metadata.contentType should be null }).catch((error) => { // Uh-oh, an error occurred! });
رسیدگی به خطاها
فراداده سفارشی
String
مشخص کنید.
const metadata = { customMetadata: { 'location': 'Yosemite, CA, USA', 'activity': 'Hiking' } };
var metadata = { customMetadata: { 'location': 'Yosemite, CA, USA', 'activity': 'Hiking' } };
ویژگی های فراداده فایل
اموال | تایپ کنید | قابل نوشتن |
---|---|---|
bucket | رشته | نه |
generation | رشته | نه |
metageneration | رشته | نه |
fullPath | رشته | نه |
name | رشته | نه |
size | شماره | نه |
timeCreated | رشته | نه |
updated | رشته | نه |
md5Hash | رشته | بله در آپلود، خیر در updateMetadata |
cacheControl | رشته | بله |
contentDisposition | رشته | بله |
contentEncoding | رشته | بله |
contentLanguage | رشته | بله |
contentType | رشته | بله |
customMetadata | شیء حاوی نگاشت رشته-> رشته | بله |