Cloud Storage for Firebase를 사용하면 Cloud Storage 버킷의 콘텐츠를 나열할 수 있습니다. SDK는 현재 Cloud Storage 참조 아래에 있는 객체의 항목과 프리픽스를 반환합니다.
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/uid').listAll()은 항목으로 파일을 반환합니다.
Cloud Storage for Firebase SDK는 두 개의 연속된 /가 포함되거나 /.으로 끝나는 객체 경로를 반환하지 않습니다. 버킷에 다음과 같은 객체가 있는 경우를 예로 들어 보겠습니다.
correctPrefix/happyItem
wrongPrefix//sadItem
lonelyItem/
이 버킷의 항목에 대한 list 작업의 결과는 다음과 같습니다.
루트에서 list 작업을 실행할 경우 correctPrefix, wrongPrefix, lonelyItem에 대한 참조가 prefixes로 반환됩니다.
correctPrefix/에서 list 작업을 실행할 경우 correctPrefix/happyItem에 대한 참조가 items로 반환됩니다.
wrongPrefix/에서 list 작업을 실행할 경우 wrongPrefix//sadItem에 / 두 개가 연속되어 있으므로 참조가 반환되지 않습니다.
lonelyItem/에서 list 작업을 실행할 경우 lonelyItem/ 객체가 /로 끝나기 때문에 참조가 반환되지 않습니다.
모든 파일 나열
listAll을 사용하여 디렉터리의 모든 결과를 가져올 수 있습니다.
모든 결과가 메모리에 버퍼링되므로 이 작업은 작은 디렉터리에 사용하는 것이 가장 좋습니다.
처리하는 중 객체가 추가되거나 삭제되면 일관된 스냅샷이 반환되지 않을 수도 있습니다.
listAll()은 메모리에 모든 결과를 버퍼링하므로 목록의 크기가 큰 경우에는 페이지로 나뉘는 list() 메서드를 사용합니다.
다음은 listAll을 보여주는 예시입니다.
Web
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!});
list() API는 반환하는 결과 수에 제한을 둡니다. list()는 일관된 페이지 조회수를 제공하고 추가 결과를 가져올 시기를 제어할 수 있는 pageToken을 노출합니다.
pageToken은 이전 결과에서 반환된 마지막 항목의 경로 및 버전을 인코딩합니다. 이후에 pageToken을 사용하여 요청하면 pageToken 다음에 오는 항목이 표시됩니다.
다음 예시에서는 async/await를 사용하여 결과를 페이지로 나누는 방법을 보여줍니다.
Web
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,["최종 업데이트: 2025-08-12(UTC)"],[],[],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)."]]