Skip to main content

No project description provided

Project description

Python Library for Deleting Objects in Google Cloud Storage Buckets

This library provides a function clean_up_gcs_bucket that helps you delete unwanted objects from a Google Cloud Storage (GCS) bucket based on a folder path and substring criteria.

** clean_up_gcs_bucket(bucket_name, folder_path, substring): **

The clean_up_gcs_bucket function takes the following arguments:

Parameters:

  1. bucket_name: (str) The name of the GCS bucket to clean up.
  2. folder_path (str, optional): An optional prefix to filter objects within a specific folder path in the bucket. Include a trailing slash. Defaults to listing all objects in the bucket.
  3. substring (str, optional): An optional substring to filter objects. Objects containing the substring (case-insensitive) will be deleted. Defaults to deleting all objects matching the folder_path criteria.

Example Usage:

from clean_up_gcs_bucket import clean_up_gcs_bucket

# Replace with your information
bucket_name = "your-bucket-name"
folder_path = "path/to/your/folder/"  # Optional, defaults to all files in bucket
substring = "report"  # Optional, deletes objects containing the substring (case-insensitive)

clean_up_gcs_bucket(bucket_name, folder_path, substring)

Function Behavior

  1. Connects to GCS using storage.Client.
  2. Retrieves the bucket specified by bucket_name.
  3. Lists all blobs (objects) in the bucket, optionally filtered by folder_path.
  4. Iterates through each file in the bucket: a). Checks if the substring is provided: 1. If not provided, all objects matching the folder_path (if specified) will be deleted. 2. If provided, objects containing the substring (case-insensitive) and matching the folder_path (if specified) will be deleted. b). Deletes the matching blob using blob.delete(). c). Keeps a count of deleted objects.
  5. Prints a summary message indicating the number of deleted objects, bucket name, folder path (if provided), and the used substring (if provided).

Additional Notes:

  1. This function permanently deletes objects from GCS. Use caution and ensure you have backups if necessary.
  2. Be specific with folder_path and substring to avoid unintended deletions.
  3. This library uses the google-cloud-storage library. Ensure it is installed (pip install google-cloud-storage).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Clean_up_gcs_bucket-0.0.tar.gz (2.4 kB view details)

Uploaded Source

Built Distribution

Clean_up_gcs_bucket-0.0-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file Clean_up_gcs_bucket-0.0.tar.gz.

File metadata

  • Download URL: Clean_up_gcs_bucket-0.0.tar.gz
  • Upload date:
  • Size: 2.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for Clean_up_gcs_bucket-0.0.tar.gz
Algorithm Hash digest
SHA256 1770f43f0dd58d1361407fed9546862f1f25a55daedaaac5ba3fec7e09548ee3
MD5 0fb10375aaa6f2ab1d7344504e4e1f43
BLAKE2b-256 349cfeeb8145bd0ee3baaaa4b04cb250209eaf8ca818b0e26d8d7ab7fa60ec21

See more details on using hashes here.

File details

Details for the file Clean_up_gcs_bucket-0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for Clean_up_gcs_bucket-0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a994e9dd98f54a56be4fdc402f9405bb262a154c0c161197cfce866b0bc212dc
MD5 995de29977a0ead0e0944fbb5369af60
BLAKE2b-256 7b6d8949087c4e3c4ffde618b80768fe3b5b2fe1beb34b507a9caceed7db3f3d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page