Bir dosyayı Cloud Storage referansına yükledikten sonra, örneğin içerik türünü güncellemek için dosya meta verilerini de alabilir veya güncelleyebilirsiniz. Dosyalar ayrıca ek dosya meta verileriyle birlikte özel anahtar/değer çiftlerini de depolayabilir.
Dosya Meta Verilerini Al
Dosya meta verileri, contentDisposition
ve timeCreated
gibi daha az yaygın olanların yanı sıra name
, size
ve contentType
(genellikle MIME türü olarak anılır) gibi ortak özellikleri içerir. Bu meta veriler, getMetadata()
yöntemi kullanılarak bir Cloud Storage referansından alınabilir. getMetadata()
tüm meta verileri içeren bir Promise
veya Promise
reddedilirse bir hata döndürür.
Web modular API
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! });
Web namespaced API
// 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! });
Dosya Meta Verilerini Güncelle
Dosya meta verilerini, dosya yükleme işlemi tamamlandıktan sonra updateMetadata()
yöntemini kullanarak istediğiniz zaman güncelleyebilirsiniz. Hangi özelliklerin güncellenebileceği hakkında daha fazla bilgi için tam listeye bakın. Yalnızca meta verilerde belirtilen özellikler güncellenir, diğerleri değiştirilmeden bırakılır. updateMetadata()
tüm meta verileri içeren bir Promise
veya Promise
reddedilirse bir hata döndürür.
Web modular API
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! });
Web namespaced API
// 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! });
Bir meta veri özelliğini null
olarak ayarlayarak silebilirsiniz:
Web modular API
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! });
Web namespaced API
// 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! });
Hataları İşle
Meta verileri alırken veya güncellerken, dosyanın mevcut olmaması veya kullanıcının istenen dosyaya erişim iznine sahip olmaması da dahil olmak üzere hataların ortaya çıkmasının çeşitli nedenleri vardır. Hatalarla ilgili daha fazla bilgiyi dokümanların Hataları İşleme bölümünde bulabilirsiniz.
Özel Meta Veriler
Özel meta verileri, String
özelliklerini içeren bir nesne olarak belirtebilirsiniz.
Web modular API
const metadata = { customMetadata: { 'location': 'Yosemite, CA, USA', 'activity': 'Hiking' } };
Web namespaced API
var metadata = { customMetadata: { 'location': 'Yosemite, CA, USA', 'activity': 'Hiking' } };
Her dosya için uygulamaya özel ek verileri depolamak için özel meta verileri kullanabilirsiniz, ancak bu tür verileri depolamak ve senkronize etmek için bir veritabanı ( Firebase Gerçek Zamanlı Veritabanı gibi) kullanmanızı kesinlikle öneririz.
Dosya Meta Verisi Özellikleri
Bir dosyadaki meta veri özelliklerinin tam listesi aşağıda mevcuttur:
Mülk | Tip | Yazılabilir |
---|---|---|
bucket | sicim | HAYIR |
generation | sicim | HAYIR |
metageneration | sicim | HAYIR |
fullPath | sicim | HAYIR |
name | sicim | HAYIR |
size | sayı | HAYIR |
timeCreated | sicim | HAYIR |
updated | sicim | HAYIR |
md5Hash | sicim | Yüklemede EVET, updateMetadata'da HAYIR |
cacheControl | sicim | EVET |
contentDisposition | sicim | EVET |
contentEncoding | sicim | EVET |
contentLanguage | sicim | EVET |
contentType | sicim | EVET |
customMetadata | Dize->dize eşlemelerini içeren nesne | EVET |
Dosyaları yüklemek, indirmek ve güncellemek önemlidir, ancak bunları kaldırabilmek de önemlidir. Cloud Storage'dan dosyaların nasıl silineceğini öğrenelim.