Lấy kích thước hình ảnh nodejs

Cập nhật mọi thông tin được công bố tại Hội nghị thượng đỉnh Firebase và tìm hiểu cách Firebase có thể giúp bạn đẩy nhanh quá trình phát triển ứng dụng cũng như tự tin chạy ứng dụng của mình. Tìm hiểu thêm

  • căn cứ hỏa lực
  • Tài liệu
  • Lưu trữ đám mây cho Firebase
  • Xây dựng

Gửi phản hồi Tải xuống tệp bằng Cloud Storage trên web Sắp xếp ngăn nắp với các bộ sưu tập Lưu và phân loại nội dung dựa trên sở thích của bạn

Lưu trữ đám mây cho Firebase cho phép bạn tải xuống tệp một cách nhanh chóng và dễ dàng từ bộ chứa Lưu trữ đám mây do Firebase cung cấp và quản lý

Ghi chú. Theo mặc định, bộ chứa Cloud Storage yêu cầu Xác thực Firebase để thực hiện bất kỳ hành động nào đối với dữ liệu hoặc tệp của bộ chứa. Bạn có thể thay đổi Quy tắc bảo mật Firebase cho Cloud Storage để cho phép truy cập trái phép. Vì Firebase và ứng dụng App Engine mặc định của dự án của bạn chia sẻ nhóm này, nên việc định cấu hình quyền truy cập công khai cũng có thể khiến các tệp App Engine mới tải lên có thể truy cập công khai. Đảm bảo hạn chế quyền truy cập vào bộ chứa Lưu trữ đám mây của bạn một lần nữa khi bạn thiết lập Xác thực.

Tạo một tài liệu tham khảo

Để tải xuống tệp, trước tiên hãy tạo tham chiếu Lưu trữ đám mây cho tệp bạn muốn tải xuống

Bạn có thể tạo tham chiếu bằng cách nối thêm các đường dẫn con vào thư mục gốc của bộ chứa Cloud Storage hoặc bạn có thể tạo tham chiếu từ URL

// 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');  
3 hoặc
// 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');  

4 hiện có tham chiếu đến một đối tượng trong Cloud Storage

Phiên bản web 9

Tìm hiểu thêm về SDK mô-đun Web v9 có thể rung chuyển dạng cây và nâng cấp từ phiên bản 8.
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');  

Phiên bản web 8

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

Tải xuống dữ liệu qua URL

Bạn có thể lấy URL tải xuống cho một tệp bằng cách gọi phương thức

// 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');  
5 trên tham chiếu Lưu trữ đám mây

Phiên bản web 9

Tìm hiểu thêm về SDK mô-đun Web v9 có thể rung chuyển dạng cây và nâng cấp từ phiên bản 8.
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
  });

Phiên bản web 8

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

Tải xuống dữ liệu trực tiếp từ SDK

Từ phiên bản 9. 5 trở lên, SDK cung cấp các chức năng này để tải xuống trực tiếp

  • // 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');  
    0
  • // 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');  

    1
  • // 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');  
    2

Sử dụng các chức năng này, bạn có thể bỏ qua việc tải xuống từ một URL và thay vào đó trả về dữ liệu trong mã của bạn. Điều này cho phép kiểm soát truy cập chi tiết hơn thông qua Quy tắc bảo mật Firebase

Ghi chú.
// 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');  
2 chỉ khả dụng cho Nút. js và
// 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');  
0 chỉ khả dụng cho các môi trường giống như trình duyệt.

Cấu hình CORS

Để tải xuống dữ liệu trực tiếp trong trình duyệt, bạn phải định cấu hình bộ chứa Lưu trữ đám mây của mình để truy cập trên nhiều nguồn gốc (CORS). Điều này có thể được thực hiện với công cụ dòng lệnh

// 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');  
5 mà bạn có thể cài đặt từ đây

Nếu bạn không muốn có bất kỳ hạn chế nào dựa trên miền (trường hợp phổ biến nhất), hãy sao chép JSON này vào một tệp có tên

// 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');  
6

// 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');  
1

Chạy

// 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');  
7 để triển khai các hạn chế này

Để biết thêm thông tin, hãy tham khảo tài liệu Google Cloud Storage

Xử lý lỗi

Có một số lý do khiến lỗi có thể xảy ra khi tải xuống, bao gồm tệp không tồn tại hoặc người dùng không có quyền truy cập tệp mong muốn. Bạn có thể tìm thêm thông tin về lỗi trong phần Xử lý lỗi của tài liệu