वेब पर क्लाउड स्टोरेज के साथ फ़ाइल मेटाडेटा का उपयोग करें

क्लाउड स्टोरेज संदर्भ में फ़ाइल अपलोड करने के बाद, आप फ़ाइल मेटाडेटा भी प्राप्त या अपडेट कर सकते हैं, उदाहरण के लिए सामग्री प्रकार को अपडेट करने के लिए। फ़ाइलें अतिरिक्त फ़ाइल मेटाडेटा के साथ कस्टम कुंजी/मूल्य जोड़े भी संग्रहीत कर सकती हैं।

फ़ाइल मेटाडेटा प्राप्त करें

फ़ाइल मेटाडेटा में सामान्य गुण जैसे name , size और contentType (अक्सर MIME प्रकार के रूप में संदर्भित) के अलावा कुछ कम सामान्य गुण जैसे contentDisposition और timeCreated शामिल होते हैं। इस मेटाडेटा को getMetadata() विधि का उपयोग करके क्लाउड स्टोरेज संदर्भ से पुनर्प्राप्त किया जा सकता है। getMetadata() एक Promise लौटाता है जिसमें पूरा मेटाडेटा होता है, या Promise अस्वीकार होने पर एक त्रुटि होती है।

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!
  });

फ़ाइल मेटाडेटा अद्यतन करें

आप updateMetadata() विधि का उपयोग करके फ़ाइल अपलोड पूरा होने के बाद किसी भी समय फ़ाइल मेटाडेटा को अपडेट कर सकते हैं। किन संपत्तियों को अद्यतन किया जा सकता है, इस बारे में अधिक जानकारी के लिए पूरी सूची देखें। केवल मेटाडेटा में निर्दिष्ट गुण अद्यतन किए जाते हैं, अन्य सभी अपरिवर्तित छोड़ दिए जाते हैं। updateMetadata() एक Promise लौटाता है जिसमें संपूर्ण मेटाडेटा होता है, या यदि Promise अस्वीकार हो जाता है तो एक त्रुटि होती है।

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!
  });

आप मेटाडेटा प्रॉपर्टी को null पर सेट करके हटा सकते हैं:

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!
  });

त्रुटियाँ संभालें

ऐसे कई कारण हैं जिनकी वजह से मेटाडेटा प्राप्त करने या अपडेट करने में त्रुटियां हो सकती हैं, जिनमें फ़ाइल का मौजूदा न होना, या उपयोगकर्ता के पास वांछित फ़ाइल तक पहुंचने की अनुमति न होना शामिल है। त्रुटियों के बारे में अधिक जानकारी डॉक्स के हैंडल एरर्स अनुभाग में पाई जा सकती है।

कस्टम मेटाडेटा

आप कस्टम मेटाडेटा को String गुणों वाले ऑब्जेक्ट के रूप में निर्दिष्ट कर सकते हैं।

Web modular API

const metadata = {
  customMetadata: {
    'location': 'Yosemite, CA, USA',
    'activity': 'Hiking'
  }
};

Web namespaced API

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 ऑब्जेक्ट जिसमें स्ट्रिंग->स्ट्रिंग मैपिंग है हाँ

फ़ाइलें अपलोड करना, डाउनलोड करना और अपडेट करना महत्वपूर्ण है, लेकिन साथ ही उन्हें हटाना भी महत्वपूर्ण है। आइए जानें क्लाउड स्टोरेज से फाइल्स को कैसे डिलीट करें