Stream from a google storage signed url to another google storage bucket with Python

53 Views Asked by At

I get as an input a valid Google Storage signed url for read

I am looking for the most elegant way to stream/upload the file from this signed url to another google storage bucket using Python

I guess it's more convenient to do so via generating an upload signed url and then streaming to it the file from the read signed url (which I get as an input), but I am open to other solutions too

Thanks!

1

There are 1 best solutions below

0
Alexander On

I have this solution, but I wonder if there is a better way

pip install google-cloud-storage requests

and then

import requests
from google.cloud import storage

def stream_and_upload(source_signed_url, destination_bucket, destination_blob_name):
    # Stream from the source signed URL
    with requests.get(source_signed_url, stream=True) as response:
        response.raise_for_status()
        
        # Set up Google Cloud Storage client
        storage_client = storage.Client()
        bucket = storage_client.bucket(destination_bucket)
        blob = bucket.blob(destination_blob_name)
        
        # Upload the streamed content to the destination bucket
        blob.upload_from_file(response.raw)

if __name__ == "__main__":
    # Replace with your actual values
    source_signed_url = "MY_SOURCE_SIGNED_URL"
    destination_bucket = "MY_DESTINATION_BUCKET"
    destination_blob_name = "MY_DESTINATION_BLOB_NAME"

    stream_and_upload(source_signed_url, destination_bucket, destination_blob_name)