Skip to content

Latest commit

 

History

History
1163 lines (708 loc) · 60.5 KB

README.md

File metadata and controls

1163 lines (708 loc) · 60.5 KB

Google Cloud Platform logo

Google Cloud Storage Python Samples

Open in Cloud Shell

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

  1. Clone this repository and change to the sample directory you want to use.

    git clone https://github.com/googleapis/python-storage.git
    
  2. Activate a venv if you have not already from the Quick Start.

    source <your-venv>/bin/activate
    
  3. Install the dependencies needed to run the samples.

    cd samples/snippets
    pip install -r requirements.txt
    

Samples

List of Samples

Activate HMAC Key

Open in Cloud Shell

View the source code. To run this sample:

python storage_activate_hmac_key.py <ACCESS_ID> <PROJECT_ID>


Batch Request

Open in Cloud Shell

View the source code. To run this sample:

python storage_batch_request.py <BUCKET_NAME> <PREFIX>


Add Bucket Conditional IAM Binding

Open in Cloud Shell

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

Open in Cloud Shell

View the source code. To run this sample:

python storage_add_bucket_default_owner.py <BUCKET_NAME> <USER_EMAIL>


Add Bucket IAM Member

Open in Cloud Shell

View the source code. To run this sample:

python storage_add_bucket_iam_member.py <BUCKET_NAME> <ROLE> <MEMBER>


Add Bucket Label

Open in Cloud Shell

View the source code. To run this sample:

python storage_add_bucket_label.py <BUCKET_NAME>


Add Bucket Owner

Open in Cloud Shell

View the source code. To run this sample:

python storage_add_bucket_owner.py <BUCKET_NAME> <USER_EMAIL>


Add File Owner

Open in Cloud Shell

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

Open in Cloud Shell

View the source code. To run this sample:

python storage_bucket_delete_default_kms_key.py <BUCKET_NAME>


Change Default Storage Class

Open in Cloud Shell

View the source code. To run this sample:

python storage_change_default_storage_class.py <BUCKET_NAME>


Change File Storage Class

Open in Cloud Shell

View the source code. To run this sample:

python storage_change_file_storage_class.py <BUCKET_NAME> <BLOB_NAME>


Compose File

Open in Cloud Shell

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

Open in Cloud Shell

View the source code. To run this sample:

python storage_configure_retries.py <BUCKET_NAME> <BLOB_NAME>


Copy File

Open in Cloud Shell

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

Open in Cloud Shell

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

Open in Cloud Shell

View the source code. To run this sample:

python storage_cors_configuration.py <BUCKET_NAME>


Create Bucket

Open in Cloud Shell

View the source code. To run this sample:

python storage_create_bucket.py <BUCKET_NAME>


Create Bucket Class Location

Open in Cloud Shell

View the source code. To run this sample:

python storage_create_bucket_class_location.py <BUCKET_NAME>


Create Bucket Dual Region

Open in Cloud Shell

View the source code. To run this sample:

python storage_create_bucket_dual_region.py <BUCKET_NAME> <REGION_1> <REGION_2>


Create Bucket Notifications

Open in Cloud Shell

View the source code. To run this sample:

python storage_create_bucket_notifications.py <BUCKET_NAME> <TOPIC_NAME>


Create Bucket Turbo Replication

Open in Cloud Shell

View the source code. To run this sample:

python storage_create_bucket_turbo_replication.py <BUCKET_NAME>


Create HMAC Key

Open in Cloud Shell

View the source code. To run this sample:

python storage_create_hmac_key.py <PROJECT_ID> <SERVICE_ACCOUNT_EMAIL>


Deactivate HMAC Key

Open in Cloud Shell

View the source code. To run this sample:

python storage_deactivate_hmac_key.py <ACCESS_ID> <PROJECT_ID>


Define Bucket Website Configuration

Open in Cloud Shell

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

Open in Cloud Shell

View the source code. To run this sample:

python storage_delete_bucket.py <BUCKET_NAME>


