List files with Cloud Storage on Android

Cloud Storage for Firebase allows you to list the contents of yourCloud Storage bucket. The SDKs return both the items and the prefixes ofobjects under the currentCloud Storage reference.

Projects that use the List API requireCloud Storage for FirebaseRules version 2. If you have an existing Firebase project, follow the steps intheSecurity Rules Guide.

Note: The List API is only allowed for Rules version 2.In Rules version 2,allow read is the shorthand forallow get, list.

list() uses theGoogle Cloud Storage List API.InCloud Storage for Firebase, we use/ as a delimiter, which allows us toemulate file system semantics. To allow for efficient traversal of large,hierarchicalCloud Storage buckets, the List API returns prefixes anditems separately. For example, if you upload one file/images/uid/file1,

  • root.child('images').listAll() will return/images/uid as a prefix.
  • root.child('images/uid').listAll() will return the file as an item.

TheCloud Storage for Firebase SDK does not return object paths that contain twoconsecutive/s or end with a/. For example, consider a bucket that has thefollowing objects:

  • correctPrefix/happyItem
  • wrongPrefix//sadItem
  • lonelyItem/

The list operations on items in this bucket will give the following results:

  • The list operation at the root returns the references tocorrectPrefix,wrongPrefix andlonelyItem asprefixes.
  • The list operation at thecorrectPrefix/ returns the references tocorrectPrefix/happyItem asitems.
  • The list operation at thewrongPrefix/ doesn't return any referencesbecausewrongPrefix//sadItem contains two consecutive/s.
  • The list operation at thelonelyItem/ doesn't return any referencesbecause the objectlonelyItem/ ends with/.

List all files

You can uselistAll to fetch all results for a directory.This is best used for small directories as all results are buffered in memory.The operation also may not return a consistent snapshot if objects are added orremoved during the process.

For a large list, use the paginatedlist() method aslistAll() buffers allresults in memory.

The following example demonstrateslistAll.

Kotlin

valstorage=Firebase.storagevallistRef=storage.reference.child("files/uid")// You'll need to import com.google.firebase.storage.component1 and// com.google.firebase.storage.component2listRef.listAll().addOnSuccessListener{(items,prefixes)->for(prefixinprefixes){// All the prefixes under listRef.// You may call listAll() recursively on them.}for(iteminitems){// All the items under listRef.}}.addOnFailureListener{// Uh-oh, an error occurred!}

Java

StorageReferencelistRef=storage.getReference().child("files/uid");listRef.listAll().addOnSuccessListener(newOnSuccessListener<ListResult>(){@OverridepublicvoidonSuccess(ListResultlistResult){for(StorageReferenceprefix:listResult.getPrefixes()){// All the prefixes under listRef.// You may call listAll() recursively on them.}for(StorageReferenceitem:listResult.getItems()){// All the items under listRef.}}}).addOnFailureListener(newOnFailureListener(){@OverridepublicvoidonFailure(@NonNullExceptione){// Uh-oh, an error occurred!}});

Paginate list results

Thelist() API places a limit on the number of results it returns.list()provides a consistent pageview and exposes a pageToken that allows control overwhen to fetch additional results.

The pageToken encodes the path and version of the last item returned in theprevious result. In a subsequent request using the pageToken, items that comeafter the pageToken are shown.

The following example demonstrates paginating a result:

Kotlin

funlistAllPaginated(pageToken:String?){valstorage=Firebase.storagevallistRef=storage.reference.child("files/uid")// Fetch the next page of results, using the pageToken if we have one.vallistPageTask=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.component2listPageTask.addOnSuccessListener{(items,prefixes,pageToken)->// Process page of resultsprocessResults(items,prefixes)// Recurse onto next pagepageToken?.let{listAllPaginated(it)}}.addOnFailureListener{// Uh-oh, an error occurred.}}

Java

publicvoidlistAllPaginated(@NullableStringpageToken){FirebaseStoragestorage=FirebaseStorage.getInstance();StorageReferencelistRef=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(newOnSuccessListener<ListResult>(){@OverridepublicvoidonSuccess(ListResultlistResult){List<StorageReference>prefixes=listResult.getPrefixes();List<StorageReference>items=listResult.getItems();// Process page of results// ...// Recurse onto next pageif(listResult.getPageToken()!=null){listAllPaginated(listResult.getPageToken());}}}).addOnFailureListener(newOnFailureListener(){@OverridepublicvoidonFailure(@NonNullExceptione){// Uh-oh, an error occurred.}});}

Handle errors

list() andlistAll() fail if you haven't upgradedthe Security Rules to version 2. Upgrade your Security Rules if you see thiserror:

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

Other possible errors may indicate the user does not have the right permission.More information on errors can be found in theHandle Errors.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-02-18 UTC.