在 Apple 平台上使用 Cloud Storage 列出檔案

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 的參照。 wrongPrefixlonelyItemprefixes 格式顯示。
  • correctPrefix/ 的清單作業會傳回對 以 items 格式 correctPrefix/happyItem
  • wrongPrefix/ 中的清單作業不會傳回任何參照 因為 wrongPrefix//sadItem 包含兩個連續的 /
  • lonelyItem/ 中的清單作業不會傳回任何參照 因為物件 lonelyItem/ 的結尾是 /

列出所有檔案

您可以使用 listAll(completion:) 擷取目錄的所有結果。 這最適合用於小型目錄,因為所有結果都會在記憶體中緩衝處理。 如果新增物件或 未達成協議

如果是大型清單,請使用分頁化的 list(withMaxResults:completion:) 方法, listAll(completion:) 會緩衝所有結果在記憶體中。

以下範例示範 listAll(completion:)

Swift

let storageReference = storage.reference().child("files/uid")
do {
  let result = try await storageReference.listAll()
  for prefix in result.prefixes {
    // The prefixes under storageReference.
    // You may call listAll(completion:) recursively on them.
  }
  for item in result.items {
    // The items under storageReference.
  }
} catch {
  // ...
}

Objective-C

FIRStorageReference *storageReference = [storage reference];
[storageReference listAllWithCompletion:^(FIRStorageListResult *result, NSError *error) {
  if (error != nil) {
    // ...
  }

  for (FIRStorageReference *prefix in result.prefixes) {
    // All the prefixes under storageReference.
    // You may call listAllWithCompletion: recursively on them.
  }
  for (FIRStorageReference *item in result.items) {
    // All items under storageReference.
  }
}];

分頁清單結果

list(withMaxResults:completion:) API 設有 傳回的結果list(withMaxResults:completion) 提供具有一致性的 網頁瀏��並公開 pageToken,即可控制擷取時間 並進行更多結果

pageToken 會對 上一個結果。在後續使用 pageToken 的後續要求中, 顯示有關

以下範例說明如何將結果分頁:

Swift

func listAllPaginated(pageToken: String? = nil) async throws {
  let storage = Storage.storage()
  let storageReference = storage.reference().child("files/uid")

  let listResult: StorageListResult
  if let pageToken = pageToken {
    listResult = try await storageReference.list(maxResults: 100, pageToken: pageToken)
  } else {
    listResult = try await storageReference.list(maxResults: 100)
  }
  let prefixes = listResult.prefixes
  let items = listResult.items
  // Handle list result
  // ...

  // Process next page
  if let token = listResult.pageToken {
    try await listAllPaginated(pageToken: token)
  }
}

Objective-C

- (void)paginateFilesAtReference:(FIRStorageReference *)reference
                       pageToken:(nullable NSString *)pageToken {
  void (^pageHandler)(FIRStorageListResult *_Nonnull, NSError *_Nullable) =
      ^(FIRStorageListResult *result, NSError *error) {
        if (error != nil) {
          // ...
        }
        NSArray *prefixes = result.prefixes;
        NSArray *items = result.items;

        // ...

        // Process next page
        if (result.pageToken != nil) {
          [self paginateFilesAtReference:reference pageToken:result.pageToken];
        }
  };

  if (pageToken != nil) {
    [reference listWithMaxResults:100 pageToken:pageToken completion:pageHandler];
  } else {
    [reference listWithMaxResults:100 completion:pageHandler];
  }
}

處理錯誤

如果您尚未將安全性規則升級為 2.如果看到這則錯誤訊息,請升級安全性規則:

Listing objects in a bucket is disallowed for rules_version = "1".
Please update storage security rules to rules_version = "2" to use list.

其他可能的錯誤表示使用者沒有適當的權限。 如要進一步瞭解錯誤,請前往 處理錯誤