geeViz.cloudStorageManagerLib

Helpful functions for managing Google Cloud Storage (GCS) buckets and blobs

geeViz.cloudStorageManagerLib includes functions for renaming, seeing if a blob exists, and deleting blobs.

Functions

bucket_exists(bucket_name)

Checks if a GCS bucket exists.

create_bucket(bucket_name)

Creates a GCS bucket.

delete_blob(bucket, filename)

Deletes a specified file from a GCS bucket.

gcs_exists(bucket, filename)

Checks if a specific file exists in a GCS bucket.

list_blobs(bucket_name)

Lists all blob objects in a bucket.

list_files(bucket_name)

Lists filenames in a bucket.

rename_blobs(bucket_name, old_name, new_name)

Renames a group of blobs in a bucket.

geeViz.cloudStorageManagerLib.list_blobs(bucket_name: str) list[source]

Lists all blob objects in a bucket.

Parameters:

bucket_name (str) – The name of the GCS bucket.

Returns:

A list of blob objects in the bucket.

Return type:

list

Example

>>> blobs = list_blobs("my-bucket")
>>> for blob in blobs:
...     print(blob.name)
geeViz.cloudStorageManagerLib.list_files(bucket_name: str) list[str][source]

Lists filenames in a bucket.

Parameters:

bucket_name (str) – The name of the GCS bucket.

Returns:

A list of filenames in the bucket.

Return type:

list[str]

Example

>>> files = list_files("my-bucket")
>>> for file in files:
...     print(file)
geeViz.cloudStorageManagerLib.bucket_exists(bucket_name: str) bool[source]

Checks if a GCS bucket exists.

Parameters:

bucket_name (str) – The name of the GCS bucket.

Returns:

True if the bucket exists, False otherwise.

Return type:

bool

Example

>>> if bucket_exists("my-bucket"):
...     print("Bucket exists.")
... else:
...     print("Bucket does not exist.")
geeViz.cloudStorageManagerLib.create_bucket(bucket_name: str)[source]

Creates a GCS bucket.

Parameters:

bucket_name (str) – The name of the GCS bucket to create.

Returns:

The created bucket object.

Return type:

google.cloud.storage.bucket.Bucket

Example

>>> bucket = create_bucket("my-new-bucket")
>>> print(f"Created bucket: {bucket.name}")
geeViz.cloudStorageManagerLib.rename_blobs(bucket_name, old_name, new_name)[source]

Renames a group of blobs in a bucket.

Parameters:
  • bucket_name (str) – The name of the GCS bucket.

  • old_name (str) – The substring to replace in blob names.

  • new_name (str) – The new substring to use in blob names.

Example

>>> rename_blobs("my-bucket", "old_prefix", "new_prefix")
geeViz.cloudStorageManagerLib.gcs_exists(bucket, filename)[source]

Checks if a specific file exists in a GCS bucket.

Parameters:
  • bucket (str) – The name of the GCS bucket.

  • filename (str) – The name of the file to check.

Returns:

True if the file exists, False otherwise.

Return type:

bool

Example

>>> if gcs_exists("my-bucket", "file.txt"):
...     print("File exists.")
... else:
...     print("File does not exist.")
geeViz.cloudStorageManagerLib.delete_blob(bucket, filename)[source]

Deletes a specified file from a GCS bucket.

Parameters:
  • bucket (str) – The name of the GCS bucket.

  • filename (str) – The name of the file to delete.

Example

>>> delete_blob("my-bucket", "file.txt")
>>> print("File deleted.")