वेब पर Cloud Storage के साथ फ़ाइल मेटाडेटा इस्तेमाल करना

Cloud Storage की रेफ़रंस फ़ाइल में फ़ाइल अपलोड करने के बाद, आपको या फ़ाइल मेटाडेटा अपडेट करें, जैसे कि कॉन्टेंट टाइप को अपडेट करने के लिए. फ़ाइल अतिरिक्त फ़ाइल मेटाडेटा के साथ पसंद के मुताबिक कुंजी/वैल्यू पेयर को भी सेव कर सकता है.

फ़ाइल मेटाडेटा पाएं

फ़ाइल के मेटाडेटा में सामान्य प्रॉपर्टी मौजूद हैं. जैसे, name, size, और contentType (इन्हें अक्सर MIME टाइप कहा जाता है) और कुछ कम contentDisposition और timeCreated जैसी सामान्य क्वेरी. यह मेटाडेटा इनमें से कोई भी हो सकता है: Cloud Storage के रेफ़रंस से लिया गया है, तो getMetadata() तरीका. getMetadata() ऐसा Promise लौटाता है जिसमें यह शामिल हो पूरा मेटाडेटा मिलेगा या अगर Promise अस्वीकार कर देता है, तो कोई गड़बड़ी होगी.

Web

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

// 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

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

// 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

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

// 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

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

Web

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

हर एक के लिए अतिरिक्त ऐप्लिकेशन खास डेटा स्टोर करने के लिए आप कस्टम मेटाडेटा का इस्तेमाल कर सकते हैं है, लेकिन हम एक डेटाबेस (जैसे कि Firebase रीयल टाइम डेटाबेस) का इस्तेमाल करें.

फ़ाइल मेटाडेटा की प्रॉपर्टी

फ़ाइल में मौजूद मेटाडेटा प्रॉपर्टी की पूरी सूची यहां दी गई है:

प्रॉपर्टी टाइप लिखा जा सकता है
bucket स्ट्रिंग नहीं
generation स्ट्रिंग नहीं
metageneration स्ट्रिंग नहीं
fullPath स्ट्रिंग नहीं
name स्ट्रिंग नहीं
size नंबर नहीं
timeCreated स्ट्रिंग नहीं
updated स्ट्रिंग नहीं
md5Hash स्ट्रिंग अपलोड करने पर हां, अपडेट मेटाडेटा पर नहीं
cacheControl स्ट्रिंग YES
contentDisposition स्ट्रिंग YES
contentEncoding स्ट्रिंग YES
contentLanguage स्ट्रिंग YES
contentType स्ट्रिंग YES
customMetadata ऑब्जेक्ट में स्ट्रिंग->स्ट्रिंग मैपिंग शामिल है YES

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