Storage

Implement api to access google storage API

class pysuite.storage.Storage(service: google.cloud.storage.client.Client)[source]

Bases: object

Class to interact with Google Storage API.

Parameters

service – an authorized Google Storage service client.

copy(from_object: str, to_object: str)[source]

Copy Google storage file or folder from one location to another. If from_object is a folder, this will copy it recursively.

Parameters
  • from_object – Source Google storage file or folder. This is a string that looks like “gs://xxxx”

  • to_object – Destination Google storage file or folder. This is a string that looks like “gs://xxxx”

Returns

None

create_bucket(bucket_name: str) → google.cloud.storage.bucket.Bucket[source]

Create a bucket in Google Storage.

Parameters

bucket_name – The name of the Google storage bucket.

Returns

None

download(from_object: str, to_object: Union[str, pathlib.PosixPath])[source]

Download target Google storage file or folder to local. If from_object is a folder, this method will download it recursively.

Parameters
  • from_object – Target Google storage path to be downloaded. This is a string that looks like “gs://xxxx”

  • to_object – Path to the local file or folder. If from_object is a file, this will be a file. If from_object is a folder, this will be a folder.

Returns

None

get_bucket(bucket_name: str) → google.cloud.storage.bucket.Bucket[source]

Get a Bucket object for the target Google storage bucket.

Parameters

bucket_name – The name of the target bucket.

Returns

A Bucket object for the target bucket.

list(target_object: str)[source]

Search Google storage target location and return an iterator. This iterator generates all files under the target location. If the target is a single file, the iterator only one object.

Parameters

target_object – Target Google storage location. This could be a file or a folder. This is a string that looks like “gs://xxxxx”

Returns

An iterator that iterates over the target location. Each item is a Blob object.

remove(target_object: str)[source]

Remove target Google storage file or folder. If target_object is a folder, this will remove it recursively.

Parameters

target_object – Target Google storage file or folder. This is a string that looks like “gs://xxxx”

Returns

None

remove_bucket(bucket_name: str, force: bool = False)[source]

Remove the target bucket.

Parameters
  • bucket_name – Target bucket name.

  • force – Whether force remove the target bucket. If True, even if the bucket is not empty, it will be removed. Default is False.

Returns

upload(from_object: Union[str, pathlib.PosixPath], to_object: str)[source]

Upload a file or a folder to google storage. If from_object is a folder, this method will upload it recursively.

Parameters
  • from_object – Path to the local file or folder to be uploaded.

  • to_object – Target Google storage object location. If from_object is a file, this will be a file. If from_object is a folder, this will be a folder. This is a string that looks like “gs://xxxxx”

Returns

None

pysuite.storage.is_gcs_uri(target_uri: str)[source]