वेब पर क्लाउड स्टोरेज संदर्भ बनाएं

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

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

एक संदर्भ बनाएँ

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

संदर्भ बनाने के लिए, getStorage() का उपयोग करके स्टोरेज सेवा का एक उदाहरण प्राप्त करें, फिर तर्क के रूप में सेवा के साथ ref() को कॉल करें। यह संदर्भ आपके क्लाउड स्टोरेज बकेट की जड़ की ओर इशारा करता है।

Web modular API

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

Web namespaced API

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

Web modular API

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"

Web namespaced API

// 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 शीर्ष तक सभी तरह से नेविगेट करता है।

Web modular API

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

Web namespaced API

// 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 है।

Web modular API

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;

Web namespaced API

// 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 गुणों का उपयोग करके उन फ़ाइलों को बेहतर ढंग से समझने के लिए संदर्भों का निरीक्षण कर सकते हैं जिन्हें वे इंगित करते हैं। इन गुणों से फ़ाइल का पूरा पथ, फ़ाइल का नाम और वह बकेट प्राप्त होता है जिसमें फ़ाइल संग्रहीत है।

Web modular API

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;

Web namespaced API

// 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. # , [ , ] , * , या ? का उपयोग करने से बचें , क्योंकि ये फायरबेस रीयलटाइम डेटाबेस या gsutil जैसे अन्य टूल के साथ अच्छी तरह से काम नहीं करते हैं।

पूर्ण उदाहरण

Web modular API

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;

Web namespaced API

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

आगे, आइए जानें कि क्लाउड स्टोरेज पर फ़ाइलें कैसे अपलोड करें