आपकी फ़ाइलें Cloud Storage बकेट में संग्रहित की जाती हैं। इस बकेट में फ़ाइलें एक पदानुक्रमित संरचना में प्रस्तुत की जाती हैं, ठीक वैसे ही जैसे आपकी स्थानीय हार्ड डिस्क पर फ़ाइल सिस्टम, या Firebase रीयलटाइम डेटाबेस में डेटा। किसी फ़ाइल का संदर्भ बनाकर, आपका ऐप उस तक पहुंच प्राप्त करता है। फिर इन संदर्भों का उपयोग डेटा अपलोड या डाउनलोड करने, मेटाडेटा प्राप्त करने या अपडेट करने या फ़ाइल को हटाने के लिए किया जा सकता है। एक संदर्भ या तो एक विशिष्ट फ़ाइल या पदानुक्रम में उच्च स्तर के नोड को इंगित कर सकता है।
यदि आपने फायरबेस रीयलटाइम डेटाबेस का उपयोग किया है, तो ये पथ आपको बहुत परिचित प्रतीत होने चाहिए। हालाँकि, आपका फ़ाइल डेटा क्लाउड स्टोरेज में संग्रहीत होता है, रीयलटाइम डेटाबेस में नहीं ।
एक संदर्भ बनाएं
फ़ाइलें अपलोड या डाउनलोड करने, फ़ाइलें हटाने, या मेटाडेटा प्राप्त करने या अपडेट करने के लिए, आपको उस फ़ाइल का एक संदर्भ बनाना होगा जिस पर आप काम करना चाहते हैं। एक संदर्भ को क्लाउड में किसी फ़ाइल के सूचक के रूप में माना जा सकता है। संदर्भ हल्के होते हैं, इसलिए आप जितने चाहें उतने बना सकते हैं, और वे कई कार्यों के लिए पुन: प्रयोज्य भी हैं।
एक संदर्भ बनाने के लिए, getStorage()
का उपयोग करके संग्रहण सेवा का एक उदाहरण प्राप्त करें, फिर सेवा के साथ ref()
को तर्क के रूप में कॉल करें। यह संदर्भ आपके क्लाउड स्टोरेज बकेट के मूल की ओर इशारा करता है।
Web version 9
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 version 8
// 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 version 9
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 version 8
// 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 version 9
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 version 8
// 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 version 9
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 version 8
// 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 version 9
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 version 8
// 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.fullPath
की कुल लंबाई। UTF-8 एन्कोड किए जाने पर FullPath 1 और 1024 बाइट्स के बीच होना चाहिए। - कोई कैरिज रिटर्न या लाइन फीड वर्ण नहीं।
-
#
,[
,]
,*
, या?
, क्योंकि ये Firebase रीयलटाइम डेटाबेस या gsutil जैसे अन्य टूल के साथ ठीक से काम नहीं करते हैं।
पूरा उदाहरण
Web version 9
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 version 8
// 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;
इसके बाद, आइए जानें कि क्लाउड स्टोरेज में फाइल कैसे अपलोड करें ।