Google Cloud Storage Python Samples
This directory contains samples for Google Cloud Storage. Cloud Storage allows world-wide storage and retrieval of any amount of data at any time. You can use Google Cloud Storage for a range of scenarios including serving website content, storing data for archival and disaster recovery, or distributing large data objects to users via direct download.
Setup
Before you begin
Before running the samples, make sure you've followed the steps outlined in Quick Start.
Authentication
Refer to the Authentication Set Up Guide for more detailed instructions.
Install Dependencies
Clone this repository and change to the sample directory you want to use.
git clone https://github.com/googleapis/python-storage.git
Activate a venv if you have not already from the Quick Start.
source <your-venv>/bin/activate
Install the dependencies needed to run the samples.
cd samples/snippets pip install -r requirements.txt
Running tests locally
Before running the tests, make sure you've followed the steps outlined in Setup.
Install nox
We use nox to instrument our tests.
pip install nox
Set environment variables
You can run tests locally using your own gcs project or with a valid service account in project python-docs-samples-tests
. This outlines the workflow of running tests locally using your own gcs project.
Refer to noxfile_config.py
and a list of environment variables that can be set manually. Not every test needs all of these variables.
Below outlines some common environment variables used in the storage samples.
See Other Resources on how to create credentials, keys, and secrets.
export GOOGLE_CLOUD_PROJECT=[your-project-name]
export MAIN_GOOGLE_CLOUD_PROJECT=[your-project-name]
export BUILD_SPECIFIC_GCLOUD_PROJECT=[your-project-name]
export HMAC_KEY_TEST_SERVICE_ACCOUNT=[your-service-account]
export CLOUD_KMS_KEY=[your-kms-key]
export GOOGLE_APPLICATION_CREDENTIALS=[your-credentials]
If you are running a single test locally that does not use the environment variables, you can delete the noxfile_config.py
file and simply set your GOOGLE_CLOUD_PROJECT
export GOOGLE_CLOUD_PROJECT=[your-project-name]
Run tests with nox
nox -s lint
nox -s py-3.9 -- snippets_test.py
nox -s py-3.9 -- snippets_test.py::test_list_blobs
Special test configurations
There are restrictions on the testing projects used in Kokoro. For instance,
we change the service account based on different test sessions to avoid
hitting the maximum limit of HMAC keys on a single service account.
Another example is requester_pays_test.py
needs to use a different Storage bucket, and looks for an environment variable REQUESTER_PAYS_TEST_BUCKET
.
Please refer to noxfile_config.py
, kokoro configs, and test files to see if there are special test configurations required.
Other Resources
Samples
List of Samples
- Activate HMAC Key
- Batch Request
- Add Bucket Conditional IAM Binding
- Add Bucket Default Owner
- Add Bucket IAM Member
- Add Bucket Label
- Add Bucket Owner
- Add File Owner
- Bucket Delete Default KMS Key
- Change Default Storage Class
- Change File Storage Class
- Compose File
- Configure Retries
- Copy File
- Copy File Archived Generation
- CORS Configuration
- Create Bucket
- Create Bucket Class Location
- Create Bucket Dual Region
- Create Bucket Notifications
- Create Bucket Turbo Replication
- Create HMAC Key
- Deactivate HMAC Key
- Define Bucket Website Configuration
- Delete Bucket
- Delete Bucket Notification
- Delete File
- Delete File Archived Generation
- Delete HMAC Key
- Disable Bucket Lifecycle Management
- Disable Default Event Based Hold
- Disable Requester Pays
- Disable Uniform Bucket Level Access
- Disable Versioning
- Download Byte Range
- Download Encrypted File
- Download File
- Download File Requester Pays
- Download Into Memory
- Download Public File
- Enable Bucket Lifecycle Management
- Enable Default Event Based Hold
- Enable Requester Pays
- Enable Uniform Bucket Level Access
- Enable Versioning
- FileIO Write-Read
- FileIO Pandas
- Generate Encryption Key
- Generate Signed Post Policy V4
- Generate Signed Url V2
- Generate Signed Url V4
- Generate Upload Signed Url V4
- Get Bucket Labels
- Get Bucket Metadata
- Get Default Event Based Hold
- Get HMAC Key
- Get Metadata
- Get Public Access Prevention
- Get Requester Pays Status
- Get Retention Policy
- Get RPO
- Get Service Account
- Get Uniform Bucket Level Access
- List Buckets
- List Bucket Notifications
- List File Archived Generations
- List Files
- List Files With Prefix
- List HMAC Keys
- Lock Retention Policy
- Make Public
- Move File
- Object CSEK To CMEK
- Object Get KMS Key
- Print Bucket ACL
- Print Bucket ACL For User
- Print File ACL
- Print File ACL For User
- Print PubSub Bucket Notification
- Release Event Based Hold
- Release Temporary Hold
- Remove Bucket Conditional IAM Binding
- Remove Bucket Default Owner
- Remove Bucket IAM Member
- Remove Bucket Label
- Remove Bucket Owner
- Remove Cors Configuration
- Remove File Owner
- Remove Retention Policy
- Rename File
- Rotate Encryption Key
- Set Bucket Default KMS Key
- Set Bucket Public IAM
- Set Event Based Hold
- Set Metadata
- Set Public Access Prevention Enforced
- Set Public Access Prevention Inherited
- Set RPO Async Turbo
- Set RPO Default
- Set Retention Policy
- Set Temporary Hold
- Upload Encrypted File
- Upload File
- Upload From Memory
- Upload With KMS Key
- View Bucket IAM Members
Activate HMAC Key
View the source code. To run this sample:
python storage_activate_hmac_key.py <ACCESS_ID> <PROJECT_ID>
Batch Request
View the source code. To run this sample:
python storage_batch_request.py <BUCKET_NAME> <PREFIX>
Add Bucket Conditional IAM Binding
View the source code. To run this sample:
python storage_add_bucket_conditional_iam_binding.py <BUCKET_NAME> <ROLE> <TITLE> <DESCRIPTION> <EXPRESSION> <MEMBERS>
Add Bucket Default Owner
View the source code. To run this sample:
python storage_add_bucket_default_owner.py <BUCKET_NAME> <USER_EMAIL>
Add Bucket IAM Member
View the source code. To run this sample:
python storage_add_bucket_iam_member.py <BUCKET_NAME> <ROLE> <MEMBER>
Add Bucket Label
View the source code. To run this sample:
python storage_add_bucket_label.py <BUCKET_NAME>
Add Bucket Owner
View the source code. To run this sample:
python storage_add_bucket_owner.py <BUCKET_NAME> <USER_EMAIL>
Add File Owner
View the source code. To run this sample:
python storage_add_file_owner.py <BUCKET_NAME> <BLOB_NAME> <USER_EMAIL>
Bucket Delete Default KMS Key
View the source code. To run this sample:
python storage_bucket_delete_default_kms_key.py <BUCKET_NAME>
Change Default Storage Class
View the source code. To run this sample:
python storage_change_default_storage_class.py <BUCKET_NAME>
Change File Storage Class
View the source code. To run this sample:
python storage_change_file_storage_class.py <BUCKET_NAME> <BLOB_NAME>
Compose File
View the source code. To run this sample:
python storage_compose_file.py <BUCKET_NAME> <FIRST_BLOB_NAME> <SECOND_BLOB_NAME> <DESTINATION_BLOB_NAME>
Configure Retries
View the source code. To run this sample:
python storage_configure_retries.py <BUCKET_NAME> <BLOB_NAME>
Copy File
View the source code. To run this sample:
python storage_copy_file.py <BUCKET_NAME> <BLOB_NAME> <DESTINATION_BUCKET_NAME> <DESTINATION_BLOB_NAME>
Copy File Archived Generation
View the source code. To run this sample:
python storage_copy_file_archived_generation.py <BUCKET_NAME> <BLOB_NAME> <DESTINATION_BUCKET_NAME> <DESTINATION_BLOB_NAME> <GENERATION>
CORS Configuration
View the source code. To run this sample:
python storage_cors_configuration.py <BUCKET_NAME>
Create Bucket
View the source code. To run this sample:
python storage_create_bucket.py <BUCKET_NAME>
Create Bucket Class Location
View the source code. To run this sample:
python storage_create_bucket_class_location.py <BUCKET_NAME>
Create Bucket Dual Region
View the source code. To run this sample:
python storage_create_bucket_dual_region.py <BUCKET_NAME> <LOCATION> <REGION_1> <REGION_2>
Create Bucket Notifications
View the source code. To run this sample:
python storage_create_bucket_notifications.py <BUCKET_NAME> <TOPIC_NAME>
Create Bucket Turbo Replication
View the source code. To run this sample:
python storage_create_bucket_turbo_replication.py <BUCKET_NAME>
Create HMAC Key
View the source code. To run this sample:
python storage_create_hmac_key.py <PROJECT_ID> <SERVICE_ACCOUNT_EMAIL>
Deactivate HMAC Key
View the source code. To run this sample:
python storage_deactivate_hmac_key.py <ACCESS_ID> <PROJECT_ID>
Define Bucket Website Configuration
View the source code. To run this sample:
python storage_define_bucket_website_configuration.py <BUCKET_NAME> <MAIN_PAGE_SUFFIX> <NOT_FOUND_PAGE>
Delete Bucket
View the source code. To run this sample:
python storage_delete_bucket.py <BUCKET_NAME>
Delete Bucket Notification
View the source code. To run this sample:
python storage_delete_bucket_notification.py <BUCKET_NAME> <NOTIFICATION_ID>
Delete File
View the source code. To run this sample:
python storage_delete_file.py <BUCKET_NAME> <BLOB_NAME>
Delete File Archived Generation
View the source code. To run this sample:
python storage_delete_file_archived_generation.py <BUCKET_NAME> <BLOB_NAME> <GENERATION>
Delete HMAC Key
View the source code. To run this sample:
python storage_delete_hmac_key.py <ACCESS_ID> <PROJECT_ID>
Disable Bucket Lifecycle Management
View the source code. To run this sample:
python storage_disable_bucket_lifecycle_management.py <BUCKET_NAME>
Disable Default Event Based Hold
View the source code. To run this sample:
python storage_disable_default_event_based_hold.py <BUCKET_NAME>
Disable Requester Pays
View the source code. To run this sample:
python storage_disable_requester_pays.py <BUCKET_NAME>
Disable Uniform Bucket Level Access
View the source code. To run this sample:
python storage_disable_uniform_bucket_level_access.py <BUCKET_NAME>
Disable Versioning
View the source code. To run this sample:
python storage_disable_versioning.py <BUCKET_NAME>
Download Byte Range
View the source code. To run this sample:
python storage_download_byte_range.py <BUCKET_NAME> <SOURCE_BLOB_NAME> <START_BYTE> <END_BYTE> <DESTINATION_FILE_NAME> <>BASE64_ENCRYPTION_KEY
Download Encrypted File
View the source code. To run this sample:
python storage_download_encrypted_file.py <BUCKET_NAME> <SOURCE_BLOB_NAME> <DESTINATION_FILE_NAME> <>BASE64_ENCRYPTION_KEY
Download File
View the source code. To run this sample:
python storage_download_file.py <BUCKET_NAME> <SOURCE_BLOB_NAME> <DESTINATION_FILE_NAME>
Download File Requester Pays
View the source code. To run this sample:
python storage_download_file_requester_pays.py <BUCKET_NAME> <PROJECT_ID> <SOURCE_BLOB_NAME> <DESTINATION_FILE_NAME>
Download Into Memory
View the source code. To run this sample:
python storage_download_into_memory.py <BUCKET_NAME> <BLOB_NAME>
Download Public File
View the source code. To run this sample:
python storage_download_public_file.py <BUCKET_NAME> <SOURCE_BLOB_NAME> <DESTINATION_FILE_NAME>
Enable Bucket Lifecycle Management
View the source code. To run this sample:
python storage_enable_bucket_lifecycle_management.py <BUCKET_NAME>
Enable Default Event Based Hold
View the source code. To run this sample:
python storage_enable_default_event_based_hold.py <BUCKET_NAME>
Enable Requester Pays
View the source code. To run this sample:
python storage_enable_requester_pays.py <BUCKET_NAME>
Enable Uniform Bucket Level Access
View the source code. To run this sample:
python storage_enable_uniform_bucket_level_access.py <BUCKET_NAME>
Enable Versioning
View the source code. To run this sample:
python storage_enable_versioning.py <BUCKET_NAME>
FileIO Write-Read
View the source code. To run this sample:
python storage_fileio_write_read.py <BUCKET_NAME> <BLOB_NAME>
FileIO Pandas
View the source code. To run this sample:
python storage_fileio_pandas.py <BUCKET_NAME> <BLOB_NAME>
Generate Encryption Key
View the source code. To run this sample:
python storage_generate_encryption_key.py
Generate Signed Post Policy V4
View the source code. To run this sample:
python storage_generate_signed_post_policy_v4.py <BUCKET_NAME> <BLOB_NAME>
Generate Signed Url V2
View the source code. To run this sample:
python storage_generate_signed_url_v2.py <BUCKET_NAME> <BLOB_NAME>
Generate Signed Url V4
View the source code. To run this sample:
python storage_generate_signed_url_v4.py <BUCKET_NAME> <BLOB_NAME>
Generate Upload Signed Url V4
View the source code. To run this sample:
python storage_generate_upload_signed_url_v4.py <BUCKET_NAME> <BLOB_NAME>
Get Bucket Labels
View the source code. To run this sample:
python storage_get_bucket_labels.py <BUCKET_NAME>
Get Bucket Metadata
View the source code. To run this sample:
python storage_get_bucket_metadata.py <BUCKET_NAME>
Get Default Event Based Hold
View the source code. To run this sample:
python storage_get_default_event_based_hold.py <BUCKET_NAME>
Get HMAC Key
View the source code. To run this sample:
python storage_get_hmac_key.py <ACCESS_ID> <PROJECT_ID>
Get Metadata
View the source code. To run this sample:
python storage_get_metadata.py <BUCKET_NAME> <BLOB_NAME>
Get Public Access Prevention
View the source code. To run this sample:
python storage_get_public_access_prevention.py <BUCKET_NAME>
Get Requester Pays Status
View the source code. To run this sample:
python storage_get_requester_pays_status.py <BUCKET_NAME>
Get Retention Policy
View the source code. To run this sample:
python storage_get_retention_policy.py <BUCKET_NAME>
Get RPO
View the source code. To run this sample:
python storage_get_rpo.py <BUCKET_NAME>
Get Service Account
View the source code. To run this sample:
python storage_get_service_account.py
Get Uniform Bucket Level Access
View the source code. To run this sample:
python storage_get_uniform_bucket_level_access.py <BUCKET_NAME>
List Buckets
View the source code. To run this sample:
python storage_list_buckets.py
List Bucket Notifications
View the source code. To run this sample:
python storage_list_bucket_notifications.py <BUCKET_NAME>
List File Archived Generations
View the source code. To run this sample:
python storage_list_file_archived_generations.py <BUCKET_NAME>
List Files
View the source code. To run this sample:
python storage_list_files.py <BUCKET_NAME>
List Files With Prefix
View the source code. To run this sample:
python storage_list_files_with_prefix.py <BUCKET_NAME> <PREFIX>
List HMAC Keys
View the source code. To run this sample:
python storage_list_hmac_keys.py <PROJECT_ID>
Lock Retention Policy
View the source code. To run this sample:
python storage_lock_retention_policy.py <BUCKET_NAME>
Make Public
View the source code. To run this sample:
python storage_make_public.py <BUCKET_NAME> <BLOB_NAME>
Move File
View the source code. To run this sample:
python storage_move_file.py <BUCKET_NAME> <BLOB_NAME> <DESTINATION_BUCKET_NAME> <DESTINATION_BLOB_NAME>
Object CSEK To CMEK
View the source code. To run this sample:
python storage_object_csek_to_cmek.py <BUCKET_NAME> <BLOB_NAME> <ENCRYPTION_KEY> <KMS_KEY_NAME>
Object Get KMS Key
View the source code. To run this sample:
python storage_object_get_kms_key.py <BUCKET_NAME> <BLOB_NAME>
Print Bucket ACL
View the source code. To run this sample:
python storage_print_bucket_acl.py <BUCKET_NAME>
Print Bucket ACL For User
View the source code. To run this sample:
python storage_print_bucket_acl_for_user.py <BUCKET_NAME> <USER_EMAIL>
Print File ACL
View the source code. To run this sample:
python storage_print_file_acl.py <BUCKET_NAME> <BLOB_NAME>
Print File ACL For User
View the source code. To run this sample:
python storage_print_file_acl_for_user.py <BUCKET_NAME> <BLOB_NAME> <USER_EMAIL>
Print PubSub Bucket Notification
View the source code. To run this sample:
python storage_print_pubsub_bucket_notification.py <BUCKET_NAME> <NOTIFICATION_ID>
Release Event Based Hold
View the source code. To run this sample:
python storage_release_event_based_hold.py <BUCKET_NAME> <BLOB_NAME>
Release Temporary Hold
View the source code. To run this sample:
python storage_release_temporary_hold.py <BUCKET_NAME> <BLOB_NAME>
Remove Bucket Conditional IAM Binding
View the source code. To run this sample:
python storage_remove_bucket_conditional_iam_binding.py <BUCKET_NAME> <ROLE> <TITLE> <DESCRIPTION> <EXPRESSION>
Remove Bucket Default Owner
View the source code. To run this sample:
python storage_remove_bucket_default_owner.py <BUCKET_NAME> <USER_EMAIL>
Remove Bucket IAM Member
View the source code. To run this sample:
python storage_remove_bucket_iam_member.py <BUCKET_NAME> <ROLE> <MEMBER>
Remove Bucket Label
View the source code. To run this sample:
python storage_remove_bucket_label.py <BUCKET_NAME>
Remove Bucket Owner
View the source code. To run this sample:
python storage_remove_bucket_owner.py <BUCKET_NAME> <USER_EMAIL>
Remove CORS Configuration
View the source code. To run this sample:
python storage_remove_cors_configuration.py <BUCKET_NAME>
Remove File Owner
View the source code. To run this sample:
python storage_remove_file_owner.py <BUCKET_NAME> <BLOB_NAME> <USER_EMAIL>
Remove Retention Policy
View the source code. To run this sample:
python storage_remove_retention_policy.py <BUCKET_NAME>
Rename File
View the source code. To run this sample:
python storage_rename_file.py <BUCKET_NAME> <BLOB_NAME> <NEW_NAME>
Rotate Encryption Key
View the source code. To run this sample:
python storage_rotate_encryption_key.py <BUCKET_NAME> <BLOB_NAME> <BASE64_ENCRYPTION_KEY> <BASE64_NEW_ENCRYPTION_KEY>
Set Bucket Default KMS Key
View the source code. To run this sample:
python storage_set_bucket_default_kms_key.py <BUCKET_NAME> <KMS_KEY_NAME>
Set Bucket Public IAM
View the source code. To run this sample:
python storage_set_bucket_public_iam.py <BUCKET_NAME>
Set Event Based Hold
View the source code. To run this sample:
python storage_set_event_based_hold.py <BUCKET_NAME> <BLOB_NAME>
Set Metadata
View the source code. To run this sample:
python storage_set_metadata.py <BUCKET_NAME> <BLOB_NAME>
Set Public Access Prevention Enforced
View the source code. To run this sample:
python storage_set_public_access_prevention_enforced.py <BUCKET_NAME>
Set Public Access Prevention Inherited
View the source code. To run this sample:
python storage_set_public_access_prevention_inherited.py <BUCKET_NAME>
Set Retention Policy
View the source code. To run this sample:
python storage_set_retention_policy.py <BUCKET_NAME> <RETENTION_PERIOD>
Set RPO Async Turbo
View the source code. To run this sample:
python storage_set_rpo_async_turbo.py <BUCKET_NAME>
Set RPO Default
View the source code. To run this sample:
python storage_set_rpo_default.py <BUCKET_NAME>
Set Temporary Hold
View the source code. To run this sample:
python storage_set_temporary_hold.py <BUCKET_NAME> <BLOB_NAME>
Upload Encrypted File
View the source code. To run this sample:
python storage_upload_encrypted_file.py <BUCKET_NAME> <SOURCE_FILE_NAME> <DESTINATION_BLOB_NAME> <BASE64_ENCRYPTION_KEY>
Upload File
View the source code. To run this sample:
python storage_upload_file.py <BUCKET_NAME> <SOURCE_FILE_NAME> <DESTINATION_BLOB_NAME>
Upload From Memory
View the source code. To run this sample:
python storage_upload_from_memory.py <BUCKET_NAME> <CONTENTS> <DESTINATION_BLOB_NAME>
Upload With KMS Key
View the source code. To run this sample:
python storage_upload_with_kms_key.py <BUCKET_NAME> <SOURCE_FILE_NAME> <DESTINATION_BLOB_NAME> <KMS_KEY_NAME>
View Bucket IAM Members
View the source code. To run this sample:
python storage_view_bucket_iam_members.py <BUCKET_NAME>