google.datalab.storage Module

Google Cloud Platform library - Cloud Storage Functionality.

class google.datalab.storage.Bucket(name, info=None, context=None)[source]

Represents a Cloud Storage bucket.

Initializes an instance of a Bucket object.

Parameters:
  • name – the name of the bucket.
  • info – the information about the bucket if available.
  • context – an optional Context object providing project_id and credentials. If a specific project id or credentials are unspecified, the default ones configured at the global level are used.
create(context=None)[source]

Creates the bucket.

Parameters:context – the context object to use when creating the bucket.
Returns:The bucket.
Raises:Exception if there was an error creating the bucket.
delete()[source]

Deletes the bucket.

Raises:Exception if there was an error deleting the bucket.
exists()[source]

Checks if the bucket exists.

metadata

Retrieves metadata about the bucket.

Returns:A BucketMetadata instance with information about this bucket.
Raises:Exception if there was an error requesting the bucket’s metadata.
name

The name of the bucket.

object(key)[source]

Retrieves a Storage Object for the specified key in this bucket.

The object need not exist.

Parameters:key – the key of the object within the bucket.
Returns:An Object instance representing the specified key.
objects(prefix=None, delimiter=None)[source]

Get an iterator for the objects within this bucket.

Parameters:
  • prefix – an optional prefix to match objects.
  • delimiter – an optional string to simulate directory-like semantics. The returned objects will be those whose names do not contain the delimiter after the prefix. For the remaining objects, the names will be returned truncated after the delimiter with duplicates removed (i.e. as pseudo-directories).
Returns:

An iterable list of objects within this bucket.

class google.datalab.storage.Buckets(context=None)[source]

Represents a list of Cloud Storage buckets for a project.

Initializes an instance of a BucketList.

Parameters:context – an optional Context object providing project_id and credentials. If a specific project id or credentials are unspecified, the default ones configured at the global level are used.
contains(name)[source]

Checks if the specified bucket exists.

Parameters:name – the name of the bucket to lookup.
Returns:True if the bucket exists; False otherwise.
Raises:Exception if there was an error requesting information about the bucket.
class google.datalab.storage.Object(bucket, key, info=None, context=None)[source]

Represents a Cloud Storage object within a bucket.

Initializes an instance of an Object.

Parameters:
  • bucket – the name of the bucket containing the object.
  • key – the key of the object.
  • info – the information about the object if available.
  • context – an optional Context object providing project_id and credentials. If a specific project id or credentials are unspecified, the default ones configured at the global level are used.
copy_to(new_key, bucket=None)[source]

Copies this object to the specified new key.

Parameters:
  • new_key – the new key to copy this object to.
  • bucket – the bucket of the new object; if None (the default) use the same bucket.
Returns:

An Object corresponding to new key.

Raises:

Exception if there was an error copying the object.

delete()[source]

Deletes this object from its bucket.

Raises:Exception if there was an error deleting the object.
download()[source]

Reads the content of this object.

Returns:The content within the object.
Raises:Exception if there was an error requesting the object’s content.
exists()[source]

Checks if the object exists.

key

Returns the key of the object.

metadata

Retrieves metadata about the object.

Returns:An ObjectMetadata instance with information about this object.
Raises:Exception if there was an error requesting the object’s metadata.
read_lines(max_lines=None)[source]

Reads the content of this object as text, and return a list of lines up to some max.

Parameters:max_lines – max number of lines to return. If None, return all lines.
Returns:The text content of the object as a list of lines.
Raises:Exception if there was an error requesting the object’s content.
read_stream(start_offset=0, byte_count=None)[source]

Reads the content of this object as text.

Parameters:
  • start_offset – the start offset of bytes to read.
  • byte_count – the number of bytes to read. If None, it reads to the end.
Returns:

The text content within the object.

Raises:

Exception if there was an error requesting the object’s content.

upload(content)[source]

Uploads content to this object.

Parameters:content – the text content to be written.
Raises:Exception if there was an error requesting the object’s content.
uri

Returns the gs – // URI for the object.

write_stream(content, content_type)[source]

Writes text content to this object.

Parameters:
  • content – the text content to be written.
  • content_type – the type of text content.
Raises:

Exception if there was an error requesting the object’s content.

class google.datalab.storage.Objects(bucket, prefix, delimiter, context=None)[source]

Represents a list of Cloud Storage objects within a bucket.

Initializes an instance of an ObjectList.

Parameters:
  • bucket – the name of the bucket containing the objects.
  • prefix – an optional prefix to match objects.
  • delimiter – an optional string to simulate directory-like semantics. The returned objects will be those whose names do not contain the delimiter after the prefix. For the remaining objects, the names will be returned truncated after the delimiter with duplicates removed (i.e. as pseudo-directories).
  • context – an optional Context object providing project_id and credentials. If a specific project id or credentials are unspecified, the default ones configured at the global level are used.
contains(key)[source]

Checks if the specified object exists.

Parameters:key – the key of the object to lookup.
Returns:True if the object exists; False otherwise.
Raises:Exception if there was an error requesting information about the object.