Class Client (2.7.0)

Client(project=<objectobject>,credentials=None,_http=None,client_info=None,client_options=None,use_auth_w_custom_endpoint=True)

Client to bundle configuration needed for API requests.

Parameters

NameDescription
projectstr or None

the project which the client acts on behalf of. Will be passed when creating a topic. If not passed, falls back to the default inferred from the environment.

credentialsgoogle.auth.credentials.Credentials

(Optional) The OAuth2 Credentials to use for this client. If not passed (and if no_http object is passed), falls back to the default inferred from the environment.

_httprequests.Session

(Optional) HTTP object to make requests. Can be any object that definesrequest() with the same interface asrequests.Session.request. If not passed, an_http object is created that is bound to thecredentials for the current object. This parameter should be considered private, and could change in the future.

client_infogoogle.api_core.client_info.ClientInfo

The client info used to send a user-agent string along with API requests. IfNone, then default info will be used. Generally, you only need to set this if you're developing your own library or partner tool.

client_optionsgoogle.api_core.client_options.ClientOptions ordict

(Optional) Client options used to set user options on the client. API Endpoint should be set through client_options.

use_auth_w_custom_endpointbool

(Optional) Whether authentication is required under custom endpoints. If false, uses AnonymousCredentials and bypasses authentication. Defaults to True. Note this is only used when a custom endpoint is set in conjunction.

Properties

current_batch

Currently-active batch.

Returns
TypeDescription
Batch orNoneType (if no batch is active).The batch at the top of the batch stack.

Methods

batch

batch()
Returns
TypeDescription
BatchThe batch object created.

bucket

Parameters
NameDescription
bucket_namestr

The name of the bucket to be instantiated.

user_projectstr

(Optional) The project ID to be billed for API requests made via the bucket.

Returns
TypeDescription
BucketThe bucket object created.

create_anonymous_client

Returns
TypeDescription
ClientInstance w/ anonymous credentials and no project.

create_bucket

Parameters
NameDescription
bucket_or_nameUnion[Bucket, str, ]

The bucket resource to pass or name to create.

requester_paysbool

DEPRECATED. Use Bucket().requester_pays instead. (Optional) Whether requester pays for API requests for this bucket and its blobs.

projectstr

(Optional) The project under which the bucket is to be created. If not passed, uses the project set on the client.

user_projectstr

(Optional) The project ID to be billed for API requests made via created bucket.

locationstr

(Optional) The location of the bucket. If not passed, the default location, US, will be used. If specifying a dual-region,data_locations should be set in conjunction. See:https://cloud.google.com/storage/docs/locations

data_locationslist of str

(Optional) The list of regional locations of a custom dual-region bucket. Dual-regions require exactly 2 regional locations. See:https://cloud.google.com/storage/docs/locations

predefined_aclstr

(Optional) Name of predefined ACL to apply to bucket. See:https://cloud.google.com/storage/docs/access-control/lists#predefined-acl

predefined_default_object_aclstr

(Optional) Name of predefined ACL to apply to bucket's objects. See:https://cloud.google.com/storage/docs/access-control/lists#predefined-acl

timeoutOptional[Union[float, Tuple[float, float]]]

The amount of time, in seconds, to wait for the server response. Can also be passed as a tuple (connect_timeout, read_timeout). Seerequests.Session.request documentation for details.

retryOptional[Union[google.api_core.retry.Retry,google.cloud.storage.retry.ConditionalRetryPolicy]]

How to retry the RPC. A None value will disable retries. A google.api_core.retry.Retry value will enable retries, and the object will define retriable response codes and errors and configure backoff and timeout options. Agoogle.cloud.storage.retry.ConditionalRetryPolicy value wraps a Retry object and activates it only if certain conditions are met. This class exists to provide safe defaults for RPC calls that are not technically safe to retry normally (due to potential data duplication or other side-effects) but become safe to retry if a condition such as if_metageneration_match is set. See the retry.py source code and docstrings in this package (google.cloud.storage.retry) for information on retry types and how to configure them.

Exceptions
TypeDescription
google.cloud.exceptions.ConflictIf the bucket already exists.

create_hmac_key

create_hmac_key(service_account_email,project_id=None,user_project=None,timeout=60,retry=None)

Create an HMAC key for a service account.

Parameters
NameDescription
service_account_emailstr

e-mail address of the service account

project_idstr

(Optional) Explicit project ID for the key. Defaults to the client's project.

user_projectstr

(Optional) This parameter is currently ignored.

timeoutfloat or tuple

(Optional) The amount of time, in seconds, to wait for the server response. See:configuring_timeouts

retrygoogle.api_core.retry.Retry orgoogle.cloud.storage.retry.ConditionalRetryPolicy

(Optional) How to retry the RPC. A None value will disable retries. A google.api_core.retry.Retry value will enable retries, and the object will define retriable response codes and errors and configure backoff and timeout options. Agoogle.cloud.storage.retry.ConditionalRetryPolicy value wraps a Retry object and activates it only if certain conditions are met. This class exists to provide safe defaults for RPC calls that are not technically safe to retry normally (due to potential data duplication or other side-effects) but become safe to retry if a condition such as if_metageneration_match is set. See the retry.py source code and docstrings in this package (google.cloud.storage.retry) for information on retry types and how to configure them.

Returns
TypeDescription
Tuple[HMACKeyMetadata, str]metadata for the created key, plus the bytes of the key's secret, which is an 40-character base64-encoded string.

download_blob_to_file

download_blob_to_file(blob_or_uri,file_obj,start=None,end=None,raw_download=False,if_etag_match=None,if_etag_not_match=None,if_generation_match=None,if_generation_not_match=None,if_metageneration_match=None,if_metageneration_not_match=None,timeout=60,checksum='md5',retry=<google.api_core.retry.Retryobject>)

Download the contents of a blob object or blob URI into a file-like object.

Seehttps://cloud.google.com/storage/docs/downloading-objects

Parameters
NameDescription
blob_or_uriUnion[Blob, str, ]

The blob resource to pass or URI to download.

file_objfile

A file handle to which to write the blob's data.

startint

(Optional) The first byte in a range to be downloaded.

endint

(Optional) The last byte in a range to be downloaded.

raw_downloadbool

(Optional) If true, download the object without any expansion.

if_etag_matchUnion[str, Set[str]]

(Optional) See :ref:using-if-etag-match

if_etag_not_matchUnion[str, Set[str]]

(Optional) See :ref:using-if-etag-not-match

if_generation_matchlong

(Optional) See :ref:using-if-generation-match

if_generation_not_matchlong

(Optional) See :ref:using-if-generation-not-match

if_metageneration_matchlong

(Optional) See :ref:using-if-metageneration-match

if_metageneration_not_matchlong

(Optional) See :ref:using-if-metageneration-not-match

timeout[Union[float, Tuple[float, float]]]

(Optional) The amount of time, in seconds, to wait for the server response. See:configuring_timeouts

checksumstr

(Optional) The type of checksum to compute to verify the integrity of the object. The response headers must contain a checksum of the requested type. If the headers lack an appropriate checksum (for instance in the case of transcoded or ranged downloads where the remote service does not know the correct checksum, including downloads where chunk_size is set) an INFO-level log will be emitted. Supported values are "md5", "crc32c" and None. The default is "md5".

retrygoogle.api_core.retry.Retry orgoogle.cloud.storage.retry.ConditionalRetryPolicy

(Optional) How to retry the RPC. A None value will disable retries. A google.api_core.retry.Retry value will enable retries, and the object will define retriable response codes and errors and configure backoff and timeout options. Agoogle.cloud.storage.retry.ConditionalRetryPolicy value wraps a Retry object and activates it only if certain conditions are met. This class exists to provide safe defaults for RPC calls that are not technically safe to retry normally (due to potential data duplication or other side-effects) but become safe to retry if a condition such as if_metageneration_match is set. See the retry.py source code and docstrings in this package (google.cloud.storage.retry) for information on retry types and how to configure them. Media operations (downloads and uploads) do not support non-default predicates in a Retry object. The default will always be used. Other configuration changes for Retry objects such as delays and deadlines are respected.

generate_signed_post_policy_v4

generate_signed_post_policy_v4(bucket_name,blob_name,expiration,conditions=None,fields=None,credentials=None,virtual_hosted_style=False,bucket_bound_hostname=None,scheme="http",service_account_email=None,access_token=None,)
Parameters
NameDescription
bucket_namestr

Bucket name.

blob_namestr

Object name.

expirationUnion[Integer, datetime.datetime, datetime.timedelta]

Policy expiration time. If adatetime instance is passed without an explicittzinfo set, it will be assumed to beUTC.

conditionslist

(Optional) List of POST policy conditions, which are used to restrict what is allowed in the request.

fieldsdict

(Optional) Additional elements to include into request.

credentialsgoogle.auth.credentials.Signing

(Optional) Credentials object with an associated private key to sign text.

virtual_hosted_stylebool

(Optional) If True, construct the URL relative to the bucket virtual hostname, e.g., '

bucket_bound_hostnamestr

(Optional) If passed, construct the URL relative to the bucket-bound hostname. Value can be bare or with a scheme, e.g., 'example.com' or 'http://example.com'. See:https://cloud.google.com/storage/docs/request-endpoints#cname

schemestr

(Optional) Ifbucket_bound_hostname is passed as a bare hostname, use this value as a scheme.https will work only when using a CDN. Defaults to"http".

service_account_emailstr

(Optional) E-mail address of the service account.

access_tokenstr

(Optional) Access token for a service account.

Returns
TypeDescription
dictSigned POST policy.

get_bucket

Parameters
NameDescription
bucket_or_nameUnion[Bucket, str, ]

The bucket resource to pass or name to create.

timeoutOptional[Union[float, Tuple[float, float]]]

The amount of time, in seconds, to wait for the server response. Can also be passed as a tuple (connect_timeout, read_timeout). Seerequests.Session.request documentation for details.

if_metageneration_matchOptional[long]

Make the operation conditional on whether the blob's current metageneration matches the given value.

if_metageneration_not_matchOptional[long]

Make the operation conditional on whether the blob's current metageneration does not match the given value.

retryOptional[Union[google.api_core.retry.Retry,google.cloud.storage.retry.ConditionalRetryPolicy]]

How to retry the RPC. A None value will disable retries. A google.api_core.retry.Retry value will enable retries, and the object will define retriable response codes and errors and configure backoff and timeout options. Agoogle.cloud.storage.retry.ConditionalRetryPolicy value wraps a Retry object and activates it only if certain conditions are met. This class exists to provide safe defaults for RPC calls that are not technically safe to retry normally (due to potential data duplication or other side-effects) but become safe to retry if a condition such as if_metageneration_match is set. See the retry.py source code and docstrings in this package (google.cloud.storage.retry) for information on retry types and how to configure them.

Exceptions
TypeDescription
google.cloud.exceptions.NotFoundIf the bucket is not found.

get_hmac_key_metadata

get_hmac_key_metadata(access_id,project_id=None,user_project=None,timeout=60)

Return a metadata instance for the given HMAC key.

Parameters
NameDescription
access_idstr

Unique ID of an existing key.

project_idstr

(Optional) Project ID of an existing key. Defaults to client's project.

timeoutfloat or tuple

(Optional) The amount of time, in seconds, to wait for the server response. See:configuring_timeouts

user_projectstr

(Optional) This parameter is currently ignored.

get_service_account_email

get_service_account_email(project=None,timeout=60,retry=<google.api_core.retry.Retryobject>)

Get the email address of the project's GCS service account

Parameters
NameDescription
projectstr

(Optional) Project ID to use for retreiving GCS service account email address. Defaults to the client's project.

timeoutfloat or tuple

(Optional) The amount of time, in seconds, to wait for the server response. See:configuring_timeouts

retrygoogle.api_core.retry.Retry orgoogle.cloud.storage.retry.ConditionalRetryPolicy

(Optional) How to retry the RPC. See:configuring_retries

Returns
TypeDescription
strservice account email address

list_blobs

list_blobs(bucket_or_name,max_results=None,page_token=None,prefix=None,delimiter=None,start_offset=None,end_offset=None,include_trailing_delimiter=None,versions=None,projection='noAcl',fields=None,page_size=None,timeout=60,retry=<google.api_core.retry.Retryobject>)

Return an iterator used to find blobs in the bucket.

Ifuser_project is set, bills the API request to that project.

Note:List prefixes (directories) in a bucket using a prefix and delimiter.See acode samplelisting objects using a prefix filter.
Parameters
NameDescription
bucket_or_nameUnion[Bucket, str, ]

The bucket resource to pass or name to create.

max_resultsint

(Optional) The maximum number of blobs to return.

page_tokenstr

(Optional) If present, return the next batch of blobs, using the value, which must correspond to thenextPageToken value returned in the previous response. Deprecated: use thepages property of the returned iterator instead of manually passing the token.

prefixstr

(Optional) Prefix used to filter blobs.

delimiterstr

(Optional) Delimiter, used withprefix to emulate hierarchy.

start_offsetstr

(Optional) Filter results to objects whose names are lexicographically equal to or afterstartOffset. IfendOffset is also set, the objects listed will have names betweenstartOffset (inclusive) andendOffset (exclusive).

end_offsetstr

(Optional) Filter results to objects whose names are lexicographically beforeendOffset. IfstartOffset is also set, the objects listed will have names betweenstartOffset (inclusive) andendOffset (exclusive).

include_trailing_delimiterboolean

(Optional) If true, objects that end in exactly one instance ofdelimiter will have their metadata included initems in addition toprefixes.

versionsbool

(Optional) Whether object versions should be returned as separate blobs.

projectionstr

(Optional) If used, must be 'full' or 'noAcl'. Defaults to'noAcl'. Specifies the set of properties to return.

fieldsstr

(Optional) Selector specifying which fields to include in a partial response. Must be a list of fields. For example to get a partial response with just the next page token and the name and language of each blob returned:'items(name,contentLanguage),nextPageToken'. See:https://cloud.google.com/storage/docs/json_api/v1/parameters#fields

page_sizeint

(Optional) Maximum number of blobs to return in each page. Defaults to a value set by the API.

timeoutOptional[Union[float, Tuple[float, float]]]

The amount of time, in seconds, to wait for the server response. Can also be passed as a tuple (connect_timeout, read_timeout). Seerequests.Session.request documentation for details.

retryOptional[Union[google.api_core.retry.Retry,google.cloud.storage.retry.ConditionalRetryPolicy]]

How to retry the RPC. A None value will disable retries. A google.api_core.retry.Retry value will enable retries, and the object will define retriable response codes and errors and configure backoff and timeout options. Agoogle.cloud.storage.retry.ConditionalRetryPolicy value wraps a Retry object and activates it only if certain conditions are met. This class exists to provide safe defaults for RPC calls that are not technically safe to retry normally (due to potential data duplication or other side-effects) but become safe to retry if a condition such as if_metageneration_match is set. See the retry.py source code and docstrings in this package (google.cloud.storage.retry) for information on retry types and how to configure them.

list_buckets

list_buckets(max_results=None,page_token=None,prefix=None,projection='noAcl',fields=None,project=None,page_size=None,timeout=60,retry=<google.api_core.retry.Retryobject>)

Get all buckets in the project associated to the client.

This will not populate the list of blobs available in eachbucket.

SeeAPI reference docs and acode sample.

Parameters
NameDescription
max_resultsint

(Optional) The maximum number of buckets to return.

page_tokenstr

(Optional) If present, return the next batch of buckets, using the value, which must correspond to thenextPageToken value returned in the previous response. Deprecated: use thepages property of the returned iterator instead of manually passing the token.

prefixstr

(Optional) Filter results to buckets whose names begin with this prefix.

projectionstr

(Optional) Specifies the set of properties to return. If used, must be 'full' or 'noAcl'. Defaults to 'noAcl'.

fieldsstr

(Optional) Selector specifying which fields to include in a partial response. Must be a list of fields. For example to get a partial response with just the next page token and the language of each bucket returned: 'items/id,nextPageToken'

projectstr

(Optional) The project whose buckets are to be listed. If not passed, uses the project set on the client.

page_sizeint

(Optional) Maximum number of buckets to return in each page. Defaults to a value set by the API.

timeoutfloat or tuple

(Optional) The amount of time, in seconds, to wait for the server response. See:configuring_timeouts

retrygoogle.api_core.retry.Retry orgoogle.cloud.storage.retry.ConditionalRetryPolicy

(Optional) How to retry the RPC. See:configuring_retries

Exceptions
TypeDescription
ValueErrorif bothproject isNone and the client's project is alsoNone.
Returns
TypeDescription
google.api_core.page_iterator.IteratorIterator of allBucket belonging to this project.

list_hmac_keys

list_hmac_keys(max_results=None,service_account_email=None,show_deleted_keys=None,project_id=None,user_project=None,timeout=60,retry=<google.api_core.retry.Retryobject>)

List HMAC keys for a project.

Parameters
NameDescription
max_resultsint

(Optional) Max number of keys to return in a given page.

service_account_emailstr

(Optional) Limit keys to those created by the given service account.

show_deleted_keysbool

(Optional) Included deleted keys in the list. Default is to exclude them.

project_idstr

(Optional) Explicit project ID for the key. Defaults to the client's project.

user_projectstr

(Optional) This parameter is currently ignored.

timeoutfloat or tuple

(Optional) The amount of time, in seconds, to wait for the server response. See:configuring_timeouts

retrygoogle.api_core.retry.Retry orgoogle.cloud.storage.retry.ConditionalRetryPolicy

(Optional) How to retry the RPC. See:configuring_retries

Returns
TypeDescription
Tuple[HMACKeyMetadata, str]metadata for the created key, plus the bytes of the key's secret, which is an 40-character base64-encoded string.

lookup_bucket

lookup_bucket(bucket_name,timeout=60,if_metageneration_match=None,if_metageneration_not_match=None,retry=<google.api_core.retry.Retryobject>)

Get a bucket by name, returning None if not found.

You can use this if you would rather check for a None valuethan catching a NotFound exception.

Parameters
NameDescription
bucket_namestr

The name of the bucket to get.

timeoutfloat or tuple

(Optional) The amount of time, in seconds, to wait for the server response. See:configuring_timeouts

if_metageneration_matchlong

(Optional) Make the operation conditional on whether the blob's current metageneration matches the given value.

if_metageneration_not_matchlong

(Optional) Make the operation conditional on whether the blob's current metageneration does not match the given value.

retrygoogle.api_core.retry.Retry orgoogle.cloud.storage.retry.ConditionalRetryPolicy

(Optional) How to retry the RPC. See:configuring_retries

Returns
TypeDescription
Bucket orNoneTypeThe bucket matching the name provided or None if not found.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-11-05 UTC.