All Cloud Storage code samples

This page contains code samples for Cloud Storage. To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.

Activate an HMAC key

Updates the state of an HMAC key to "Active."

Add a default ACL to a bucket

Add a default access control list (ACL) to a Cloud Storage bucket.

Add a label to a bucket

Add a label to a Cloud Storage bucket.

View in documentation

Add an owner ACL to an object

Add an owner access control list (ACL) to an object in a Cloud Storage bucket.

Add an owner to a bucket

Add owner access control to a Cloud Storage bucket.

Add conditional role binding

An example of how to set a startsWith condition with the client libraries.

View in documentation

Amazon S3 SDK: List objects

Shows an example of listing objects to help developers overcome an issue with interoperability until it's fixed.

Change the default storage class of a bucket

Update the storage class of a Cloud Storage bucket.

Compose multiple objects

Compose multiple objects into a single object in a Cloud Storage bucket.

View in documentation

Configure CORS for a bucket

Add a CORS configuration to a Cloud Storage bucket.

View in documentation

Convert CSEK to CMEK

Sample converts CSEK to CMEK.

View in documentation

Copy an archived object

Copy an archived generation of an object in a Cloud Storage bucket.

View in documentation

Copy an object between buckets

Copy an object from one Cloud Storage bucket to another.

View in documentation

Create a bucket with options

Create a Cloud Storage bucket with storage class and location parameters.

Create a dual-region bucket

Create a dual-region bucket

View in documentation

Create a dual-region bucket with turbo replication

Create a dual-region bucket with turbo replication

Create a GET-signed URL for an object using Cloud Storage libraries (V4)

Generate a V4-signed URL to download an object.

Create a new Pub/Sub notification

Add a Pub/Sub notification to a Cloud Storage bucket.

Create a PUT-signed URL using Cloud Storage libraries (V4)

Provides a sample of how to generate a PUT-signed URL that is used to upload an object.

Create a service account HMAC key

Create a service account HMAC key.

Deactivate a service account HMAC key

Updates the state of a service account HMAC key to "Inactive."

Delete a bucket

Demonstrates deleting a Cloud Storage bucket.

View in documentation

Delete a Pub/Sub notification

Delete a Pub/Sub notification from a Cloud Storage storage bucket.

Delete an archived generation of an object

Delete an archived generation of an object in a Cloud Storage bucket.

View in documentation

Delete an HMAC key

Delete an HMAC key based on the access ID.

Delete an IAM member

Delete a member from the IAM policy for a Cloud Storage bucket.

View in documentation

Demonstrate retry configuration

Demonstrate retry configuration

View in documentation

Disable a default event-based hold on a bucket

Provides an example of how to disable a bucket's default event-based hold.

View in documentation

Disable a lifecycle policy

Disable lifecycle management for a Cloud Storage bucket.

View in documentation

Disable object versioning for a bucket

Disable object versioning for a Cloud Storage bucket.

View in documentation

Disable the requester pays status for a bucket

Disable the requester pays status for a Cloud Storage bucket.

View in documentation

Disable uniform bucket-level access

Disable uniform bucket-level access.

View in documentation

Download a CSEK-encrypted object

Download a CSEK-encrypted object.

Download a public object without credentials

Download an object from a public data set object.

View in documentation

Download all object contents into memory.

Download all object contents into memory.

View in documentation

Download an object

Download an object from a Cloud Storage bucket.

View in documentation

Download an object using a byte range

Download an object using a byte range from a Cloud Storage bucket.

View in documentation

Enable a default event-based hold

Provides an example of how to enable a default event-based hold.

View in documentation

Enable lifecycle management for a bucket

Enable lifecycle management for a Cloud Storage bucket.

View in documentation

Enable object versioning for a bucket

Enable object versioning for a Cloud Storage bucket.

View in documentation

Enable Requester Pays for a bucket

Enable the Requester Pays status for a Cloud Storage bucket.

View in documentation

Enable uniform bucket-level access

Enable uniform bucket-level access.

