Blobs / Objects#
Create / interact with Google Cloud Storage blobs.
-
class
gcloud.storage.blob.Blob(name, bucket, chunk_size=None)[source]# Bases:
gcloud.storage._helpers._PropertyMixinA wrapper around Cloud Storage’s concept of an
Object.Parameters: - name (string) – The name of the blob. This corresponds to the unique path of the object in the bucket.
- bucket (
gcloud.storage.bucket.Bucket) – The bucket to which this blob belongs. - chunk_size (integer) – The size of a chunk of data whenever iterating (1 MB). This must be a multiple of 256 KB per the API specification.
-
acl# Create our ACL on demand.
-
cache_control# HTTP ‘Cache-Control’ header for this object.
- See: https://tools.ietf.org/html/rfc7234#section-5.2 and
- https://cloud.google.com/storage/docs/json_api/v1/objects
If the property is not set locally, returns
None.Return type: string or NoneType
-
chunk_size# Get the blob’s default chunk size.
Return type: integer or NoneTypeReturns: The current blob’s chunk size, if it is set.
-
client# The client bound to this blob.
-
component_count# Number of underlying components that make up this object.
See: https://cloud.google.com/storage/docs/json_api/v1/objects
Return type: integer or NoneTypeReturns: The component count (in case of a composed object) or Noneif the property is not set locally. This property will not be set on objects not created viacompose.
-
content_disposition# HTTP ‘Content-Disposition’ header for this object.
- See: https://tools.ietf.org/html/rfc6266 and
- https://cloud.google.com/storage/docs/json_api/v1/objects
If the property is not set locally, returns
None.Return type: string or NoneType
-
content_encoding# HTTP ‘Content-Encoding’ header for this object.
- See: https://tools.ietf.org/html/rfc7231#section-3.1.2.2 and
- https://cloud.google.com/storage/docs/json_api/v1/objects
If the property is not set locally, returns
None.Return type: string or NoneType
-
content_language# HTTP ‘Content-Language’ header for this object.
If the property is not set locally, returns
None.Return type: string or NoneType
-
content_type# HTTP ‘Content-Type’ header for this object.
- See: https://tools.ietf.org/html/rfc2616#section-14.17 and
- https://cloud.google.com/storage/docs/json_api/v1/objects
If the property is not set locally, returns
None.Return type: string or NoneType
-
crc32c# CRC32C checksum for this object.
- See: http://tools.ietf.org/html/rfc4960#appendix-B and
- https://cloud.google.com/storage/docs/json_api/v1/objects
If the property is not set locally, returns
None.Return type: string or NoneType
-
delete(client=None)[source]# Deletes a blob from Cloud Storage.
Parameters: client ( gcloud.storage.client.ClientorNoneType) – Optional. The client to use. If not passed, falls back to theclientstored on the blob’s bucket.Return type: BlobReturns: The blob that was just deleted. Raises: gcloud.exceptions.NotFound(propagated fromgcloud.storage.bucket.Bucket.delete_blob()).
-
download_as_string(client=None)[source]# Download the contents of this blob as a string.
Parameters: client ( gcloud.storage.client.ClientorNoneType) – Optional. The client to use. If not passed, falls back to theclientstored on the blob’s bucket.Return type: bytes Returns: The data stored in this blob. Raises: gcloud.exceptions.NotFound
-
download_to_file(file_obj, client=None)[source]# Download the contents of this blob into a file-like object.
Parameters: - file_obj (file) – A file handle to which to write the blob’s data.
- client (
gcloud.storage.client.ClientorNoneType) – Optional. The client to use. If not passed, falls back to theclientstored on the blob’s bucket.
Raises:
-
download_to_filename(filename, client=None)[source]# Download the contents of this blob into a named file.
Parameters: - filename (string) – A filename to be passed to
open. - client (
gcloud.storage.client.ClientorNoneType) – Optional. The client to use. If not passed, falls back to theclientstored on the blob’s bucket.
Raises: - filename (string) – A filename to be passed to
-
etag# Retrieve the ETag for the object.
- See: http://tools.ietf.org/html/rfc2616#section-3.11 and
- https://cloud.google.com/storage/docs/json_api/v1/objects
Return type: string or NoneTypeReturns: The blob etag or Noneif the property is not set locally.
-
exists(client=None)[source]# Determines whether or not this blob exists.
Parameters: client ( gcloud.storage.client.ClientorNoneType) – Optional. The client to use. If not passed, falls back to theclientstored on the blob’s bucket.Return type: boolean Returns: True if the blob exists in Cloud Storage.
-
generate_signed_url(expiration, method='GET', client=None, credentials=None)[source]# Generates a signed URL for this blob.
Note
If you are on Google Compute Engine, you can’t generate a signed URL. Follow https://github.com/GoogleCloudPlatform/gcloud-python/issues/922 for updates on this. If you’d like to be able to generate a signed URL from GCE, you can use a standard service account from a JSON file rather than a GCE service account.
If you have a blob that you want to allow access to for a set amount of time, you can use this method to generate a URL that is only valid within a certain time period.
This is particularly useful if you don’t want publicly accessible blobs, but don’t want to require users to explicitly log in.
Parameters: - expiration (int, long, datetime.datetime, datetime.timedelta) – When the signed URL should expire.
- method (string) – The HTTP verb that will be used when requesting the URL.
- client (
gcloud.storage.client.ClientorNoneType) – Optional. The client to use. If not passed, falls back to theclientstored on the blob’s bucket. - credentials (
oauth2client.client.OAuth2CredentialsorNoneType) – The OAuth2 credentials to use to sign the URL.
Return type: Returns: A signed URL you can use to access the resource until expiration.
-
generation# Retrieve the generation for the object.
See: https://cloud.google.com/storage/docs/json_api/v1/objects
Return type: integer or NoneTypeReturns: The generation of the blob or Noneif the property is not set locally.
-
id# Retrieve the ID for the object.
See: https://cloud.google.com/storage/docs/json_api/v1/objects
Return type: string or NoneTypeReturns: The ID of the blob or Noneif the property is not set locally.
-
make_public(client=None)[source]# Make this blob public giving all users read access.
Parameters: client ( gcloud.storage.client.ClientorNoneType) – Optional. The client to use. If not passed, falls back to theclientstored on the blob’s bucket.
-
md5_hash# MD5 hash for this object.
- See: http://tools.ietf.org/html/rfc4960#appendix-B and
- https://cloud.google.com/storage/docs/json_api/v1/objects
If the property is not set locally, returns
None.Return type: string or NoneType
-
media_link# Retrieve the media download URI for the object.
See: https://cloud.google.com/storage/docs/json_api/v1/objects
Return type: string or NoneTypeReturns: The media link for the blob or Noneif the property is not set locally.
-
metadata# Retrieve arbitrary/application specific metadata for the object.
See: https://cloud.google.com/storage/docs/json_api/v1/objects
Return type: dict or NoneTypeReturns: The metadata associated with the blob or Noneif the property is not set locally.
-
metageneration# Retrieve the metageneration for the object.
See: https://cloud.google.com/storage/docs/json_api/v1/objects
Return type: integer or NoneTypeReturns: The metageneration of the blob or Noneif the property is not set locally.
-
owner# Retrieve info about the owner of the object.
See: https://cloud.google.com/storage/docs/json_api/v1/objects
Return type: dict or NoneTypeReturns: Mapping of owner’s role/ID. If the property is not set locally, returns None.
-
path# Getter property for the URL path to this Blob.
Return type: string Returns: The URL path to this Blob.
-
static
path_helper(bucket_path, blob_name)[source]# Relative URL path for a blob.
Parameters: Return type: Returns: The relative URL path for
blob_name.
-
public_url# The public URL for this blob’s object.
Return type: string Returns: The public URL for this blob.
-
self_link# Retrieve the URI for the object.
See: https://cloud.google.com/storage/docs/json_api/v1/objects
Return type: string or NoneTypeReturns: The self link for the blob or Noneif the property is not set locally.
-
size# Size of the object, in bytes.
See: https://cloud.google.com/storage/docs/json_api/v1/objects
Return type: integer or NoneTypeReturns: The size of the blob or Noneif the property is not set locally.
-
storage_class# Retrieve the storage class for the object.
See: https://cloud.google.com/storage/docs/storage-classes https://cloud.google.com/storage/docs/nearline-storage https://cloud.google.com/storage/docs/durable-reduced-availability
Return type: string or NoneTypeReturns: If set, one of “STANDARD”, “NEARLINE”, or “DURABLE_REDUCED_AVAILABILITY”, else None.
-
time_deleted# Retrieve the timestamp at which the object was deleted.
See: https://cloud.google.com/storage/docs/json_api/v1/objects
Return type: datetime.datetimeorNoneTypeReturns: Datetime object parsed from RFC3339 valid timestamp, or Noneif the property is not set locally. If the blob has not been deleted, this will never be set.
-
updated# Retrieve the timestamp at which the object was updated.
See: https://cloud.google.com/storage/docs/json_api/v1/objects
Return type: datetime.datetimeorNoneTypeReturns: Datetime object parsed from RFC3339 valid timestamp, or Noneif the property is not set locally.
-
upload_from_file(file_obj, rewind=False, size=None, content_type=None, num_retries=6, client=None)[source]# Upload the contents of this blob from a file-like object.
The content type of the upload will either be - The value passed in to the function (if any) - The value stored on the current blob - The default value of ‘application/octet-stream’
Note
The effect of uploading to an existing blob depends on the “versioning” and “lifecycle” policies defined on the blob’s bucket. In the absence of those policies, upload will overwrite any existing contents.
See the object versioning and lifecycle API documents for details.
Parameters: - file_obj (file) – A file handle open for reading.
- rewind (boolean) – If True, seek to the beginning of the file handle before writing the file to Cloud Storage.
- size (int) – The number of bytes to read from the file handle.
If not provided, we’ll try to guess the size using
os.fstat(). (If the file handle is not from the filesystem this won’t be possible.) - content_type (string or
NoneType) – Optional type of content being uploaded. - num_retries (integer) – Number of upload retries. Defaults to 6.
- client (
gcloud.storage.client.ClientorNoneType) – Optional. The client to use. If not passed, falls back to theclientstored on the blob’s bucket.
Raises: ValueErrorif size is not passed in and can not be determined
-
upload_from_filename(filename, content_type=None, client=None)[source]# Upload this blob’s contents from the content of a named file.
The content type of the upload will either be - The value passed in to the function (if any) - The value stored on the current blob - The value given by mimetypes.guess_type
Note
The effect of uploading to an existing blob depends on the “versioning” and “lifecycle” policies defined on the blob’s bucket. In the absence of those policies, upload will overwrite any existing contents.
See the object versioning and lifecycle API documents for details.
Parameters: - filename (string) – The path to the file.
- content_type (string or
NoneType) – Optional type of content being uploaded. - client (
gcloud.storage.client.ClientorNoneType) – Optional. The client to use. If not passed, falls back to theclientstored on the blob’s bucket.
-
upload_from_string(data, content_type='text/plain', client=None)[source]# Upload contents of this blob from the provided string.
Note
The effect of uploading to an existing blob depends on the “versioning” and “lifecycle” policies defined on the blob’s bucket. In the absence of those policies, upload will overwrite any existing contents.
See the object versioning and lifecycle API documents for details.
Parameters: - data (bytes or text) – The data to store in this blob. If the value is text, it will be encoded as UTF-8.
- content_type (string) – Optional type of content being uploaded. Defaults
to
'text/plain'. - client (
gcloud.storage.client.ClientorNoneType) – Optional. The client to use. If not passed, falls back to theclientstored on the blob’s bucket.