Sau khi tải tệp lên tham chiếu Cloud Storage, bạn cũng có thể lấy 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 khóa/giá trị tùy chỉnh với siêu dữ liệu tệp bổ sung.
Nhận siêu dữ liệu tệp
Siêu dữ liệu tệp chứa các thuộc tính phổ biến như name
, size
và contentType
(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ư contentDisposition
và timeCreated
. Siêu dữ liệu này có thể được truy xuất từ 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 modular API
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 namespaced API
// 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 tệp
Bạn có thể cập nhật siêu dữ liệu tệp bất kỳ 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()
. 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ỉ các thuộc tính được chỉ định trong siêu dữ liệu được cập nhật, tất cả các thuộc tính khác không được sửa đổi. updateMetadata()
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 modular API
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 namespaced API
// 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ể xóa một thuộc tính siêu dữ liệu bằng cách đặt nó thành null
:
Web modular API
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 namespaced API
// 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ả 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.
Siêu dữ liệu tùy chỉnh
Bạn có thể chỉ định siêu dữ liệu tùy chỉnh làm đối tượng chứa các thuộc tính String
.
Web modular API
const metadata = { customMetadata: { 'location': 'Yosemite, CA, USA', 'activity': 'Hiking' } };
Web namespaced API
var metadata = { customMetadata: { 'location': 'Yosemite, CA, USA', 'activity': 'Hiking' } };
Bạn có thể sử dụng siêu dữ liệu tùy chỉnh để lưu trữ dữ liệu cụ thể của ứng dụng bổ sung cho từng tệp nhưng chúng tôi khuyên bạn nên sử dụng cơ sở dữ liệu (chẳng hạn như Cơ sở dữ liệu thời gian thực Firebase ) để lưu trữ và đồng bộ hóa loại dữ liệu này.
Thuộc tính siêu dữ liệu tệp
Dưới đây là danh sách đầy đủ các thuộc tính siêu dữ liệu trên một tệp:
Tài sản | Kiểu | Có thể ghi |
---|---|---|
bucket | sợi dây | KHÔNG |
generation | sợi dây | KHÔNG |
metageneration | sợi dây | KHÔNG |
fullPath | sợi dây | KHÔNG |
name | sợi dây | KHÔNG |
size | con số | KHÔNG |
timeCreated | sợi dây | KHÔNG |
updated | sợi dây | KHÔNG |
md5Hash | sợi dây | CÓ khi tải lên, KHÔNG khi cập nhậtMetadata |
cacheControl | sợi dây | ĐÚNG |
contentDisposition | sợi dây | ĐÚNG |
contentEncoding | sợi dây | ĐÚNG |
contentLanguage | sợi dây | ĐÚNG |
contentType | sợi dây | ĐÚNG |
customMetadata | Đối tượng chứa chuỗi-> ánh xạ chuỗi | ĐÚNG |
Việc tải lên, tải xuống và cập nhật tệp là quan trọng, nhưng việc có thể xóa chúng cũng vậy. Hãy tìm hiểu cách xóa tệp khỏi Cloud Storage.