Blob¶
-
class
cloudstorage.base.
Blob
(name: str, checksum: str, etag: str, size: int, container: cloudstorage.base.Container, driver: cloudstorage.base.Driver, acl: typing.Union[typing.Dict[typing.Any, typing.Any], NoneType] = None, meta_data: typing.Union[typing.Dict[typing.Any, typing.Any], NoneType] = None, content_disposition: str = None, content_type: str = None, created_at: datetime.datetime = None, modified_at: datetime.datetime = None, expires_at: datetime.datetime = None) → None[source]¶ Represents an object blob.
picture_blob = container.get_blob('picture.png') picture_blob.size # 50301 picture_blob.checksum # '2f907a59924ad96b7478074ed96b05f0' picture_blob.content_type # 'image/png' picture_blob.content_disposition # 'attachment; filename=picture-attachment.png'
Parameters: - name (str) – Blob name (must be unique in container).
- checksum (str) – Checksum of this blob.
- etag (str) – Blob etag which can also be the checksum. The etag for
LocalDriver
is a SHA1 hexdigest of the blob’s full path. - size (int) – Blob size in bytes.
- container (Container) – Reference to the blob’s container.
- driver (Driver) – Reference to the blob’s container’s driver.
- meta_data (Dict[str, str] or None) – (optional) Metadata stored with the blob.
- acl (dict or None) – (optional) Access control list (ACL) for this blob.
- content_disposition (str or None) – (optional) Specifies presentational information for this blob.
- content_type (str or None) – (optional) A standard MIME type describing the format of the object data.
- created_at (datetime.datetime or None) – (optional) Creation time of this blob.
- modified_at (datetime.datetime or None) – (optional) Last modified time of this blob.
- expires_at (datetime.datetime or None) – (optional) Deletion or expiration time for this blob.
-
cdn_url
¶ The Content Delivery Network URL for this blob.
https://container-name.storage.com/picture.png
Returns: The CDN URL for this blob. Return type: str
-
path
¶ Relative URL path for this blob.
container-name/picture.png
Returns: The relative URL path to this blob. Return type: str
-
delete
() → None[source]¶ Delete this blob from the container.
picture_blob = container.get_blob('picture.png') picture_blob.delete() picture_blob in container # False
Returns: NoneType Return type: None Raises: NotFoundError – If the blob object doesn’t exist.
-
download
(destination: typing.Union[str, io.IOBase, typing.BinaryIO]) → None[source]¶ Download the contents of this blob into a file-like object or into a named file.
Filename:
picture_blob = container.get_blob('picture.png') picture_blob.download('/path/picture-copy.png')
File object:
Important
Always use write binary mode
wb
when downloading a blob to a file object.picture_blob = container.get_blob('picture.png') with open('/path/picture-copy.png', 'wb') as picture_file: picture_blob.download(picture_file)
Parameters: destination (file or str) – A file handle to which to write the blob’s data or a filename to be passed to open
.Returns: NoneType Return type: None Raises: NotFoundError – If the blob object doesn’t exist.
-
generate_download_url
(expires: int = 3600, method: str = 'GET', content_disposition: str = None, extra: typing.Union[typing.Dict[typing.Any, typing.Any], NoneType] = None) → str[source]¶ Generates a signed URL for this blob.
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. [1]
[1] Blobs / Objects — google-cloud 0.24.0 documentation Basic example:
import requests picture_blob = container.get_blob('picture.png') download_url = picture_blob.download_url(expires=3600) response = requests.get(download_url) # <Response [200]> with open('/path/picture-download.png', 'wb') as picture_file: for chunk in response.iter_content(chunk_size=128): picture_file.write(chunk)
Response Content-Disposition example:
picture_blob = container.get_blob('picture.png') params = { 'expires': 3600, 'content_disposition': 'attachment; filename=attachment.png' } download_url = picture_blob.download_url(**params) response = requests.get(download_url) # <Response [200]> response.headers['content-disposition'] # attachment; filename=attachment.png
References:
- Boto 3: S3.Client.generate_presigned_url
- Google Cloud Storage: generate_signed_url
- Rackspace: TempURL
Parameters: - expires (int) – (optional) Expiration in seconds.
- method (str) – (optional) HTTP request method. Defaults to
GET
. - content_disposition (str or None) – (optional) Sets the Content-Disposition header of the response.
- extra (Dict[str, str] or None) –
(optional) Extra parameters for the request.
- All
- content_type (str) – Sets the Content-Type header of the response.
- Google Cloud Storage
- version (str) – A value that indicates which generation of the resource to fetch.
- Amazon S3
- version_id (str) – Version of the object.
- All
Returns: Pre-signed URL for downloading a blob.
LocalDriver
returns urlsafe signature.Return type:
-
patch
() → None[source]¶ Saves all changed attributes for this blob.
Warning
Not supported by all drivers yet.
Returns: NoneType Return type: None Raises: NotFoundError – If the blob object doesn’t exist.