Sử dụng siêu dữ liệu tệp với Cloud Storage trên Android

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

Nhận siêu dữ liệu về 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ố ít hơn các số liệu phổ biến như contentDispositiontimeCreated. Siêu dữ liệu này có thể truy xuất từ tham chiếu Cloud Storage bằng cách sử dụng phương thức getMetadata().

Kotlin+KTX

// Create a storage reference from our app
val storageRef = storage.reference

// Get reference to the file
val forestRef = storageRef.child("images/forest.jpg")
forestRef.metadata.addOnSuccessListener { metadata ->
    // Metadata now contains the metadata for 'images/forest.jpg'
}.addOnFailureListener {
    // Uh-oh, an error occurred!
}

Java

// Create a storage reference from our app
StorageReference storageRef = storage.getReference();

// Get reference to the file
StorageReference forestRef = storageRef.child("images/forest.jpg");
forestRef.getMetadata().addOnSuccessListener(new OnSuccessListener<StorageMetadata>() {
    @Override
    public void onSuccess(StorageMetadata storageMetadata) {
        // Metadata now contains the metadata for 'images/forest.jpg'
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // 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 bằ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ỉ 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 mã khác vẫn không bị sửa đổi.

Kotlin+KTX

// Create a storage reference from our app
val storageRef = storage.reference

// Get reference to the file
val forestRef = storageRef.child("images/forest.jpg")
// Create file metadata including the content type
val metadata = storageMetadata {
    contentType = "image/jpg"
    setCustomMetadata("myCustomProperty", "myValue")
}

// Update metadata properties
forestRef.updateMetadata(metadata).addOnSuccessListener { updatedMetadata ->
    // Updated metadata is in updatedMetadata
}.addOnFailureListener {
    // Uh-oh, an error occurred!
}

Java

// Create a storage reference from our app
StorageReference storageRef = storage.getReference();

// Get reference to the file
StorageReference forestRef = storageRef.child("images/forest.jpg");
// Create file metadata including the content type
StorageMetadata metadata = new StorageMetadata.Builder()
        .setContentType("image/jpg")
        .setCustomMetadata("myCustomProperty", "myValue")
        .build();

// Update metadata properties
forestRef.updateMetadata(metadata)
        .addOnSuccessListener(new OnSuccessListener<StorageMetadata>() {
            @Override
            public void onSuccess(StorageMetadata storageMetadata) {
                // Updated metadata is in storageMetadata
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                // Uh-oh, an error occurred!
            }
        });

Bạn có thể xoá các thuộc tính siêu dữ liệu có thể ghi bằng cách truyền null:

Kotlin+KTX

// Create file metadata with property to delete
val metadata = storageMetadata {
    contentType = null
}

// Delete the metadata property
forestRef.updateMetadata(metadata).addOnSuccessListener { updatedMetadata ->
    // updatedMetadata.contentType should be null
}.addOnFailureListener {
    // Uh-oh, an error occurred!
}

Java

// Create file metadata with property to delete
StorageMetadata metadata = new StorageMetadata.Builder()
        .setContentType(null)
        .build();

// Delete the metadata property
forestRef.updateMetadata(metadata)
        .addOnSuccessListener(new OnSuccessListener<StorageMetadata>() {
            @Override
            public void onSuccess(StorageMetadata storageMetadata) {
                // metadata.contentType should be null
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                // 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 tải hoặc cập nhật siêu dữ liệu, 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ể xem thêm thông tin về các lỗi trong Xử lý lỗi của phần 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 bằng cách sử dụng phương thức setCustomMetadata() trong Lớp StorageMetadata.Builder.

Kotlin+KTX

val metadata = storageMetadata {
    setCustomMetadata("location", "Yosemite, CA, USA")
    setCustomMetadata("activity", "Hiking")
}

Java

StorageMetadata metadata = new StorageMetadata.Builder()
        .setCustomMetadata("location", "Yosemite, CA, USA")
        .setCustomMetadata("activity", "Hiking")
        .build();

Bạn có thể lưu trữ dữ liệu dành riêng cho ứng dụng cho từng tệp trong siêu dữ liệu tuỳ chỉnh, nhưng chúng tôi đề xuất sử dụng cơ sở dữ liệu (chẳng hạn như Cơ sở dữ liệu theo thời gian thực của Firebase) để 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

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:

Phương thức getter thuộc tính Loại Có phương thức setter
getBucket String KHÔNG
getGeneration String KHÔNG
getMetadataGeneration String KHÔNG
getPath String KHÔNG
getName String KHÔNG
getSizeBytes long KHÔNG
getCreationTimeMillis long KHÔNG
getUpdatedTimeMillis long KHÔNG
getMd5Hash String KHÔNG
getCacheControl String
getContentDisposition String
getContentEncoding String
getContentLanguage String
getContentType String
getCustomMetadata String
getCustomMetadataKeys Set<String> KHÔNG

Việc tải lên, tải xuống và cập nhật tệp rất quan trọng, nhưng khả năng để xoá chúng. Hãy tìm hiểu cách xoá tệp từ Cloud Storage.