使用 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,则:
letstorageReference=storage.reference().child("files/uid")do{letresult=tryawaitstorageReference.listAll()forprefixinresult.prefixes{// The prefixes under storageReference.// You may call listAll(completion:) recursively on them.}foriteminresult.items{// The items under storageReference.}}catch{// ...}
FIRStorageReference*storageReference=[storagereference];[storageReferencelistAllWithCompletion:^(FIRStorageListResult*result,NSError*error){if(error!=nil){// ...}for(FIRStorageReference*prefixinresult.prefixes){// All the prefixes under storageReference.// You may call listAllWithCompletion: recursively on them.}for(FIRStorageReference*iteminresult.items){// All items under storageReference.}}];
funclistAllPaginated(pageToken:String?=nil)asyncthrows{letstorage=Storage.storage()letstorageReference=storage.reference().child("files/uid")letlistResult:StorageListResultifletpageToken=pageToken{listResult=tryawaitstorageReference.list(maxResults:100,pageToken:pageToken)}else{listResult=tryawaitstorageReference.list(maxResults:100)}letprefixes=listResult.prefixesletitems=listResult.items// Handle list result// ...// Process next pageiflettoken=listResult.pageToken{tryawaitlistAllPaginated(pageToken:token)}}
-(void)paginateFilesAtReference:(FIRStorageReference*)referencepageToken:(nullableNSString*)pageToken{void(^pageHandler)(FIRStorageListResult*_Nonnull,NSError*_Nullable)=^(FIRStorageListResult*result,NSError*error){if(error!=nil){// ...}NSArray*prefixes=result.prefixes;NSArray*items=result.items;// ...// Process next pageif(result.pageToken!=nil){[selfpaginateFilesAtReference:referencepageToken:result.pageToken];}};if(pageToken!=nil){[referencelistWithMaxResults:100pageToken:pageTokencompletion:pageHandler];}else{[referencelistWithMaxResults:100completion:pageHandler];}}
[null,null,["最后更新时间 (UTC):2025-08-22。"],[],[],null,["\u003cbr /\u003e\n\nCloud Storage for Firebase allows you to list the contents of your\nCloud Storage bucket. The SDKs return both the items and the prefixes\nof objects under the current Cloud Storage reference.\n\nProjects that use the list API require Cloud Storage for Firebase\nRules version 2. If you have an existing Firebase project, follow the steps in\nthe [Security Rules Guide](/docs/storage/security/core-syntax).\n| **Note:** The list API is only allowed for Rules version 2. In Rules version 2, `allow read` is the shorthand for `allow get, list`.\n\n`list()` uses the\n[Google Cloud Storage List API](//cloud.google.com/storage/docs/json_api/v1/objects/list).\nIn Cloud Storage for Firebase, we use `/` as a delimiter, which allows us to\nemulate file system semantics. To allow for efficient traversal of large,\nhierarchical Cloud Storage buckets, the List API returns prefixes and items\nseparately. For example, if you upload one file `/images/uid/file1`,\n\n- `root.child('images').listAll()` will return `/images/uid` as a prefix.\n- `root.child('images/uid').listAll()` will return the file as an item.\n\nThe Cloud Storage for Firebase SDK does not return object paths that contain two\nconsecutive `/`s or end with a `/.`. For example, consider a bucket that has the\nfollowing objects:\n\n- `correctPrefix/happyItem`\n- `wrongPrefix//sadItem`\n- `lonelyItem/`\n\nThe list operations on items in this bucket will give the following results:\n\n- The list operation at the root returns the references to `correctPrefix`, `wrongPrefix` and `lonelyItem` as `prefixes`.\n- The list operation at the `correctPrefix/` returns the references to `correctPrefix/happyItem` as `items`.\n- The list operation at the `wrongPrefix/` doesn't return any references because `wrongPrefix//sadItem` contains two consecutive `/`s.\n- The list operation at the `lonelyItem/` doesn't return any references because the object `lonelyItem/` ends with `/`.\n\nList all files\n\nYou can use `listAll(completion:)` to fetch all results for a directory.\nThis is best used for small directories as all results are buffered in memory.\nThe operation also may not return a consistent snapshot if objects are added or\nremoved during the process.\n\nFor a large list, use the paginated `list(withMaxResults:completion:)` method as\n`listAll(completion:)` buffers all results in memory.\n\nThe following example demonstrates `listAll(completion:)`. \n\nSwift \n\n```swift\nlet storageReference = storage.reference().child(\"files/uid\")\ndo {\n let result = try await storageReference.listAll()\n for prefix in result.prefixes {\n // The prefixes under storageReference.\n // You may call listAll(completion:) recursively on them.\n }\n for item in result.items {\n // The items under storageReference.\n }\n} catch {\n // ...\n}https://github.com/firebase/snippets-ios/blob/cdce007fedb3bb90dd3a70ce03066178236e1deb/storage/StorageReferenceSwift/ViewController.swift#L620-L632\n```\n\nObjective-C \n\n```objective-c\nFIRStorageReference *storageReference = [storage reference];\n[storageReference listAllWithCompletion:^(FIRStorageListResult *result, NSError *error) {\n if (error != nil) {\n // ...\n }\n\n for (FIRStorageReference *prefix in result.prefixes) {\n // All the prefixes under storageReference.\n // You may call listAllWithCompletion: recursively on them.\n }\n for (FIRStorageReference *item in result.items) {\n // All items under storageReference.\n }\n}];https://github.com/firebase/snippets-ios/blob/cdce007fedb3bb90dd3a70ce03066178236e1deb/storage/StorageReference/ViewController.m#L627-L640\n```\n\nPaginate list results\n\nThe `list(withMaxResults:completion:)` API places a limit on the number of\nresults it returns. `list(withMaxResults:completion)` provides a consistent\npageview and exposes a pageToken that allows control over when to fetch\nadditional results.\n\nThe pageToken encodes the path and version of the last item returned in the\nprevious result. In a subsequent request using the pageToken, items that come\nafter the pageToken are shown.\n\nThe following example demonstrates paginating a result: \n\nSwift \n\n```swift\nfunc listAllPaginated(pageToken: String? = nil) async throws {\n let storage = Storage.storage()\n let storageReference = storage.reference().child(\"files/uid\")\n\n let listResult: StorageListResult\n if let pageToken = pageToken {\n listResult = try await storageReference.list(maxResults: 100, pageToken: pageToken)\n } else {\n listResult = try await storageReference.list(maxResults: 100)\n }\n let prefixes = listResult.prefixes\n let items = listResult.items\n // Handle list result\n // ...\n\n // Process next page\n if let token = listResult.pageToken {\n try await listAllPaginated(pageToken: token)\n }\n}https://github.com/firebase/snippets-ios/blob/cdce007fedb3bb90dd3a70ce03066178236e1deb/storage/StorageReferenceSwift/ViewController.swift#L637-L656\n```\n\nObjective-C \n\n```objective-c\n- (void)paginateFilesAtReference:(FIRStorageReference *)reference\n pageToken:(nullable NSString *)pageToken {\n void (^pageHandler)(FIRStorageListResult *_Nonnull, NSError *_Nullable) =\n ^(FIRStorageListResult *result, NSError *error) {\n if (error != nil) {\n // ...\n }\n NSArray *prefixes = result.prefixes;\n NSArray *items = result.items;\n\n // ...\n\n // Process next page\n if (result.pageToken != nil) {\n [self paginateFilesAtReference:reference pageToken:result.pageToken];\n }\n };\n\n if (pageToken != nil) {\n [reference listWithMaxResults:100 pageToken:pageToken completion:pageHandler];\n } else {\n [reference listWithMaxResults:100 completion:pageHandler];\n }\n}https://github.com/firebase/snippets-ios/blob/cdce007fedb3bb90dd3a70ce03066178236e1deb/storage/StorageReference/ViewController.m#L645-L668\n```\n\nHandle errors\n\nMethods in the list API will fail if you haven't upgraded your Security Rules to\nversion 2. Upgrade your Security Rules if you see this error: \n\n Listing objects in a bucket is disallowed for rules_version = \"1\".\n Please update storage security rules to rules_version = \"2\" to use list.\n\nOther possible errors may indicate the user does not have the right permissions.\nMore information on errors can be found in\n[Handle Errors](/docs/storage/ios/handle-errors)."]]