Skip to content

Instantly share code, notes, and snippets.

@Neutrollized
Last active June 3, 2022 15:21
Show Gist options
  • Save Neutrollized/0f44c35eefca9f0e2025e93601d72b1b to your computer and use it in GitHub Desktop.
Save Neutrollized/0f44c35eefca9f0e2025e93601d72b1b to your computer and use it in GitHub Desktop.
Medium: DIY Google Cloud Storage replication using Cloud Functions (copy_to_gcs)
import string
from google.cloud import storage
def copy_to_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)
blob_copy = source_bucket.copy_blob(
source_blob, destination_bucket
)
print(
"gs://{}/{} replicated to gs://{}/{}.".format(
source_bucket.name,
source_blob.name,
destination_bucket.name,
blob_copy.name,
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment