gcs_client.gcs_object module

class gcs_client.gcs_object.Object(bucket=None, name=None, generation=None, credentials=None, retry_params=None, chunksize=None)[source]

Bases: gcs_client.base.Fillable

GCS Stored Object Object representation.

Variables:
  • bucket (string) – The name of the bucket containing this object.
  • contentType (string) – Content-Type of the object data.
  • crc32c (string) – CRC32c checksum, as described in RFC 4960, Appendix B; encoded using base64 in big-endian byte order.
  • etag (string) – HTTP 1.1 Entity tag for the object.
  • generation (long) – The content generation of this object. Used for object versioning.
  • id (string) – The ID of the object.
  • kind (string) – The kind of item this is. For objects, this is always storage#object.
  • md5Hash (string) – MD5 hash of the data; encoded using base64.
  • mediaLink (string) – Media download link.
  • metadata (dict) – User-provided metadata, in key/value pairs.
  • metageneration (long) – The version of the metadata for this object at this generation. Used for preconditions and for detecting changes in metadata. A metageneration number is only meaningful in the context of a particular generation of a particular object.
  • name (string) – The name of this object.
  • owner (dict) – The owner of the object. This will always be the uploader of the object. Contains entity and entityId keys.
  • selfLink (string) – The link to this object.
  • size (unsigned long) – Content-Length of the data in bytes.
  • storageClass (string) – Storage class of the object.
  • timeCreated (string) – The creation time of the object in RFC 3339 format.
  • timeDeleted (string) – The deletion time of the object in RFC 3339 format. Will be None if this version of the object has not been deleted.
  • updated (string) – The modification time of the object metadata in RFC 3339 format.

Initialize an Object object.

Parameters:
  • bucket (String) – Name of the bucket to use.
  • name (String) – Name of the object.
  • generation (long) – If present, selects a specific revision of this object (as opposed to the latest version, the default).
  • credentials (Credentials) – A credentials object to authorize the connection.
  • retry_params (RetryParams or NoneType) – Retry configuration used for communications with GCS. If None is passed default retries will be used.
  • chunksize (int) – Size in bytes of the payload to send/receive to/from GCS. Default is gcs_client.DEFAULT_BLOCK_SIZE
delete(*args, **kwargs)[source]

Deletes an object and its metadata.

Deletions are permanent if versioning is not enabled for the bucket, or if the generation parameter is used.

The authenticated user in the credentials must have WRITER permissions on the bucket.

Parameters:
  • generation (long) – If present, permanently deletes a specific revision of this object (as opposed to the latest version, the default).
  • if_generation_match (long) – Makes the operation conditional on whether the object’s current generation matches the given value.
  • if_generation_not_match (long) – Makes the operation conditional on whether the object’s current generation does not match the given value.
  • if_metageneration_match (long) – Makes the operation conditional on whether the object’s current metageneration matches the given value.
  • if_metageneration_not_match (long) – Makes the operation conditional on whether the object’s current metageneration does not match the given value.
Returns:

None

exists(*args, **kwargs)

Check if exists in GCS server.

open(*args, **kwargs)[source]

Open this object.

Parameters:
  • mode (String) – Mode to open the file with, ‘r’ for read and ‘w’ for writing are only supported formats. Default is ‘r’ if this argument is not provided.
  • chunksize (int) – Size in bytes of the payload to send/receive to/from GCS. Default chunksize is the one defined on object’s initialization.
credentials

Credentials used to connect to GCS server.

kind = 'storage#objects'
metadata = {}
retry_params

Get retry configuration used by this instance for accessing GCS.

timeDeleted = None
class gcs_client.gcs_object.GCSObjFile(bucket, name, credentials, mode='r', chunksize=None, retry_params=None, generation=None)[source]

Bases: object

Reader/Writer for GCS Objects.

Supports basic functionality:
  • Read
  • Write
  • Close
  • Seek
  • Tell

Instances support context manager behavior.

Initialize reader/writer of GCS object.

On initialization connection to GCS will be tested. For reading it’ll confirm the existence of the object in the bucket and for writing it’ll create the object (it won’t send any content).

Parameters:
  • bucket (String) – Name of the bucket to use.
  • name (String) – Name of the object.
  • credentials (gcs_client.Credentials) – A credentials object to authorize the connection.
  • mode (String) – Mode to open the file with, ‘r’ for read and ‘w’ for writing are only supported formats. Default is ‘r’ if this argument is not provided.
  • chunksize (int) – Size in bytes of the payload to send/receive to/from GCS. Default is gcs_client.DEFAULT_BLOCK_SIZE
  • retry_params (RetryParams or NoneType) – Retry configuration used for communications with GCS. If None is passed default retries will be used.
  • generation (long) – If present, selects a specific revision of this object (as opposed to the latest version, the default).
close()[source]

Close the file.

A closed file cannot be read or written any more. Any operation which requires that the file be open will raise an error after the file has been closed. Calling close() more than once is allowed.

read(size=None)[source]

Read data from the file.

Read at most size bytes from the file (less if the read hits EOF before obtaining size bytes). If the size argument is None, read all data until EOF is reached.

The bytes are returned as a bytes object. An empty string is returned when EOF is encountered immediately.

Note that this method may make multiple requests to GCS service in an effort to acquire as close to size bytes as possible.

Parameters:size (int) – Number of bytes to read.
Returns:Bytes with read data from GCS.
Return type:bytes
seek(offset, whence=0)[source]

Set the file’s current position, like stdio’s fseek().

Note that only files open for reading are seekable.

Parameters:
  • offset (int) – Offset to move the file cursor.
  • whence (int) – How to interpret the offset, defaults to os.SEEK_SET (0) -absolute file positioning- other values are os.SEEK_CUR (1) -seek relative to the current position- and os.SEEK_END (2) -seek relative to the file’s end-.
Returns:

None

tell()[source]

Return file’s current position from the beginning of the file.

write(data)[source]

Write a string to the file.

Due to buffering, the string may not actually show up in the file until we close the file or enough data to send another chunk has been buffered.

Parameters:data (String) – Data to write to the object.
Returns:None