क्लाउड स्टोरेज संदर्भ में फ़ाइल अपलोड करने के बाद, आप फ़ाइल मेटाडेटा प्राप्त या अपडेट भी कर सकते हैं, उदाहरण के लिए सामग्री प्रकार को अपडेट करने के लिए। फ़ाइलें अतिरिक्त फ़ाइल मेटाडेटा के साथ कस्टम कुंजी/मान युग्म भी संग्रहीत कर सकती हैं।
फ़ाइल मेटाडेटा प्राप्त करें
फ़ाइल मेटाडेटा में सामान्य गुण जैसे name
, size
, और सामग्री प्रकार (अक्सर MIME प्रकार के रूप में संदर्भित) होते हैं, साथ ही कुछ कम सामान्य जैसे contentDisposition
और contentType
भी timeCreated
हैं। यह मेटाडेटा getMetadata()
विधि का उपयोग करके क्लाउड स्टोरेज संदर्भ से प्राप्त किया जा सकता है। getMetadata()
पूर्ण मेटाडेटा वाला Promise
लौटाता है, या Promise
अस्वीकार होने पर एक त्रुटि।
Web version 9
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 version 8
// 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
अस्वीकार होने पर एक त्रुटि।
Web version 9
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 version 8
// 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
पर सेट करके हटा सकते हैं:
Web version 9
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 version 8
// 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
गुणों वाले ऑब्जेक्ट के रूप में निर्दिष्ट कर सकते हैं।
Web version 9
const metadata = { customMetadata: { 'location': 'Yosemite, CA, USA', 'activity': 'Hiking' } };
Web version 8
var metadata = { customMetadata: { 'location': 'Yosemite, CA, USA', 'activity': 'Hiking' } };
आप प्रत्येक फ़ाइल के लिए अतिरिक्त ऐप विशिष्ट डेटा संग्रहीत करने के लिए कस्टम मेटाडेटा का उपयोग कर सकते हैं, लेकिन हम इस प्रकार के डेटा को संग्रहीत और सिंक्रनाइज़ करने के लिए एक डेटाबेस (जैसे फायरबेस रीयलटाइम डेटाबेस ) का उपयोग करने की अत्यधिक अनुशंसा करते हैं।
फ़ाइल मेटाडेटा गुण
फ़ाइल पर मेटाडेटा गुणों की पूरी सूची नीचे उपलब्ध है:
संपत्ति | प्रकार | लिखने योग्य |
---|---|---|
bucket | डोरी | नहीं |
generation | डोरी | नहीं |
metageneration | डोरी | नहीं |
fullPath | डोरी | नहीं |
name | डोरी | नहीं |
size | संख्या | नहीं |
timeCreated | डोरी | नहीं |
updated | डोरी | नहीं |
md5Hash | डोरी | अपलोड पर हां, अपडेटमेटाडाटा पर नहीं |
cacheControl | डोरी | हां |
contentDisposition | डोरी | हां |
contentEncoding | डोरी | हां |
contentLanguage | डोरी | हां |
contentType | डोरी | हां |
customMetadata | ऑब्जेक्ट युक्त स्ट्रिंग-> स्ट्रिंग मैपिंग | हां |
फ़ाइलों को अपलोड करना, डाउनलोड करना और अपडेट करना महत्वपूर्ण है, लेकिन उन्हें हटाने में सक्षम होना भी महत्वपूर्ण है। आइए जानें कि क्लाउड स्टोरेज से फाइल्स को कैसे डिलीट किया जाता है।