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, ek dosya meta verileriyle birlikte özel anahtar/değer çiftlerini de depolayabilir.
Dosya Meta Verilerini Alın
Dosya meta verileri, name
, size
ve contentType
(genellikle MIME türü olarak adlandırılır) gibi ortak özelliklerin yanı sıra contentDisposition
ve timeCreated
gibi daha az yaygın olan bazı özellikleri içerir. Bu meta veriler, getMetadata()
yöntemi kullanılarak bir Bulut Depolama referansından alınabilir. getMetadata()
tüm meta verileri içeren bir Promise
veya Promise
reddedilmesi durumunda 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 yüklemesi tamamlandıktan sonra updateMetadata()
yöntemini kullanarak dosya meta verilerini 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
reddederse 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! });
İşleme Hataları
Dosyanın mevcut olmaması veya kullanıcının istenen dosyaya erişim iznine sahip olmaması da dahil olmak üzere, meta verileri alırken veya güncellerken hataların ortaya çıkmasının birkaç nedeni vardır. Hatalar hakkında daha fazla bilgi, belgelerin Hataları İşle bölümünde bulunabilir.
Özel Meta Veriler
Özel meta verileri, String
özelliklerini içeren bir nesne olarak belirleyebilirsiniz.
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ı önemle tavsiye ederiz.
Dosya Meta Veri Ö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.