Delete Bucket Notification

Open in Cloud Shell

View the source code. To run this sample:

python storage_delete_bucket_notification.py <BUCKET_NAME> <NOTIFICATION_ID>


Delete File

Open in Cloud Shell

View the source code. To run this sample:

python storage_delete_file.py <BUCKET_NAME> <BLOB_NAME>


Delete File Archived Generation

Open in Cloud Shell

View the source code. To run this sample:

python storage_delete_file_archived_generation.py <BUCKET_NAME> <BLOB_NAME> <GENERATION>


Delete HMAC Key

Open in Cloud Shell

View the source code. To run this sample:

python storage_delete_hmac_key.py <ACCESS_ID> <PROJECT_ID>


Disable Bucket Lifecycle Management

Open in Cloud Shell

View the source code. To run this sample:

python storage_disable_bucket_lifecycle_management.py <BUCKET_NAME>


Disable Default Event Based Hold

Open in Cloud Shell

View the source code. To run this sample:

python storage_disable_default_event_based_hold.py <BUCKET_NAME>


Disable Requester Pays

Open in Cloud Shell

View the source code. To run this sample:

python storage_disable_requester_pays.py <BUCKET_NAME>


Disable Uniform Bucket Level Access

Open in Cloud Shell

View the source code. To run this sample:

python storage_disable_uniform_bucket_level_access.py <BUCKET_NAME>


Disable Versioning

Open in Cloud Shell

View the source code. To run this sample:

python storage_disable_versioning.py <BUCKET_NAME>


Download Byte Range

Open in Cloud Shell

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

Open in Cloud Shell

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

Open in Cloud Shell

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

Open in Cloud Shell

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

Open in Cloud Shell

View the source code. To run this sample:

python storage_download_into_memory.py <BUCKET_NAME> <BLOB_NAME>


Download Public File

Open in Cloud Shell

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

Open in Cloud Shell

View the source code. To run this sample:

python storage_enable_bucket_lifecycle_management.py <BUCKET_NAME>


Enable Default Event Based Hold

Open in Cloud Shell

View the source code. To run this sample:

python storage_enable_default_event_based_hold.py <BUCKET_NAME>


Enable Requester Pays

Open in Cloud Shell

View the source code. To run this sample:

python storage_enable_requester_pays.py <BUCKET_NAME>


Enable Uniform Bucket Level Access

Open in Cloud Shell

View the source code. To run this sample:

python storage_enable_uniform_bucket_level_access.py <BUCKET_NAME>


Enable Versioning

Open in Cloud Shell

View the source code. To run this sample:

python storage_enable_versioning.py <BUCKET_NAME>


FileIO Write-Read

Open in Cloud Shell

View the source code. To run this sample:

python storage_fileio_write_read.py <BUCKET_NAME> <BLOB_NAME>


FileIO Pandas

Open in Cloud Shell

View the source code. To run this sample:

python storage_fileio_pandas.py <BUCKET_NAME> <BLOB_NAME>


Generate Encryption Key

Open in Cloud Shell

View the source code. To run this sample:

python storage_generate_encryption_key.py


Generate Signed Post Policy V4

Open in Cloud Shell

View the source code. To run this sample:

python storage_generate_signed_post_policy_v4.py <BUCKET_NAME> <BLOB_NAME>


Generate Signed Url V2

Open in Cloud Shell

View the source code. To run this sample:

python storage_generate_signed_url_v2.py <BUCKET_NAME> <BLOB_NAME>


Generate Signed Url V4

Open in Cloud Shell

View the source code. To run this sample:

python storage_generate_signed_url_v4.py <BUCKET_NAME> <BLOB_NAME>


Generate Upload Signed Url V4

Open in Cloud Shell

View the source code. To run this sample:

python storage_generate_upload_signed_url_v4.py <BUCKET_NAME> <BLOB_NAME>


Get Bucket Labels

Open in Cloud Shell

View the source code. To run this sample:

