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:
- bucket_name: (str) The name of the GCS bucket to clean up.
- 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.
- 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
- Connects to GCS using storage.Client.
- Retrieves the bucket specified by bucket_name.
- Lists all blobs (objects) in the bucket, optionally filtered by folder_path.
- 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.
- Prints a summary message indicating the number of deleted objects, bucket name, folder path (if provided), and the used substring (if provided).
Additional Notes:
- This function permanently deletes objects from GCS. Use caution and ensure you have backups if necessary.
- Be specific with folder_path and substring to avoid unintended deletions.
- This library uses the google-cloud-storage library. Ensure it is installed (pip install google-cloud-storage).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file Clean_up_gcs_bucket-0.1.tar.gz
.
File metadata
- Download URL: Clean_up_gcs_bucket-0.1.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5166e6c318a0cb67680d3ae535eb7fca9e3c16798e692c164cb96929d8a6cd83 |
|
MD5 | 0dd463ba7b24e9fb5eee670312116206 |
|
BLAKE2b-256 | 4f4b99f185342a085726b84d1c38aa899b3291da5e809bf3b968c6b483200bb8 |
File details
Details for the file Clean_up_gcs_bucket-0.1-py3-none-any.whl
.
File metadata
- Download URL: Clean_up_gcs_bucket-0.1-py3-none-any.whl
- Upload date:
- Size: 3.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96fa2d3daebc2e400ecabed78f0b0f0a0b672c4e7281f4e10d75ea6138456bda |
|
MD5 | 012953d9346b1fe450a14212565e78fe |
|
BLAKE2b-256 | 55adc198e4059e046c65c915d20fab88a0cd0d28a3f8eab7bda8b6ccd1ea6a1b |