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

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

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

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

वेब मॉड्यूलर एपीआई

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

वेब नेमस्पेसेड एपीआई

// 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, अनुरोध अस्वीकार करता है, तो यह गड़बड़ी का मैसेज दिखाता है.

वेब मॉड्यूलर एपीआई

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

वेब नेमस्पेसेड एपीआई

// 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 पर सेट करके मिटाया जा सकता है:

वेब मॉड्यूलर एपीआई

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

वेब नेमस्पेसेड एपीआई

// 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 प्रॉपर्टी वाले ऑब्जेक्ट के तौर पर तय किया जा सकता है.

वेब मॉड्यूलर एपीआई

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

वेब नेमस्पेसेड एपीआई

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

हर फ़ाइल के लिए ऐप्लिकेशन से जुड़ा अतिरिक्त डेटा सेव करने के लिए, कस्टम मेटाडेटा का इस्तेमाल किया जा सकता है. हालांकि, इस तरह के डेटा को सेव और सिंक करने के लिए, हम डेटाबेस (जैसे कि Firebase रीयल टाइम डेटाबेस) का इस्तेमाल करने का सुझाव देते हैं.

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

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

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

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