使用 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,则:
import{getStorage,ref,listAll}from"firebase/storage";conststorage=getStorage();// Create a reference under which you want to listconstlistRef=ref(storage,'files/uid');// Find all the prefixes and items.listAll(listRef).then((res)=>{res.prefixes.forEach((folderRef)=>{// All the prefixes under listRef.// You may call listAll() recursively on them.});res.items.forEach((itemRef)=>{// All the items under listRef.});}).catch((error)=>{// Uh-oh, an error occurred!});
// Create a reference under which you want to listvarlistRef=storageRef.child('files/uid');// Find all the prefixes and items.listRef.listAll().then((res)=>{res.prefixes.forEach((folderRef)=>{// All the prefixes under listRef.// You may call listAll() recursively on them.});res.items.forEach((itemRef)=>{// All the items under listRef.});}).catch((error)=>{// Uh-oh, an error occurred!});
import{getStorage,ref,list}from"firebase/storage";asyncfunctionpageTokenExample(){// Create a reference under which you want to listconststorage=getStorage();constlistRef=ref(storage,'files/uid');// Fetch the first page of 100.constfirstPage=awaitlist(listRef,{maxResults:100});// Use the result.// processItems(firstPage.items)// processPrefixes(firstPage.prefixes)// Fetch the second page if there are more elements.if(firstPage.nextPageToken){constsecondPage=awaitlist(listRef,{maxResults:100,pageToken:firstPage.nextPageToken,});// processItems(secondPage.items)// processPrefixes(secondPage.prefixes)}}
asyncfunctionpageTokenExample(){// Create a reference under which you want to listvarlistRef=storageRef.child('files/uid');// Fetch the first page of 100.varfirstPage=awaitlistRef.list({maxResults:100});// Use the result.// processItems(firstPage.items)// processPrefixes(firstPage.prefixes)// Fetch the second page if there are more elements.if(firstPage.nextPageToken){varsecondPage=awaitlistRef.list({maxResults:100,pageToken:firstPage.nextPageToken,});// processItems(secondPage.items)// processPrefixes(secondPage.prefixes)}}
[null,null,["最后更新时间 (UTC):2025-08-13。"],[],[],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 of\nobjects 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](https://firebase.google.com/docs/storage/security/).\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` 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()` method as `listAll()` buffers all\nresults in memory.\n\nThe following example demonstrates `listAll`. \n\nWeb \n\n```javascript\nimport { getStorage, ref, listAll } from \"firebase/storage\";\n\nconst storage = getStorage();\n\n// Create a reference under which you want to list\nconst listRef = ref(storage, 'files/uid');\n\n// Find all the prefixes and items.\nlistAll(listRef)\n .then((res) =\u003e {\n res.prefixes.forEach((folderRef) =\u003e {\n // All the prefixes under listRef.\n // You may call listAll() recursively on them.\n });\n res.items.forEach((itemRef) =\u003e {\n // All the items under listRef.\n });\n }).catch((error) =\u003e {\n // Uh-oh, an error occurred!\n });https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/snippets/storage-next/list-files/storage_list_all.js#L8-L27\n```\n\nWeb \n\n```javascript\n// Create a reference under which you want to list\nvar listRef = storageRef.child('files/uid');\n\n// Find all the prefixes and items.\nlistRef.listAll()\n .then((res) =\u003e {\n res.prefixes.forEach((folderRef) =\u003e {\n // All the prefixes under listRef.\n // You may call listAll() recursively on them.\n });\n res.items.forEach((itemRef) =\u003e {\n // All the items under listRef.\n });\n }).catch((error) =\u003e {\n // Uh-oh, an error occurred!\n });https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/storage/list-files.js#L8-L23\n```\n\nPaginate list results\n\nThe `list()` API places a limit on the number of results it returns. `list()`\nprovides a consistent pageview and exposes a pageToken that allows control over\nwhen to fetch additional 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 using `async/await`. \n\nWeb \n\n```javascript\nimport { getStorage, ref, list } from \"firebase/storage\";\n\nasync function pageTokenExample(){\n // Create a reference under which you want to list\n const storage = getStorage();\n const listRef = ref(storage, 'files/uid');\n\n // Fetch the first page of 100.\n const firstPage = await list(listRef, { maxResults: 100 });\n\n // Use the result.\n // processItems(firstPage.items)\n // processPrefixes(firstPage.prefixes)\n\n // Fetch the second page if there are more elements.\n if (firstPage.nextPageToken) {\n const secondPage = await list(listRef, {\n maxResults: 100,\n pageToken: firstPage.nextPageToken,\n });\n // processItems(secondPage.items)\n // processPrefixes(secondPage.prefixes)\n }\n}https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/snippets/storage-next/list-files/storage_list_paginate.js#L8-L31\n```\n\nWeb \n\n```javascript\nasync function pageTokenExample(){\n // Create a reference under which you want to list\n var listRef = storageRef.child('files/uid');\n\n // Fetch the first page of 100.\n var firstPage = await listRef.list({ maxResults: 100});\n\n // Use the result.\n // processItems(firstPage.items)\n // processPrefixes(firstPage.prefixes)\n\n // Fetch the second page if there are more elements.\n if (firstPage.nextPageToken) {\n var secondPage = await listRef.list({\n maxResults: 100,\n pageToken: firstPage.nextPageToken,\n });\n // processItems(secondPage.items)\n // processPrefixes(secondPage.prefixes)\n }\n}https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/storage/list-files.js#L31-L51\n```\n\nHandle errors\n\n`list()` and `listAll()` return a rejected Promise if you haven't upgraded\nthe Security Rules to version 2. Upgrade your Security Rules if you see this\nerror: \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 permission.\nMore information on errors can be found in the\n[Handle Errors](/docs/storage/web/handle-errors)."]]