Last active
June 13, 2022 18:05
-
-
Save djaquels/1b71e128fa8596969e44afe284132bb6 to your computer and use it in GitHub Desktop.
Transfer Files from GCS 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
#add google-cloud-storage == 2.1.0 to requirements.txt | |
""" | |
curl -m 70 -X POST https://<cf-url> \ | |
-H "Authorization:bearer $(gcloud auth print-identity-token)" \ | |
-H "Content-Type:application/json" \ | |
-d '{ | |
"source_bucket_name":"<replace-with-bucket-name>", | |
"destination_bucket_name":"<replace-with-dest-bucket-name>", | |
"folder":"<folder-to-transfer>/" | |
}' | |
""" | |
from google.cloud import storage | |
from zipfile import ZipFile | |
import os | |
def transfer(request): | |
"""Responds to any HTTP request. | |
Args: | |
request (flask.Request): HTTP request object. | |
Returns: | |
The response text or any set of values that can be turned into a | |
Response object using | |
`make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`. | |
""" | |
request_json = request.get_json() | |
# GCS Buckets | |
client = storage.Client() | |
# Params | |
# source | |
source_bucket_name = request_json["source_bucket_name"] | |
source_bucket = client.get_bucket(source_bucket_name) | |
folder = request_json["folder"] #prefix | |
#destination | |
destination_bucket_name = request_json["destination_bucket_name"] | |
destination_bucket = client.get_bucket(destination_bucket_name) | |
# Transfer Job | |
for blob in client.list_blobs(source_bucket_name, prefix=folder): | |
if blob.name == folder: | |
continue | |
print(blob.name) | |
file_name = str(blob.name) | |
file_path = "{}".format(file_name) | |
blob.download_to_filename("/tmp/{}".format(file_name.split("/")[-1])) | |
new_blob = destination_bucket.blob("{}".format(file_path)) | |
new_blob.upload_from_filename(filename="/tmp/{}".format(file_name.split("/")[-1])) | |
print("Transfer done") | |
return "200" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment