Use the Google Cloud Client Library for Python
mainThe samples in this repository are built using the Google Cloud Client Library for Python. For detailed API usage, documentation, or to report issues, refer to the official resources.
repository·main·Indexed 19 days ago
https://github.com/googleapis/python-storageA Python client library for interacting with Google Cloud Storage, enabling the management of buckets and objects (unstructured data). The library supports operations such as creating buckets with various configurations, managing HMAC keys, configuring CORS, handling IAM bindings, and performing file operations including downloads, uploads, and composition.
The samples in this repository are built using the Google Cloud Client Library for Python. For detailed API usage, documentation, or to report issues, refer to the official resources.
The StorageTransport Abstract Base Class (ABC) serves as the foundation for all transport implementations in the library. Depending on your requirements for protocol (gRPC vs. REST) and execution model (Synchronous vs. Asynchronous), you will interact with different child classes:
StorageGrpcTransport (defined in grpc.py).StorageGrpcAsyncIOTransport (defined in grpc_asyncio.py).StorageRestTransport (defined in rest.py). This class uses METHOD inner classes derived from the private _BaseMETHOD classes in _BaseStorageRestTransport.Note that _BaseStorageRestTransport and its inner class _BaseMETHOD are private implementation details and should not be used directly by end-users.
Cloud Storage uses preconditions to ensure safe read-modify-write updates and conditional operations. By providing specific identifiers, you can instruct the service to only perform a request if the object is in the expected state.
ETag attribute is read-only.generation changes and the metageneration is reset to 1. The generation attribute is read-only.Bucket, it is initialized to 1 upon creation.Blob, it is initialized to 1 upon the first upload.metageneration increments. The metageneration attribute is read-only.The Google Cloud Storage Python client uses the requests library by default, making it safe to share client instances across multiple threads.
However, when using multiprocessing, you should avoid sharing a client instance created in a parent process. Instead, create new client instances within the child process after multiprocessing.Pool or multiprocessing.Process has invoked os.fork() to ensure stability and avoid resource conflicts.
Cloud Storage provides two parallel access control systems:
Note: You can enable uniform bucket-level access to disable ACLs entirely and use IAM exclusively.
Starting with version 3.0, uploads and downloads use an "auto" checksum policy.
crc32c checksums by default. If the fast C extension for crc32c is unavailable, it falls back to md5.start or end parameters still do not support checksumming.Blob.upload_from_file(): This method now requires the file to be opened in bytes mode. Passing a file in string mode will now raise a TypeError due to the new checksum defaults.This is a PREVIEW FEATURE. You can use OpenTelemetry to generate traces for Cloud Storage calls.
pip install google-cloud-storage[tracing]export ENABLE_GCS_PYTHON_CLIENT_OTEL_TRACES=Truerequests library to trace underlying HTTP calls.pip install google-cloud-storage[tracing]
export ENABLE_GCS_PYTHON_CLIENT_OTEL_TRACES=TrueTo use the Google Cloud Storage Python client, you must configure authentication for your application. This typically involves setting up credentials that allow your code to interact with Google Cloud services.
For detailed instructions on setting up credentials for your specific environment (such as using Service Accounts or Application Default Credentials), refer to the official Google Cloud Authentication Getting Started Guide.
https://cloud.google.com/docs/authentication/getting-startedThe library provides default retry policies based on the idempotency of the API request. You can override these defaults to customize how transient errors are handled.
DEFAULT_RETRY (retries any transient error).DEFAULT_RETRY_IF_GENERATION_SPECIFIED (requires generation or ifGenerationMatch header).DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED (requires ifMetagenerationMatch header).DEFAULT_RETRY_IF_ETAG_IN_JSON (requires ETAG in payload).retry=None).You can customize retries using several methods:
retry=None..with_XXX methods on DEFAULT_RETRY to adjust timeout or delay parameters.google.api_core.retry.Retry instance: Define specific retriable exceptions and backoff logic.ConditionalRetryPolicy: Wrap a retry policy to activate it only when specific conditions (like an ETAG being present) are met.from google.cloud.storage.retry import DEFAULT_RETRY
# Customize retry with a timeout of 500 seconds (default=120 seconds).
modified_retry = DEFAULT_RETRY.with_timeout(500.0)
# Customize delay parameters: initial wait, multiplier, and maximum wait time.
# Defaults: initial=1.0, multiplier=2.0, maximum=60.0
modified_retry = modified_retry.with_delay(initial=1.5, multiplier=1.2, maximum=45.0)The samples/snippets directory contains standalone Python scripts demonstrating various Google Cloud Storage operations. Most samples require specific command-line arguments such as <BUCKET_NAME>, <BLOB_NAME>, or <PROJECT_ID> to function. You can run these scripts directly using the Python interpreter.
python <sample_name>.py <ARGUMENTS>To use the zonal bucket snippets, you must have:
To contribute to python-storage, you must first fork the repository on GitHub and clone your fork locally. You should also configure an upstream remote pointing to the official googleapis/python-storage repository to keep your local version synchronized with changes from the main project.
Follow these steps to initialize your environment:
hack-on-python-storage).upstream remote.main branch.$ cd ${HOME}
$ git clone git@github.com:USERNAME/python-storage.git hack-on-python-storage
$ cd hack-on-python-storage
# Configure remotes to pull changes from the official repository
$ git remote add upstream git@github.com:googleapis/python-storage.git
# Fetch and merge changes from upstream into main
$ git fetch upstream
$ git merge upstream/main