वेब पर क्लाउड स्टोरेज के साथ फ़ाइलें अपलोड करें

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

फाइलें अपलोड करें

किसी फ़ाइल को क्लाउड स्टोरेज पर अपलोड करने के लिए, आप पहले फ़ाइल नाम सहित फ़ाइल के पूर्ण पथ का एक संदर्भ बनाते हैं।

Web modular API

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

// Create a root reference
const storage = getStorage();

// Create a reference to 'mountains.jpg'
const mountainsRef = ref(storage, 'mountains.jpg');

// Create a reference to 'images/mountains.jpg'
const mountainImagesRef = ref(storage, 'images/mountains.jpg');

// While the file names are the same, the references point to different files
mountainsRef.name === mountainImagesRef.name;           // true
mountainsRef.fullPath === mountainImagesRef.fullPath;   // false 

Web namespaced API

// Create a root reference
var storageRef = firebase.storage().ref();

// Create a reference to 'mountains.jpg'
var mountainsRef = storageRef.child('mountains.jpg');

// Create a reference to 'images/mountains.jpg'
var mountainImagesRef = storageRef.child('images/mountains.jpg');

// While the file names are the same, the references point to different files
mountainsRef.name === mountainImagesRef.name;           // true
mountainsRef.fullPath === mountainImagesRef.fullPath;   // false 

Blob या File से अपलोड करें

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

Web modular API

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

const storage = getStorage();
const storageRef = ref(storage, 'some-child');

// 'file' comes from the Blob or File API
uploadBytes(storageRef, file).then((snapshot) => {
  console.log('Uploaded a blob or file!');
});

Web namespaced API

// 'file' comes from the Blob or File API
ref.put(file).then((snapshot) => {
  console.log('Uploaded a blob or file!');
});

बाइट ऐरे से अपलोड करें

File और Blob प्रकारों के अलावा, uploadBytes() क्लाउड स्टोरेज पर Uint8Array भी अपलोड कर सकता है।

Web modular API

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

const storage = getStorage();
const storageRef = ref(storage, 'some-child');

const bytes = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21]);
uploadBytes(storageRef, bytes).then((snapshot) => {
  console.log('Uploaded an array!');
});

Web namespaced API

var bytes = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21]);
ref.put(bytes).then((snapshot) => {
  console.log('Uploaded an array!');
});

एक स्ट्रिंग से अपलोड करें

यदि Blob , File , या Uint8Array उपलब्ध नहीं है, तो आप क्लाउड स्टोरेज पर रॉ, base64 , base64url , या data_url एन्कोडेड स्ट्रिंग अपलोड करने के लिए uploadString() विधि का उपयोग कर सकते हैं।

Web modular API

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

const storage = getStorage();
const storageRef = ref(storage, 'some-child');

// Raw string is the default if no format is provided
const message = 'This is my message.';
uploadString(storageRef, message).then((snapshot) => {
  console.log('Uploaded a raw string!');
});

// Base64 formatted string
const message2 = '5b6p5Y+344GX44G+44GX44Gf77yB44GK44KB44Gn44Go44GG77yB';
uploadString(storageRef, message2, 'base64').then((snapshot) => {
  console.log('Uploaded a base64 string!');
});

// Base64url formatted string
const message3 = '5b6p5Y-344GX44G-44GX44Gf77yB44GK44KB44Gn44Go44GG77yB';
uploadString(storageRef, message3, 'base64url').then((snapshot) => {
  console.log('Uploaded a base64url string!');
});

// Data URL string
const message4 = 'data:text/plain;base64,5b6p5Y+344GX44G+44GX44Gf77yB44GK44KB44Gn44Go44GG77yB';
uploadString(storageRef, message4, 'data_url').then((snapshot) => {
  console.log('Uploaded a data_url string!');
});

Web namespaced API

// Raw string is the default if no format is provided
var message = 'This is my message.';
ref.putString(message).then((snapshot) => {
  console.log('Uploaded a raw string!');
});

// Base64 formatted string
var message = '5b6p5Y+344GX44G+44GX44Gf77yB44GK44KB44Gn44Go44GG77yB';
ref.putString(message, 'base64').then((snapshot) => {
  console.log('Uploaded a base64 string!');
});

// Base64url formatted string
var message = '5b6p5Y-344GX44G-44GX44Gf77yB44GK44KB44Gn44Go44GG77yB';
ref.putString(message, 'base64url').then((snapshot) => {
  console.log('Uploaded a base64url string!');
});

// Data URL string
var message = 'data:text/plain;base64,5b6p5Y+344GX44G+44GX44Gf77yB44GK44KB44Gn44Go44GG77yB';
ref.putString(message, 'data_url').then((snapshot) => {
  console.log('Uploaded a data_url string!');
});

चूँकि संदर्भ फ़ाइल के पूर्ण पथ को परिभाषित करता है, इसलिए सुनिश्चित करें कि आप एक गैर-रिक्त पथ पर अपलोड कर रहे हैं।

फ़ाइल मेटाडेटा जोड़ें

किसी फ़ाइल को अपलोड करते समय, आप उस फ़ाइल के लिए मेटाडेटा भी निर्दिष्ट कर सकते हैं। इस मेटाडेटा में विशिष्ट फ़ाइल मेटाडेटा गुण जैसे name , size और contentType (आमतौर पर MIME प्रकार के रूप में जाना जाता है) शामिल हैं। क्लाउड स्टोरेज स्वचालित रूप से फ़ाइल एक्सटेंशन से सामग्री प्रकार का अनुमान लगाता है जहां फ़ाइल डिस्क पर संग्रहीत होती है, लेकिन यदि आप मेटाडेटा में एक contentType निर्दिष्ट करते हैं तो यह स्वतः-पहचाने गए प्रकार को ओवरराइड कर देगा। यदि कोई contentType मेटाडेटा निर्दिष्ट नहीं है और फ़ाइल में फ़ाइल एक्सटेंशन नहीं है, तो क्लाउड स्टोरेज application/octet-stream प्रकार पर डिफ़ॉल्ट होता है। फ़ाइल मेटाडेटा पर अधिक जानकारी फ़ाइल मेटाडेटा का उपयोग करें अनुभाग में पाई जा सकती है।

Web modular API

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

const storage = getStorage();
const storageRef = ref(storage, 'images/mountains.jpg');

// Create file metadata including the content type
/** @type {any} */
const metadata = {
  contentType: 'image/jpeg',
};

// Upload the file and metadata
const uploadTask = uploadBytes(storageRef, file, metadata);

Web namespaced API

// Create file metadata including the content type
var metadata = {
  contentType: 'image/jpeg',
};

// Upload the file and metadata
var uploadTask = storageRef.child('images/mountains.jpg').put(file, metadata);

अपलोड प्रबंधित करें

अपलोड शुरू करने के अलावा, आप stop pause() और cancel() तरीकों का उपयोग करके अपलोड को रोक सकते हैं, resume() और रद्द कर सकते हैं। pause() या resume() को कॉल करने से pause या running स्थिति में बदलाव आ जाएगा। cancel() विधि को कॉल करने से अपलोड विफल हो जाता है और एक त्रुटि आती है जो दर्शाती है कि अपलोड रद्द कर दिया गया था।

Web modular API

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

const storage = getStorage();
const storageRef = ref(storage, 'images/mountains.jpg');

// Upload the file and metadata
const uploadTask = uploadBytesResumable(storageRef, file);

// Pause the upload
uploadTask.pause();

// Resume the upload
uploadTask.resume();

// Cancel the upload
uploadTask.cancel();

Web namespaced API

// Upload the file and metadata
var uploadTask = storageRef.child('images/mountains.jpg').put(file);

// Pause the upload
uploadTask.pause();

// Resume the upload
uploadTask.resume();

// Cancel the upload
uploadTask.cancel();

अपलोड प्रगति की निगरानी करें

अपलोड करते समय, अपलोड कार्य state_changed पर्यवेक्षक में प्रगति की घटनाओं को बढ़ा सकता है, जैसे:

घटना प्रकार विशिष्ट उपयोग
running यह ईवेंट तब सक्रिय होता है जब कार्य प्रारंभ होता है या अपलोड करना फिर से शुरू होता है, और अक्सर pause ईवेंट के साथ संयोजन में उपयोग किया जाता है। बड़े अपलोड के लिए यह ईवेंट प्रगति अद्यतन के रूप में कई बार सक्रिय हो सकता है।
pause यह ईवेंट किसी भी समय अपलोड रुकने पर सक्रिय हो जाता है, और अक्सर running ईवेंट के साथ संयोजन में उपयोग किया जाता है।

जब कोई घटना घटती है, तो एक TaskSnapshot ऑब्जेक्ट वापस भेज दिया जाता है। यह स्नैपशॉट घटना घटित होने के समय कार्य का एक अपरिवर्तनीय दृश्य है। इस ऑब्जेक्ट में निम्नलिखित गुण हैं:

संपत्ति प्रकार विवरण
bytesTransferred Number इस स्नैपशॉट को लेते समय स्थानांतरित किए गए बाइट्स की कुल संख्या।
totalBytes Number अपलोड किए जाने वाले अपेक्षित बाइट्स की कुल संख्या.
state firebase.storage.TaskState अपलोड की वर्तमान स्थिति.
metadata firebaseStorage.Metadata अपलोड पूरा होने से पहले, मेटाडेटा सर्वर को भेजा जाता है। अपलोड पूरा होने के बाद, सर्वर ने मेटाडेटा वापस भेज दिया।
task firebaseStorage.UploadTask यह कार्य एक स्नैपशॉट है, जिसका उपयोग कार्य को 'रोकने', 'फिर से शुरू करने' या 'रद्द' करने के लिए किया जा सकता है।
ref firebaseStorage.Reference यह कार्य जिस संदर्भ से आया है।

राज्य में ये परिवर्तन, TaskSnapshot के गुणों के साथ मिलकर अपलोड घटनाओं की निगरानी करने का एक सरल लेकिन शक्तिशाली तरीका प्रदान करते हैं।

Web modular API

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

const storage = getStorage();
const storageRef = ref(storage, 'images/rivers.jpg');

const uploadTask = uploadBytesResumable(storageRef, file);

// Register three observers:
// 1. 'state_changed' observer, called any time the state changes
// 2. Error observer, called on failure
// 3. Completion observer, called on successful completion
uploadTask.on('state_changed', 
  (snapshot) => {
    // Observe state change events such as progress, pause, and resume
    // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
    const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
    console.log('Upload is ' + progress + '% done');
    switch (snapshot.state) {
      case 'paused':
        console.log('Upload is paused');
        break;
      case 'running':
        console.log('Upload is running');
        break;
    }
  }, 
  (error) => {
    // Handle unsuccessful uploads
  }, 
  () => {
    // Handle successful uploads on complete
    // For instance, get the download URL: https://firebasestorage.googleapis.com/...
    getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => {
      console.log('File available at', downloadURL);
    });
  }
);

Web namespaced API

var uploadTask = storageRef.child('images/rivers.jpg').put(file);