python storage_get_bucket_labels.py <BUCKET_NAME>


Get Bucket Metadata

Open in Cloud Shell

View the source code. To run this sample:

python storage_get_bucket_metadata.py <BUCKET_NAME>


Get Default Event Based Hold

Open in Cloud Shell

View the source code. To run this sample:

python storage_get_default_event_based_hold.py <BUCKET_NAME>


Get HMAC Key

Open in Cloud Shell

View the source code. To run this sample:

python storage_get_hmac_key.py <ACCESS_ID> <PROJECT_ID>


Get Metadata

Open in Cloud Shell

View the source code. To run this sample:

python storage_get_metadata.py <BUCKET_NAME> <BLOB_NAME>


Get Public Access Prevention

Open in Cloud Shell

View the source code. To run this sample:

python storage_get_public_access_prevention.py <BUCKET_NAME>


Get Requester Pays Status

Open in Cloud Shell

View the source code. To run this sample:

python storage_get_requester_pays_status.py <BUCKET_NAME>


Get Retention Policy

Open in Cloud Shell

View the source code. To run this sample:

python storage_get_retention_policy.py <BUCKET_NAME>


Get RPO

Open in Cloud Shell

View the source code. To run this sample:

python storage_get_rpo.py <BUCKET_NAME>


Get Service Account

Open in Cloud Shell

View the source code. To run this sample:

python storage_get_service_account.py


Get Uniform Bucket Level Access

Open in Cloud Shell

View the source code. To run this sample:

python storage_get_uniform_bucket_level_access.py <BUCKET_NAME>


List Buckets

Open in Cloud Shell

View the source code. To run this sample:

python storage_list_buckets.py


List Bucket Notifications

Open in Cloud Shell

View the source code. To run this sample:

python storage_list_bucket_notifications.py <BUCKET_NAME>


List File Archived Generations

Open in Cloud Shell

View the source code. To run this sample:

python storage_list_file_archived_generations.py <BUCKET_NAME>


List Files

Open in Cloud Shell

View the source code. To run this sample:

python storage_list_files.py <BUCKET_NAME>


List Files With Prefix

Open in Cloud Shell

View the source code. To run this sample:

python storage_list_files_with_prefix.py <BUCKET_NAME> <PREFIX>


List HMAC Keys

Open in Cloud Shell

View the source code. To run this sample:

python storage_list_hmac_keys.py <PROJECT_ID>


Lock Retention Policy

Open in Cloud Shell

View the source code. To run this sample:

python storage_lock_retention_policy.py <BUCKET_NAME>


Make Public

Open in Cloud Shell

View the source code. To run this sample:

python storage_make_public.py <BUCKET_NAME> <BLOB_NAME>


Move File

Open in Cloud Shell

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

Open in Cloud Shell

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

Open in Cloud Shell

View the source code. To run this sample:

python storage_object_get_kms_key.py <BUCKET_NAME> <BLOB_NAME>


Print Bucket ACL

Open in Cloud Shell

View the source code. To run this sample:

python storage_print_bucket_acl.py <BUCKET_NAME>


Print Bucket ACL For User

Open in Cloud Shell

View the source code. To run this sample:

python storage_print_bucket_acl_for_user.py <BUCKET_NAME> <USER_EMAIL>


Print File ACL

Open in Cloud Shell

View the source code. To run this sample:

python storage_print_file_acl.py <BUCKET_NAME> <BLOB_NAME>


Print File ACL For User

Open in Cloud Shell

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

Open in Cloud Shell

View the source code. To run this sample:

python storage_print_pubsub_bucket_notification.py <BUCKET_NAME> <NOTIFICATION_ID>


Release Event Based Hold

Open in Cloud Shell

View the source code. To run this sample:

python storage_release_event_based_hold.py <BUCKET_NAME> <BLOB_NAME>


Release Temporary Hold

Open in Cloud Shell

View the source code. To run this sample:

python storage_release_temporary_hold.py <BUCKET_NAME> <BLOB_NAME>


