वेब पर Cloud Storage का रेफ़रंस बनाना

आपकी फ़ाइलें Cloud Storage बकेट में सेव होती हैं. इस बकेट में मौजूद फ़ाइलों को हैरारकी के हिसाब से स्ट्रक्चर में दिखाया जाता है. यह ठीक वैसे ही होता है जैसे आपकी लोकल हार्ड डिस्क में मौजूद फ़ाइल सिस्टम या Firebase रीयल टाइम डेटाबेस में मौजूद डेटा होता है. किसी फ़ाइल का रेफ़रंस बनाने पर, आपका ऐप्लिकेशन उसे ऐक्सेस कर सकता है. इसके बाद, इन पहचान फ़ाइलों का इस्तेमाल डेटा को अपलोड या डाउनलोड करने, मेटाडेटा को पाने या अपडेट करने या फ़ाइल को मिटाने के लिए किया जा सकता है. रेफ़रंस फ़ाइल या तो किसी खास फ़ाइल या हैरारकी में बड़े लेवल की नोड हो सकती है.

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

रेफ़रंस बनाना

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

रेफ़रंस बनाने के लिए, getStorage() का इस्तेमाल करके स्टोरेज सेवा का इंस्टेंस पाएं. इसके बाद, सेवा के साथ ref() को आर्ग्युमेंट के तौर पर कॉल करें. यह रेफ़रंस, आपके Cloud Storage बकेट के रूट के बारे में बताता है.

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

import { getStorage, ref } from "firebase/storage";

// Get a reference to the storage service, which is used to create references in your storage bucket
const storage = getStorage();

// Create a storage reference from our storage service
const storageRef = ref(storage);

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

// Get a reference to the storage service, which is used to create references in your storage bucket
var storage = firebase.storage();

// Create a storage reference from our storage service
var storageRef = storage.ref();

आपके पास ट्री में नीचे की जगह के लिए रेफ़रंस बनाने का विकल्प है. जैसे, ref() को कॉल करते समय, इस पाथ में दूसरे आर्ग्युमेंट के तौर पर 'images/space.jpg' पास करें.

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

import { getStorage, ref } from "firebase/storage";

const storage = getStorage();

// Create a child reference
const imagesRef = ref(storage, 'images');
// imagesRef now points to 'images'

// Child references can also take paths delimited by '/'
const spaceRef = ref(storage, 'images/space.jpg');
// spaceRef now points to "images/space.jpg"
// imagesRef still points to "images"

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

// Create a child reference
var imagesRef = storageRef.child('images');
// imagesRef now points to 'images'

// Child references can also take paths delimited by '/'
var spaceRef = storageRef.child('images/space.jpg');
// spaceRef now points to "images/space.jpg"
// imagesRef still points to "images"

फ़ाइल हैरारकी में नेविगेट करने के लिए, parent और root प्रॉपर्टी का भी इस्तेमाल किया जा सकता है. parent एक लेवल ऊपर जाता है, जबकि root ऊपर की ओर नेविगेट करता है.

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

import { getStorage, ref } from "firebase/storage";

const storage = getStorage();
const spaceRef = ref(storage, 'images/space.jpg');

// Parent allows us to move to the parent of a reference
const imagesRef = spaceRef.parent;
// imagesRef now points to 'images'

// Root allows us to move all the way back to the top of our bucket
const rootRef = spaceRef.root;
// rootRef now points to the root

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

// Parent allows us to move to the parent of a reference
var imagesRef = spaceRef.parent;
// imagesRef now points to 'images'

// Root allows us to move all the way back to the top of our bucket
var rootRef = spaceRef.root;
// rootRef now points to the root

child(), parent, और root को एक साथ कई बार जोड़ा जा सकता है, क्योंकि हर एक रेफ़रंस देता है. इसका अपवाद root का parent है, जो कि null है.

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

import { getStorage, ref } from "firebase/storage";

const storage = getStorage();
const spaceRef = ref(storage, 'images/space.jpg');

// References can be chained together multiple times
const earthRef = ref(spaceRef.parent, 'earth.jpg');
// earthRef points to 'images/earth.jpg'

// nullRef is null, since the parent of root is null
const nullRef = spaceRef.root.parent;

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

// References can be chained together multiple times
var earthRef = spaceRef.parent.child('earth.jpg');
// earthRef points to 'images/earth.jpg'

// nullRef is null, since the parent of root is null
var nullRef = spaceRef.root.parent;

रेफ़रंस प्रॉपर्टी

रेफ़रंस की जांच करने से, उन फ़ाइलों को बेहतर तरीके से समझा जा सकता है जो fullPath, name, और bucket प्रॉपर्टी का इस्तेमाल करके रेफ़र की जाती हैं. इन प्रॉपर्टी को फ़ाइल का पूरा पाथ, फ़ाइल का नाम, और उस बकेट की जानकारी मिलती है जिसमें फ़ाइल सेव की जाती है.

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

import { getStorage, ref } from "firebase/storage";

const storage = getStorage();
const spaceRef = ref(storage, 'images/space.jpg');

// Reference's path is: 'images/space.jpg'
// This is analogous to a file path on disk
spaceRef.fullPath;

// Reference's name is the last segment of the full path: 'space.jpg'
// This is analogous to the file name
spaceRef.name;

// Reference's bucket is the name of the storage bucket where files are stored
spaceRef.bucket;

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

// Reference's path is: 'images/space.jpg'
// This is analogous to a file path on disk
spaceRef.fullPath;

// Reference's name is the last segment of the full path: 'space.jpg'
// This is analogous to the file name
spaceRef.name;

// Reference's bucket is the name of the storage bucket where files are stored
spaceRef.bucket;

पहचान फ़ाइलों की सीमाएं

रेफ़रंस पाथ और नामों में मान्य यूनिकोड वर्णों का कोई भी क्रम हो सकता है, लेकिन कुछ पाबंदियां लागू होती हैं, जिनमें ये शामिल हैं:

  1. UTF-8 कोड में बदलने पर, reference.fullPath की कुल लंबाई 1 से 1024 बाइट के बीच होनी चाहिए.
  2. कोई नई लाइन शुरू करने का चिह्न या लाइन फ़ीड वर्ण नहीं.
  3. #, [, ], * या ? का इस्तेमाल करने से बचें, क्योंकि ये Firebase रीयल टाइम डेटाबेस या gsutil जैसे दूसरे टूल के साथ ठीक से काम नहीं करते.

पूरा उदाहरण

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

import { getStorage, ref } from "firebase/storage";

const storage = getStorage();

// Points to the root reference
const storageRef = ref(storage);

// Points to 'images'
const imagesRef = ref(storageRef, 'images');

// Points to 'images/space.jpg'
// Note that you can use variables to create child values
const fileName = 'space.jpg';
const spaceRef = ref(imagesRef, fileName);

// File path is 'images/space.jpg'
const path = spaceRef.fullPath;

// File name is 'space.jpg'
const name = spaceRef.name;

// Points to 'images'
const imagesRefAgain = spaceRef.parent;

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

// Points to the root reference
var storageRef = firebase.storage().ref();

// Points to 'images'
var imagesRef = storageRef.child('images');

// Points to 'images/space.jpg'
// Note that you can use variables to create child values
var fileName = 'space.jpg';
var spaceRef = imagesRef.child(fileName);

// File path is 'images/space.jpg'
var path = spaceRef.fullPath;

// File name is 'space.jpg'
var name = spaceRef.name;

// Points to 'images'
var imagesRef = spaceRef.parent;

अब Cloud Storage में फ़ाइलें अपलोड करने का तरीका जानते हैं.