Created
October 24, 2020 16:00
-
-
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.
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 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}') |
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
# 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