Cloud Storage for Firebase 可讓您列出 Cloud Storage 值區SDK 會同時傳回項目和 物件。
使用 List API 的專案必須搭配 Cloud Storage for Firebase 規則版本 2.如果您目前有 Firebase 專案,請按照 安全性規則指南。
list()
會使用
Google Cloud Storage List API。
在 Cloud Storage for Firebase 中,我們使用 /
做為分隔符號,以便:
模擬檔案系統語意為了有效率地週遊大型
階層式 Cloud Storage 值區,List API 會傳回前置字串
不同的項目舉例來說,如果您上傳一個檔案 /images/uid/file1
,
root.child('images').listAll()
會傳回/images/uid
做為前置字元。root.child('images/uid').listAll()
會將檔案視為項目傳回。
Cloud Storage for Firebase SDK 不會傳回包含兩個
連續 /
,或是以 /
結尾。舉例來說,假設值區有
下列物件:
correctPrefix/happyItem
wrongPrefix//sadItem
lonelyItem/
針對這個值區中的項目執行清單作業,會傳回下列結果:
- 根層級的清單作業會傳回
correctPrefix
的參照。wrongPrefix
,lonelyItem
以prefixes
格式顯示。 correctPrefix/
的清單作業會傳回對 以items
格式correctPrefix/happyItem
。wrongPrefix/
中的清單作業不會傳回任何參照 因為wrongPrefix//sadItem
包含兩個連續的/
。lonelyItem/
中的清單作業不會傳回任何參照 因為物件lonelyItem/
的結尾是/
。
列出所有檔案
您可以使用 listAll
擷取目錄的所有結果。
這最適合用於小型目錄,因為所有結果都會在記憶體中緩衝處理。
如果新增物件或
未達成協議
如果是大型清單,請使用分頁化的 list()
方法,做為 listAll()
緩衝區,
會產生記憶體
以下範例示範 listAll
。
Kotlin+KTX
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! } });
分頁清單結果
list()
API 會限制傳回的結果數量。list()
可提供一致的網頁瀏覽,並公開可讓您掌控
何時該擷取額外結果
pageToken 會對 上一個結果。在後續使用 pageToken 的後續要求中, 顯示有關
以下範例說明如何將結果分頁:
Kotlin+KTX
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. } }); }
處理錯誤
如未升級,list()
和listAll()
會失敗
升級至第 2 版的安全性規則這時請升級安全性規則 (如果有的話)
錯誤:
Listing objects in a bucket is disallowed for rules_version = "1".
Please update storage security rules to rules_version = "2" to use list.
其他可能的錯誤則代表使用者沒有適當權限。 如要進一步瞭解錯誤,請前往 處理錯誤。