आपकी फ़ाइलें क्लाउड स्टोरेज बकेट में संग्रहीत हैं। इस बकेट में फ़ाइलें एक पदानुक्रमित संरचना में प्रस्तुत की जाती हैं, जैसे आपकी स्थानीय हार्ड डिस्क पर फ़ाइल सिस्टम, या फायरबेस रीयलटाइम डेटाबेस में डेटा। किसी फ़ाइल का संदर्भ बनाकर, आपका ऐप उस तक पहुंच प्राप्त करता है। फिर इन संदर्भों का उपयोग डेटा अपलोड या डाउनलोड करने, मेटाडेटा प्राप्त करने या अपडेट करने या फ़ाइल को हटाने के लिए किया जा सकता है। एक संदर्भ या तो किसी विशिष्ट फ़ाइल या पदानुक्रम में उच्च स्तर के नोड को इंगित कर सकता है।
यदि आपने Firebase रीयलटाइम डेटाबेस का उपयोग किया है, तो ये पथ आपको बहुत परिचित लगने चाहिए. हालाँकि, आपका फ़ाइल डेटा क्लाउड स्टोरेज में संग्रहीत है, रीयलटाइम डेटाबेस में नहीं ।
एक संदर्भ बनाएँ
फ़ाइलें अपलोड या डाउनलोड करने, फ़ाइलें हटाने, या मेटाडेटा प्राप्त करने या अपडेट करने के लिए, आपको उस फ़ाइल का संदर्भ बनाना होगा जिस पर आप काम करना चाहते हैं। एक संदर्भ को क्लाउड में फ़ाइल के सूचक के रूप में माना जा सकता है। संदर्भ हल्के होते हैं, इसलिए आप जितने चाहें उतने बना सकते हैं, और वे कई कार्यों के लिए पुन: प्रयोज्य भी हैं।
एक संदर्भ बनाने के लिए, 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;
संदर्भों पर सीमाएं
संदर्भ पथ और नामों में मान्य यूनिकोड वर्णों का कोई भी क्रम हो सकता है, लेकिन कुछ प्रतिबंध लगाए गए हैं जिनमें शामिल हैं:
- UTF-8 एन्कोडेड होने पर
reference.fullPath
की कुल लंबाई 1 और 1024 बाइट्स के बीच होनी चाहिए। - कोई कैरिज रिटर्न या लाइन फीड वर्ण नहीं।
-
#
,[
,]
,*
, या?
, क्योंकि ये फायरबेस रीयलटाइम डेटाबेस या 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;
आगे, आइए जानें कि क्लाउड स्टोरेज में फाइल कैसे अपलोड करें ।