Sử dụng siêu dữ liệu tệp bằng Cloud Storage trên web

Sau khi tải một tệp lên Cloud Storage, bạn cũng có thể nhận hoặc cập nhật siêu dữ liệu của tệp, chẳng hạn như cập nhật loại nội dung. Các tệp cũng có thể lưu trữ các cặp khoá/giá trị tuỳ chỉnh cùng với siêu dữ liệu bổ sung của tệp.

Lấy siêu dữ liệu của tệp

Siêu dữ liệu tệp chứa các thuộc tính phổ biến như name, sizecontentType (thường được gọi là loại MIME) ngoài một số thuộc tính ít phổ biến hơn như contentDispositiontimeCreated. Bạn có thể truy xuất siêu dữ liệu này từ một tệp tham chiếu Cloud Storage bằng phương thức getMetadata(). getMetadata() trả về một Promise chứa siêu dữ liệu hoàn chỉnh hoặc một lỗi nếu Promise từ chối.

Web

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

// Create a reference to the file whose metadata we want to retrieve
const storage = getStorage();
const forestRef = ref(storage, 'images/forest.jpg');

// Get metadata properties
getMetadata(forestRef)
  .then((metadata) => {
    // Metadata now contains the metadata for 'images/forest.jpg'
  })
  .catch((error) => {
    // Uh-oh, an error occurred!
  });

Web

// Create a reference to the file whose metadata we want to retrieve
var forestRef = storageRef.child('images/forest.jpg');

// Get metadata properties
forestRef.getMetadata()
  .then((metadata) => {
    // Metadata now contains the metadata for 'images/forest.jpg'
  })
  .catch((error) => {
    // Uh-oh, an error occurred!
  });

Cập nhật siêu dữ liệu của tệp

Bạn có thể cập nhật siêu dữ liệu của tệp bất cứ lúc nào sau khi quá trình tải tệp lên hoàn tất bằng cách sử dụng phương thức updateMetadata(). Hãy tham khảo danh sách đầy đủ để biết thêm thông tin về những thuộc tính có thể được cập nhật. Chỉ những thuộc tính được chỉ định trong siêu dữ liệu mới được cập nhật, tất cả các thuộc tính khác đều không thay đổi. updateMetadata() trả về một Promise chứa đầy đủ siêu dữ liệu hoặc một lỗi nếu Promise từ chối.

Web

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

// Create a reference to the file whose metadata we want to change
const storage = getStorage();
const forestRef = ref(storage, 'images/forest.jpg');

// Create file metadata to update
const newMetadata = {
  cacheControl: 'public,max-age=300',
  contentType: 'image/jpeg'
};

// Update metadata properties
updateMetadata(forestRef, newMetadata)
  .then((metadata) => {
    // Updated metadata for 'images/forest.jpg' is returned in the Promise
  }).catch((error) => {
    // Uh-oh, an error occurred!
  });

Web

// Create a reference to the file whose metadata we want to change
var forestRef = storageRef.child('images/forest.jpg');

// Create file metadata to update
var newMetadata = {
  cacheControl: 'public,max-age=300',
  contentType: 'image/jpeg'
};

// Update metadata properties
forestRef.updateMetadata(newMetadata)
  .then((metadata) => {
    // Updated metadata for 'images/forest.jpg' is returned in the Promise
  }).catch((error) => {
    // Uh-oh, an error occurred!
  });

Bạn có thể xoá một thuộc tính siêu dữ liệu bằng cách đặt thuộc tính đó thành null:

Web

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

const storage = getStorage();
const forestRef = ref(storage, 'images/forest.jpg');

// Create file metadata with property to delete
const deleteMetadata = {
  contentType: null
};

// Delete the metadata property
updateMetadata(forestRef, deleteMetadata)
  .then((metadata) => {
    // metadata.contentType should be null
  }).catch((error) => {
    // Uh-oh, an error occurred!
  });

Web

// Create file metadata with property to delete
var deleteMetadata = {
  contentType: null
};

// Delete the metadata property
forestRef.updateMetadata(deleteMetadata)
  .then((metadata) => {
    // metadata.contentType should be null
  }).catch((error) => {
    // Uh-oh, an error occurred!
  });

Xử lý lỗi

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

Siêu dữ liệu tuỳ chỉnh

Bạn có thể chỉ định siêu dữ liệu tuỳ chỉnh dưới dạng một đối tượng chứa các thuộc tính String.

Web

const metadata = {
  customMetadata: {
    'location': 'Yosemite, CA, USA',
    'activity': 'Hiking'
  }
};

Web

var metadata = {
  customMetadata: {
    'location': 'Yosemite, CA, USA',
    'activity': 'Hiking'
  }
};

Bạn có thể sử dụng siêu dữ liệu tuỳ chỉnh để lưu trữ dữ liệu bổ sung dành riêng cho ứng dụng cho từng tệp, nhưng bạn nên sử dụng cơ sở dữ liệu (chẳng hạn như Firebase Realtime Database) để lưu trữ và đồng bộ hoá loại dữ liệu này.

Thuộc tính siêu dữ liệu của tệp

Sau đây là danh sách đầy đủ các thuộc tính siêu dữ liệu của một tệp:

Tài sản Loại Có thể ghi
bucket chuỗi KHÔNG
generation chuỗi KHÔNG
metageneration chuỗi KHÔNG
fullPath chuỗi KHÔNG
name chuỗi KHÔNG
size số KHÔNG
timeCreated chuỗi KHÔNG
updated chuỗi KHÔNG
md5Hash chuỗi CÓ khi tải lên, KHÔNG khi updateMetadata
cacheControl chuỗi
contentDisposition chuỗi
contentEncoding chuỗi
contentLanguage chuỗi
contentType chuỗi
customMetadata Đối tượng chứa các ánh xạ chuỗi->chuỗi

Việc tải lên, tải xuống và cập nhật tệp là rất quan trọng, nhưng việc có thể xoá tệp cũng quan trọng không kém. Hãy tìm hiểu cách xoá tệp khỏi Cloud Storage.