Cloud Storage for Firebase cho phép bạn liệt kê nội dung trong nhóm Cloud Storage. Các SDK này trả về cả các mục và tiền tố của các đối tượng trong tham chiếu Cloud Storage hiện tại.
Các dự án sử dụng List API yêu cầu Cloud Storage for FirebaseRules phiên bản 2. Nếu bạn đang có một dự án Firebase, hãy làm theo các bước trong Hướng dẫn về quy tắc bảo mật.
list()
sử dụng Google Cloud Storage List API.
Trong Cloud Storage for Firebase, chúng ta sử dụng /
làm dấu phân cách, cho phép chúng ta mô phỏng ngữ nghĩa của hệ thống tệp. Để cho phép duyệt qua hiệu quả các vùng chứa Cloud Storage phân cấp lớn, List API sẽ trả về các tiền tố và mục riêng biệt. Ví dụ: nếu bạn tải một tệp /images/uid/file1
lên,
root.child('images').listAll()
sẽ trả về/images/uid
dưới dạng tiền tố.root.child('images/uid').listAll()
sẽ trả về tệp dưới dạng một mục.
SDK Cloud Storage for Firebase không trả về các đường dẫn đối tượng chứa hai /
liên tiếp hoặc kết thúc bằng /
. Ví dụ: hãy xem xét một vùng chứa có các đối tượng sau:
correctPrefix/happyItem
wrongPrefix//sadItem
lonelyItem/
Các thao tác liệt kê trên các mục trong nhóm này sẽ cho ra kết quả như sau:
- Thao tác liệt kê ở thư mục gốc sẽ trả về các tham chiếu đến
correctPrefix
,wrongPrefix
vàlonelyItem
dưới dạngprefixes
. - Thao tác danh sách tại
correctPrefix/
sẽ trả về các tham chiếu đếncorrectPrefix/happyItem
dưới dạngitems
. - Thao tác liệt kê tại
wrongPrefix/
không trả về bất kỳ thông tin tham chiếu nào vìwrongPrefix//sadItem
chứa hai/
liên tiếp. - Thao tác liệt kê tại
lonelyItem/
không trả về bất kỳ thông tin tham chiếu nào vì đối tượnglonelyItem/
kết thúc bằng/
.
Liệt kê tất cả các tệp
Bạn có thể dùng listAll
để tìm nạp tất cả kết quả cho một thư mục.
Phương thức này phù hợp nhất với các thư mục nhỏ vì tất cả kết quả đều được lưu vào bộ nhớ đệm.
Thao tác này cũng có thể không trả về một ảnh chụp nhanh nhất quán nếu các đối tượng được thêm hoặc xoá trong quá trình này.
Đối với danh sách lớn, hãy sử dụng phương thức list()
được phân trang vì listAll()
sẽ đệm tất cả kết quả trong bộ nhớ.
Ví dụ sau đây minh hoạ listAll
.
Kotlin
val storage = Firebase.storage val listRef = storage.reference.child("files/uid") // You'll need to import com.google.firebase.storage.component1 and // com.google.firebase.storage.component2 listRef.listAll() .addOnSuccessListener { (items, prefixes) -> for (prefix in prefixes) { // All the prefixes under listRef. // You may call listAll() recursively on them. } for (item in items) { // All the items under listRef. } } .addOnFailureListener { // Uh-oh, an error occurred! }
Java
StorageReference listRef = storage.getReference().child("files/uid"); listRef.listAll() .addOnSuccessListener(new OnSuccessListener<ListResult>() { @Override public void onSuccess(ListResult listResult) { for (StorageReference prefix : listResult.getPrefixes()) { // All the prefixes under listRef. // You may call listAll() recursively on them. } for (StorageReference item : listResult.getItems()) { // All the items under listRef. } } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Uh-oh, an error occurred! } });
Phân trang kết quả danh sách
API list()
giới hạn số lượng kết quả mà API này trả về. list()
cung cấp một lượt xem trang nhất quán và hiển thị một pageToken cho phép kiểm soát thời điểm tìm nạp kết quả bổ sung.
pageToken mã hoá đường dẫn và phiên bản của mục cuối cùng được trả về trong kết quả trước đó. Trong một yêu cầu tiếp theo bằng pageToken, các mục xuất hiện sau pageToken sẽ được hiển thị.
Ví dụ sau đây minh hoạ cách phân trang kết quả:
Kotlin
fun listAllPaginated(pageToken: String?) { val storage = Firebase.storage val listRef = storage.reference.child("files/uid") // Fetch the next page of results, using the pageToken if we have one. val listPageTask = if (pageToken != null) { listRef.list(100, pageToken) } else { listRef.list(100) } // You'll need to import com.google.firebase.storage.component1 and // com.google.firebase.storage.component2 listPageTask .addOnSuccessListener { (items, prefixes, pageToken) -> // Process page of results processResults(items, prefixes) // Recurse onto next page pageToken?.let { listAllPaginated(it) } }.addOnFailureListener { // Uh-oh, an error occurred. } }
Java
public void listAllPaginated(@Nullable String pageToken) { FirebaseStorage storage = FirebaseStorage.getInstance(); StorageReference listRef = storage.getReference().child("files/uid"); // Fetch the next page of results, using the pageToken if we have one. Task<ListResult> listPageTask = pageToken != null ? listRef.list(100, pageToken) : listRef.list(100); listPageTask .addOnSuccessListener(new OnSuccessListener<ListResult>() { @Override public void onSuccess(ListResult listResult) { List<StorageReference> prefixes = listResult.getPrefixes(); List<StorageReference> items = listResult.getItems(); // Process page of results // ... // Recurse onto next page if (listResult.getPageToken() != null) { listAllPaginated(listResult.getPageToken()); } } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Uh-oh, an error occurred. } }); }
Xử lý lỗi
list()
và listAll()
sẽ không thành công nếu bạn chưa nâng cấp Quy tắc bảo mật lên phiên bản 2. Nâng cấp Quy tắc bảo mật nếu bạn thấy lỗi này:
Listing objects in a bucket is disallowed for rules_version = "1".
Please update storage security rules to rules_version = "2" to use list.
Các lỗi có thể xảy ra khác có thể cho biết người dùng không có quyền phù hợp. Bạn có thể xem thêm thông tin về các lỗi trong phần Xử lý lỗi.