Remove Bucket Conditional IAM Binding

Open in Cloud Shell

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

Open in Cloud Shell

View the source code. To run this sample:

python storage_remove_bucket_default_owner.py <BUCKET_NAME> <USER_EMAIL>


Remove Bucket IAM Member

Open in Cloud Shell

View the source code. To run this sample:

python storage_remove_bucket_iam_member.py <BUCKET_NAME> <ROLE> <MEMBER>


Remove Bucket Label

Open in Cloud Shell

View the source code. To run this sample:

python storage_remove_bucket_label.py <BUCKET_NAME>


Remove Bucket Owner

Open in Cloud Shell

View the source code. To run this sample:

python storage_remove_bucket_owner.py <BUCKET_NAME> <USER_EMAIL>


Remove CORS Configuration

Open in Cloud Shell

View the source code. To run this sample:

python storage_remove_cors_configuration.py <BUCKET_NAME>


Remove File Owner

Open in Cloud Shell

View the source code. To run this sample:

python storage_remove_file_owner.py <BUCKET_NAME> <BLOB_NAME> <USER_EMAIL>


Remove Retention Policy

Open in Cloud Shell

View the source code. To run this sample:

python storage_remove_retention_policy.py <BUCKET_NAME>


Rename File

Open in Cloud Shell

View the source code. To run this sample:

python storage_rename_file.py <BUCKET_NAME> <BLOB_NAME> <NEW_NAME>


Rotate Encryption Key

Open in Cloud Shell

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

Open in Cloud Shell

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

Open in Cloud Shell

View the source code. To run this sample:

python storage_set_bucket_public_iam.py <BUCKET_NAME>


Set Event Based Hold

Open in Cloud Shell

View the source code. To run this sample:

python storage_set_event_based_hold.py <BUCKET_NAME> <BLOB_NAME>


Set Metadata

Open in Cloud Shell

View the source code. To run this sample:

python storage_set_metadata.py <BUCKET_NAME> <BLOB_NAME>


Set Public Access Prevention Enforced

Open in Cloud Shell

View the source code. To run this sample:

python storage_set_public_access_prevention_enforced.py <BUCKET_NAME>


Set Public Access Prevention Inherited

Open in Cloud Shell

View the source code. To run this sample:

python storage_set_public_access_prevention_inherited.py <BUCKET_NAME>


Set Retention Policy

Open in Cloud Shell

View the source code. To run this sample:

python storage_set_retention_policy.py <BUCKET_NAME> <RETENTION_PERIOD>


Set RPO Async Turbo

Open in Cloud Shell

View the source code. To run this sample:

python storage_set_rpo_async_turbo.py <BUCKET_NAME>


Set RPO Default

Open in Cloud Shell

View the source code. To run this sample:

python storage_set_rpo_default.py <BUCKET_NAME>


Set Temporary Hold

Open in Cloud Shell

View the source code. To run this sample:

python storage_set_temporary_hold.py <BUCKET_NAME> <BLOB_NAME>


Upload Encrypted File

Open in Cloud Shell

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

Open in Cloud Shell

View the source code. To run this sample:

python storage_upload_file.py <BUCKET_NAME> <SOURCE_FILE_NAME> <DESTINATION_BLOB_NAME>


Upload From Memory

Open in Cloud Shell

View the source code. To run this sample:

python storage_upload_from_memory.py <BUCKET_NAME> <CONTENTS> <DESTINATION_BLOB_NAME>


Upload With KMS Key

Open in Cloud Shell

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

Open in Cloud Shell

View the source code. To run this sample:

python storage_view_bucket_iam_members.py <BUCKET_NAME>


Running tests locally

Before running the tests, make sure you've followed the steps outlined in Setup.

Install nox

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. The common environment variables used in the storage samples include:

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]

See Other Resources on how to create credentials, keys, and secrets

Run tests with nox

nox -s lint
nox -s py-3.7 -- snippets_test.py
nox -s py-3.7 -- 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