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

Sau khi tải tệp lên tài liệu tham khảo Cloud Storage, bạn cũng có thể lấy 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. 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 , sizecontentType (thường được gọi là loại MIME) bên cạnh một số thuộc tính ít phổ biến hơn như contentDispositiontimeCreated . 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() .

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 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 mới được cập nhật, tất cả các thuộc tính khác không được 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ể xóa thuộc tính siêu dữ liệu có thể ghi bằng cách chuyể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 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 vào 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 bằ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 tùy chỉnh 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

Danh sách đầy đủ các thuộc tính siêu dữ liệu trên một tệp có sẵn bên dưới:

Người nhận tài sản Kiểu Setter tồn tại
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 ĐÚNG
getContentDisposition String ĐÚNG
getContentEncoding String ĐÚNG
getContentLanguage String ĐÚNG
getContentType String ĐÚNG
getCustomMetadata String ĐÚNG
getCustomMetadataKeys Set<String> KHÔNG

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 quan trọng không kém. Hãy tìm hiểu cách xóa tệp khỏi Cloud Storage.