View in documentation

Generate an encryption key

Generate an encryption key.

Get a bucket's ACL

View the access control list (ACL) for a Cloud Storage bucket.

Get a bucket's ACL that's filtered by user

View the access control list (ACL) for a Cloud Storage bucket that's filtered by user.

Get a bucket's default KMS key

Get a bucket's default KMS key.

View in documentation

Get a lifecycle policy for a bucket

View the lifecycle configuration of a Cloud Storage bucket.

View in documentation

Get a Pub/Sub notification

Provides an example of how to get Pub/Sub notification metadata.

Get a retention policy

Provides an example of how to get a bucket's retention policy.

View in documentation

Get a service account

Get a Cloud Storage service account using projects.GetServiceAccount (). This is used by Pub/Sub notifications and CMEK KMS keys.

View in documentation

Get an HMAC key

Get an HMAC key with its access ID.

Get an object ACL that's filtered by user

View the access control list (ACL) for an object in a Cloud Storage bucket that's filtered by user.

Get an object's ACL

View the access control list (ACL) for an object in a Cloud Storage bucket.

Get an object's KMS key name

Get a resource's KMS key name that's used to encrypt an object.

View in documentation

Get IAM members

Get the members from an IAM policy for a Cloud Storage bucket.

View in documentation

Get metadata for a bucket and display current rpo setting

Get metadata for a bucket and display current rpo setting

View in documentation

Get the Requester Pays status on a bucket

Get the Requester Pays status for a Cloud Storage bucket.

View in documentation

Get the state of a default event-based hold

Example of how to get the state of a default event-based hold.

View in documentation

Get uniform bucket-level access

Get uniform bucket-level access.

View in documentation

List archived generations

List archived generations for an object in a Cloud Storage bucket.

View in documentation

List bucket labels

Retrieve the labels for a Cloud Storage bucket.

List buckets

List the Cloud Storage buckets in a project.

View in documentation

List buckets using Amazon S3 SDKs

List Cloud Storage buckets that use Amazon Simple Storage Service (Amazon S3) SDKs by using the Cloud Storage XML API with HMAC credentials.

List files in a paginated manner

List objects in a bucket with no autopagination, showing how to move to the next set of objects.

List Pub/Sub notifications

List the Pub/Sub notifications set up for a Cloud Storage storage bucket.

List the objects in a bucket

List all the objects in a Cloud Storage bucket.

List the objects in a bucket using a prefix filter

List the objects in a Cloud Storage bucket using a prefix.

View in documentation

Lock a bucket's retention policy

Provides an example of how to lock a bucket's retention policy.

View in documentation

Make a bucket public

Make a bucket public by using a bucket-level IAM policy.

Make an object public

Make an object in a Cloud Storage bucket publicly accessible.

View in documentation

Move an object

Moving an object in Cloud Storage consists of two operations. First, you copy the object to a destination bucket and rename the moved object. Next, you delete the original object. Delete an object only when the old and new destinations are not equal.

View in documentation

Print the website configuration for a Cloud Storage bucket.

Read Object as a Stream

Read Object as a Stream

View in documentation

Release a temporary hold on an object

Shows an example of how to release a temporary hold on an object.

View in documentation

Release an event-based hold on an object

Release an event-based hold on an object.

View in documentation

Remove a bucket label

Remove a label from a Cloud Storage bucket.

View in documentation

Remove a default ACL from a bucket

Remove a default access control list (ACL) for a Cloud Storage bucket.

Remove an owner from a bucket

Remove owner access control to a Cloud Storage bucket.

Remove owner access to an object

Remove owner access to an object in a Cloud Storage bucket.

Remove retention policy

Shows an example of how to remove a retention policy on a bucket. The bucket retention policy must be unlocked to successfully remove a policy.

View in documentation

Remove the default KMS key name

Remove the default KMS key name.

View in documentation

Requester Pays: Download an object

Download a file using the specified project as the requester.

View in documentation

Rotate an encryption key

Rotate an encryption key.

View in documentation