Skip to content

Instantly share code, notes, and snippets.

@Neutrollized
Created June 3, 2022 15:23
Show Gist options
  • Save Neutrollized/c16529715735785ebae277503063a7b0 to your computer and use it in GitHub Desktop.
Save Neutrollized/c16529715735785ebae277503063a7b0 to your computer and use it in GitHub Desktop.
Medium: DIY Google Cloud Storage replication using Cloud Functions (delete_from_gcs)
import string
from google.cloud import storage
def delete_from_gcs(event, context):
print('Event ID: {}'.format(context.event_id))
print('Event type: {}'.format(context.event_type))
print('File: {}'.format(event['name']))
storage_client = storage.Client()
source_bucket = storage_client.bucket(event['bucket'])
source_blob = source_bucket.blob(event['name'])
temp_bucket_name = event['bucket'].split('-')[:-1]
temp_bucket_name.append('tor')
destination_bucket_name = '-'.join(temp_bucket_name)
destination_bucket = storage_client.bucket(destination_bucket_name)
destination_blob = destination_bucket.blob(event['name'])
blob_delete = destination_bucket.delete_blob(destination_blob.name)
print(
"Deleted gs://{}/{}.".format(
destination_bucket.name,
destination_blob.name,
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment