Last active
June 3, 2022 15:21
-
-
Save Neutrollized/0f44c35eefca9f0e2025e93601d72b1b to your computer and use it in GitHub Desktop.
Medium: DIY Google Cloud Storage replication using Cloud Functions (copy_to_gcs)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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