ดาวน์โหลดไฟล์ด้วย Cloud Storage บนเว็บ

Cloud Storage for Firebase ช่วยให้คุณดาวน์โหลดไฟล์จากที่เก็บข้อมูล Cloud Storage ที่ให้บริการและจัดการโดย Firebase ได้อย่างรวดเร็วและง่ายดาย

สร้างการอ้างอิง

หากต้องการดาวน์โหลดไฟล์ ให้สร้างข้อมูลอ้างอิง Cloud Storage ไปยังไฟล์ที่ต้องการดาวน์โหลดก่อน

คุณสร้างข้อมูลอ้างอิงได้โดยการเพิ่มเส้นทางย่อยต่อท้ายรูทของที่เก็บข้อมูล Cloud Storage หรือสร้างข้อมูลอ้างอิงจาก URL ของ gs:// หรือ https:// ที่มีอยู่ซึ่งอ้างอิงออบเจ็กต์ใน Cloud Storage

Web Modular API

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

// Create a reference with an initial file path and name
const storage = getStorage();
const pathReference = ref(storage, 'images/stars.jpg');

// Create a reference from a Google Cloud Storage URI
const gsReference = ref(storage, 'gs://bucket/images/stars.jpg');

// Create a reference from an HTTPS URL
// Note that in the URL, characters are URL escaped!
const httpsReference = ref(storage, 'https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg');  

API ที่ใช้เนมสเปซในเว็บ

// Create a reference with an initial file path and name
var storage = firebase.storage();
var pathReference = storage.ref('images/stars.jpg');

// Create a reference from a Google Cloud Storage URI
var gsReference = storage.refFromURL('gs://bucket/images/stars.jpg');

// Create a reference from an HTTPS URL
// Note that in the URL, characters are URL escaped!
var httpsReference = storage.refFromURL('https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg');  

ดาวน์โหลดข้อมูลผ่าน URL

คุณรับ URL การดาวน์โหลดสำหรับไฟล์ได้ด้วยการเรียกใช้เมธอด getDownloadURL() ในข้อมูลอ้างอิงของ Cloud Storage

Web Modular API

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

const storage = getStorage();
getDownloadURL(ref(storage, 'images/stars.jpg'))
  .then((url) => {
    // `url` is the download URL for 'images/stars.jpg'

    // This can be downloaded directly:
    const xhr = new XMLHttpRequest();
    xhr.responseType = 'blob';
    xhr.onload = (event) => {
      const blob = xhr.response;
    };
    xhr.open('GET', url);
    xhr.send();

    // Or inserted into an <img> element
    const img = document.getElementById('myimg');
    img.setAttribute('src', url);
  })
  .catch((error) => {
    // Handle any errors
  });

API ที่ใช้เนมสเปซในเว็บ

storageRef.child('images/stars.jpg').getDownloadURL()
  .then((url) => {
    // `url` is the download URL for 'images/stars.jpg'

    // This can be downloaded directly:
    var xhr = new XMLHttpRequest();
    xhr.responseType = 'blob';
    xhr.onload = (event) => {
      var blob = xhr.response;
    };
    xhr.open('GET', url);
    xhr.send();

    // Or inserted into an <img> element
    var img = document.getElementById('myimg');
    img.setAttribute('src', url);
  })
  .catch((error) => {
    // Handle any errors
  });

ดาวน์โหลดข้อมูลจาก SDK โดยตรง

SDK มีฟังก์ชันต่อไปนี้สำหรับการดาวน์โหลดโดยตรงตั้งแต่เวอร์ชัน 9.5 ขึ้นไป

เมื่อใช้ฟังก์ชันเหล่านี้ คุณสามารถข้ามการดาวน์โหลดจาก URL และแสดงข้อมูลในโค้ดของคุณแทนได้ ซึ่งช่วยให้ควบคุมการเข้าถึงได้ละเอียดยิ่งขึ้นผ่านกฎความปลอดภัยของ Firebase

การกำหนดค่า CORS

หากต้องการดาวน์โหลดข้อมูลในเบราว์เซอร์โดยตรง คุณต้องกำหนดค่าที่เก็บข้อมูล Cloud Storage สำหรับการเข้าถึงข้ามต้นทาง (CORS) ซึ่งทำได้ด้วยเครื่องมือบรรทัดคำสั่ง gsutil ซึ่งจะติดตั้งจากที่นี่

หากไม่ต้องการข้อจำกัดใดๆ ตามโดเมน (กรณีทั่วไป) ให้คัดลอก JSON นี้ไปยังไฟล์ชื่อ cors.json:

[
  {
    "origin": ["*"],
    "method": ["GET"],
    "maxAgeSeconds": 3600
  }
]

เรียกใช้ gsutil cors set cors.json gs://<your-cloud-storage-bucket> เพื่อทำให้ข้อจำกัดเหล่านี้ใช้งานได้

ดูข้อมูลเพิ่มเติมได้ที่เอกสารประกอบของ Google Cloud Storage

จัดการข้อผิดพลาด

การเกิดข้อผิดพลาดขึ้นขณะดาวน์โหลดมีสาเหตุหลายประการ ได้แก่ การที่ไฟล์ไม่มีอยู่ หรือผู้ใช้ไม่มีสิทธิ์เข้าถึงไฟล์ที่ต้องการ ดูข้อมูลเพิ่มเติมเกี่ยวกับข้อผิดพลาดได้ในส่วนจัดการข้อผิดพลาดของเอกสาร

ตัวอย่างแบบเต็ม

ตัวอย่างแบบเต็มของการดาวน์โหลดที่มีการจัดการข้อผิดพลาดแสดงอยู่ด้านล่าง

Web Modular API

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

// Create a reference to the file we want to download
const storage = getStorage();
const starsRef = ref(storage, 'images/stars.jpg');

// Get the download URL
getDownloadURL(starsRef)
  .then((url) => {
    // Insert url into an <img> tag to "download"
  })
  .catch((error) => {
    // A full list of error codes is available at
    // https://firebase.google.com/docs/storage/web/handle-errors
    switch (error.code) {
      case 'storage/object-not-found':
        // File doesn't exist
        break;
      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 the server response
        break;
    }
  });

API ที่ใช้เนมสเปซในเว็บ

// Create a reference to the file we want to download
var starsRef = storageRef.child('images/stars.jpg');

// Get the download URL
starsRef.getDownloadURL()
.then((url) => {
  // Insert url into an <img> tag to "download"
})
.catch((error) => {
  // A full list of error codes is available at
  // https://firebase.google.com/docs/storage/web/handle-errors
  switch (error.code) {
    case 'storage/object-not-found':
      // File doesn't exist
      break;
    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 the server response
      break;
  }
});

นอกจากนี้คุณยังรับหรืออัปเดตข้อมูลเมตาสำหรับไฟล์ที่เก็บไว้ใน Cloud Storage ได้ด้วย