使用Python从Google存储签名的URL流式传输到另一个Google存储桶

问题描述 投票:0回答:1

我得到一个有效的 Google Storage 签名 url 作为输入以供读取

我正在寻找最优雅的方式使用Python将文件从这个签名的URL流式传输/上传到另一个谷歌存储桶

我想通过生成上传签名的网址,然后从读取签名的网址(我作为输入获得)将文件流式传输到该文件会更方便,但我也对其他解决方案持开放态度

谢谢!

python google-cloud-storage signed-url
1个回答
0
投票

我有这个解决方案,但我想知道是否有更好的方法

pip install google-cloud-storage requests

然后

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)
© www.soinside.com 2019 - 2024. All rights reserved.