Skip to content

Instantly share code, notes, and snippets.

@spicydog
Created October 24, 2020 16:00
Show Gist options
  • Save spicydog/277a90dacfb79d8ec7f07c4741b68ed1 to your computer and use it in GitHub Desktop.
Save spicydog/277a90dacfb79d8ec7f07c4741b68ed1 to your computer and use it in GitHub Desktop.
A Google Cloud Function to copy/clone a file from one Google Cloud Storage bucket to another with the same file path.
import os
from google.cloud import storage
OUTPUT_BUCKET = os.environ['OUTPUT_BUCKET']
def clone_gcs_file(event, context):
print(f"rev.4")
INPUT_BUCKET = event['bucket']
TEMP_FILE_PATH = '/tmp/data.temp'
filename = event['name']
client = storage.Client()
print(f'triggered {filename}')
# Read data from input bucket
bucket = client.get_bucket(INPUT_BUCKET)
blob = bucket.get_blob(filename)
f = open(TEMP_FILE_PATH, "wb")
f.write(blob.download_as_string())
f.close()
print(f'loaded {filename}')
# Write data to output bucket
bucket = client.get_bucket(OUTPUT_BUCKET)
output_path = filename
blob = bucket.blob(output_path)
blob.upload_from_filename(filename=f'{TEMP_FILE_PATH}')
print(f'cloned {filename}')
# Function dependencies, for example:
# package>=version
google-cloud-storage==1.29.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment