This page shows you how to list the objects stored in your Cloud Storage buckets, which are ordered in the list lexicographically by name.
Before you begin
To get the permissions that you need to list objects, ask your administrator
to grant you the Storage Object Viewer (roles/storage.objectViewer
)
IAM role for the bucket that contains the objects you want to
list.
If you plan on using the Google Cloud console to perform the tasks on this page,
ask your administrator to grant you the Viewer (roles/viewer
) basic role in
addition to the Storage Object Viewer (roles/storage.objectViewer
) role.
These roles contain the permissions required to list objects. To see the exact permissions that are required, expand the Required permissions section:
Required permissions
storage.objects.list
storage.buckets.list
- This permission is only needed if you want to use the Google Cloud console to perform the tasks on this page.
You can also get these permissions with other predefined roles or custom roles.
For information about granting roles for buckets, see Use IAM with buckets.
List the objects in a bucket
Complete the following steps to list the objects in a bucket:
Console
- In the Google Cloud console, go to the Cloud Storage Buckets page.
In the bucket list, click the name of the bucket whose contents you want to view.
Optional: Use filtering and sorting to limit and organize the results in your list.
Command line
Use the gcloud storage ls
command with the --recursive
flag:
gcloud storage ls --recursive gs://BUCKET_NAME/**
Where:
BUCKET_NAME
is the name of the bucket whose objects you want to list. For example,my-bucket
.
The response looks like the following example:
gs://my-bucket/cats.jpeg gs://my-bucket/dogs.jpeg gs://my-bucket/thesis.txt ...
Client libraries
C++
For more information, see the Cloud Storage C++ API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries.
The following sample lists all objects in a bucket:
The following sample lists objects with a given prefix:
C#
For more information, see the Cloud Storage C# API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries.
The following sample lists all objects in a bucket:
The following sample lists objects with a given prefix:
Go
For more information, see the Cloud Storage Go API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries.
The following sample lists all objects in a bucket:
The following sample lists objects with a given prefix:
Java
For more information, see the Cloud Storage Java API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries.
The following sample lists all objects in a bucket:
The following sample lists objects with a given prefix:
Node.js
For more information, see the Cloud Storage Node.js API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries.
The following sample lists all objects in a bucket:
The following sample lists objects with a given prefix:
PHP
For more information, see the Cloud Storage PHP API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries.
The following sample lists all objects in a bucket:
The following sample lists objects with a given prefix:
Python
For more information, see the Cloud Storage Python API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries.
The following sample lists all objects in a bucket:
The following sample lists objects with a given prefix:
Ruby
For more information, see the Cloud Storage Ruby API reference documentation.
To authenticate to Cloud Storage, set up Application Default Credentials. For more information, see Set up authentication for client libraries.
The following sample lists all objects in a bucket:
The following sample lists objects with a given prefix:
REST APIs
JSON API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorization
header.Use
cURL
to call the JSON API with a request to list objects:curl -X GET -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://storage.googleapis.com/storage/v1/b/BUCKET_NAME/o"
Where
BUCKET_NAME
is the name of the bucket whose objects you want to list. For example,my-bucket
.You can use the
includeFoldersAsPrefixes=True
query parameter to return managed folders as part of your list results. When using theincludeFoldersAsPrefixes
parameter, thedelimiter
parameter must be set to/
.
XML API
Have gcloud CLI installed and initialized, which lets you generate an access token for the
Authorization
header.Use
cURL
to call the XML API with aGET
Bucket request:curl -X GET -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://storage.googleapis.com/BUCKET_NAME?list-type=2"
Where
BUCKET_NAME
is the name of the bucket whose objects you want to list. For example,my-bucket
.You can use a
prefix=PREFIX
query string parameter to limit results to objects that have the specified prefix.
Filtering objects
Console
To filter objects by their name prefix using the Google Cloud console, use the Filter objects and folders field in the Bucket details page.
See filtering and sorting for additional filtering options available using the Google Cloud console.
Command line
When listing objects using the Google Cloud CLI, you can use
wildcards to filter for objects that begin with a specified prefix
or end with a specified suffix. For example, the following command matches
objects that begin with image
and end in .png
:
gcloud storage ls gs://my-bucket/image*.png
For more information on filtering using the Google Cloud CLI, see the
gcloud storage ls
documentation.
REST APIs
JSON API
When listing objects using the Cloud Storage JSON API, you can
use the prefix
or matchGlob
query string parameters to
filter your results. For details about using these query string
parameters, see the
Objects list JSON API reference documentation.
Filtering by prefix
You can use the prefix=PREFIX
or query string
parameter to limit results to objects or managed folders that have
the specified prefix. For example, to list all the objects in the bucket
my-bucket
with the prefix folder/subfolder/
, make an object listing
request using the URL
"https://storage.googleapis.com/storage/v1/b/my-bucket/o?prefix=folder/subfolder/"
.
Using prefix
to list the contents of a managed folder is useful for
when you only have the permission to list objects in the managed
folder, but not the whole bucket. For example, say you have the
Storage Object Viewer (roles/storage.objectViewer
) IAM
role for the managed folder my-bucket/my-managed-folder-a/
, but not for
the managed folder my-bucket/my-managed-folder-b/
. To return only the
objects in my-managed-folder-a
, you can specify
prefix=my-managed-folder-a/
.
When limiting results to a managed folder and the objects within it,
you must end PREFIX
with /
(for example,
prefix=my-managed-folder/
). Otherwise, the results can also include
objects that are adjacent to the managed folder. In this example,
you have a bucket that contains the following objects:
my-bucket/abc.txt
my-bucket/abc/object.txt
Specifying prefix=abc/
can return the objects
my-bucket/abc/object.txt
, while specifying prefix=abc
can return
both my-bucket/abc.txt
and my-bucket/abc/object.txt
.
Filtering by glob expression
You can use the matchGlob=GLOB_PATTERN
query
string parameter to filter the results to only the objects that match a
specific glob expression. For example, matchGlob=**.jpeg
can
be used to match all objects that end in .jpeg
.
Requests that use the matchGlob
parameter fail if they also include
a delimiter
parameter set to a value other than /
.
Performance considerations when listing objects
The underlying structure of buckets with hierarchical namespace enabled can influence the performance of the listing objects operation, when compared to flat namespace buckets. For more information about how to optimize the performance when listing objects in buckets with hierarchical namespace enabled, see Listing objects.
What's next
- Download an object from your bucket.
- View and edit object metadata.
- Delete objects from your bucket.
- Learn how to paginate results.