// Register three observers:
// 1. 'state_changed' observer, called any time the state changes
// 2. Error observer, called on failure
// 3. Completion observer, called on successful completion
uploadTask.on('state_changed', 
  (snapshot) => {
    // Observe state change events such as progress, pause, and resume
    // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
    var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
    console.log('Upload is ' + progress + '% done');
    switch (snapshot.state) {
      case firebase.storage.TaskState.PAUSED: // or 'paused'
        console.log('Upload is paused');
        break;
      case firebase.storage.TaskState.RUNNING: // or 'running'
        console.log('Upload is running');
        break;
    }
  }, 
  (error) => {
    // Handle unsuccessful uploads
  }, 
  () => {
    // Handle successful uploads on complete
    // For instance, get the download URL: https://firebasestorage.googleapis.com/...
    uploadTask.snapshot.ref.getDownloadURL().then((downloadURL) => {
      console.log('File available at', downloadURL);
    });
  }
);

त्रुटि प्रबंधन

अपलोड करते समय त्रुटियाँ होने के कई कारण हो सकते हैं, जिनमें स्थानीय फ़ाइल का मौजूदा न होना, या उपयोगकर्ता के पास वांछित फ़ाइल अपलोड करने की अनुमति न होना शामिल है। त्रुटियों के बारे में अधिक जानकारी डॉक्स के हैंडल एरर्स अनुभाग में पाई जा सकती है।

पूर्ण उदाहरण

प्रगति निगरानी और त्रुटि प्रबंधन के साथ अपलोड का एक पूरा उदाहरण नीचे दिखाया गया है:

Web modular API

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

const storage = getStorage();

// Create the file metadata
/** @type {any} */
const metadata = {
  contentType: 'image/jpeg'
};

// Upload file and metadata to the object 'images/mountains.jpg'
const storageRef = ref(storage, 'images/' + file.name);
const uploadTask = uploadBytesResumable(storageRef, file, metadata);

// Listen for state changes, errors, and completion of the upload.
uploadTask.on('state_changed',
  (snapshot) => {
    // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
    const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
    console.log('Upload is ' + progress + '% done');
    switch (snapshot.state) {
      case 'paused':
        console.log('Upload is paused');
        break;
      case 'running':
        console.log('Upload is running');
        break;
    }
  }, 
  (error) => {
    // A full list of error codes is available at
    // https://firebase.google.com/docs/storage/web/handle-errors
    switch (error.code) {
      case 'storage/unauthorized':
        // User doesn't have permission to access the object
        break;
      case 'storage/canceled':
        // User canceled the upload
        break;

      // ...

      case 'storage/unknown':
        // Unknown error occurred, inspect error.serverResponse
        break;
    }
  }, 
  () => {
    // Upload completed successfully, now we can get the download URL
    getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => {
      console.log('File available at', downloadURL);
    });
  }
);

Web namespaced API

// Create the file metadata
var metadata = {
  contentType: 'image/jpeg'
};

// Upload file and metadata to the object 'images/mountains.jpg'
var uploadTask = storageRef.child('images/' + file.name).put(file, metadata);

// Listen for state changes, errors, and completion of the upload.
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, // or 'state_changed'
  (snapshot) => {
    // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
    var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
    console.log('Upload is ' + progress + '% done');
    switch (snapshot.state) {
      case firebase.storage.TaskState.PAUSED: // or 'paused'
        console.log('Upload is paused');
        break;
      case firebase.storage.TaskState.RUNNING: // or 'running'
        console.log('Upload is running');
        break;
    }
  }, 
  (error) => {
    // A full list of error codes is available at
    // https://firebase.google.com/docs/storage/web/handle-errors
    switch (error.code) {
      case 'storage/unauthorized':
        // User doesn't have permission to access the object
        break;
      case 'storage/canceled':
        // User canceled the upload
        break;

      // ...

      case 'storage/unknown':
        // Unknown error occurred, inspect error.serverResponse
        break;
    }
  }, 
  () => {
    // Upload completed successfully, now we can get the download URL
    uploadTask.snapshot.ref.getDownloadURL().then((downloadURL) => {
      console.log('File available at', downloadURL);
    });
  }
);

अब जब आपने फ़ाइलें अपलोड कर दी हैं, तो आइए जानें कि उन्हें क्लाउड स्टोरेज से कैसे डाउनलोड किया जाए