【问题标题】:How to move files in Google Cloud Storage from one bucket to another bucket by Python如何通过 Python 将 Google Cloud Storage 中的文件从一个存储桶移动到另一个存储桶
【发布时间】:2022-02-27 18:25:16
【问题描述】:

是否有任何 API 函数允许我们将 Google Cloud Storage 中的文件从一个存储桶移动到另一个存储桶?

场景是我们希望 Python 将 A 存储桶中的读取文件移动到 B 存储桶。我知道 gsutil 可以做到这一点,但不确定 Python 是否支持。

谢谢。

【问题讨论】:

  • 在 Google Compute Engine 中,您甚至可以从 Python 应用程序运行外部 gsutil 命令来移动文件。
  • 您使用的是哪个 Python API?

标签: python google-cloud-storage


【解决方案1】:

这是我在同一存储桶内的目录之间移动 Blob 或移动到不同存储桶时使用的函数。

from google.cloud import storage
import os
    
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="path_to_your_creds.json"

def mv_blob(bucket_name, blob_name, new_bucket_name, new_blob_name):
    """
    Function for moving files between directories or buckets. it will use GCP's copy 
    function then delete the blob from the old location.
    
    inputs
    -----
    bucket_name: name of bucket
    blob_name: str, name of file 
        ex. 'data/some_location/file_name'
    new_bucket_name: name of bucket (can be same as original if we're just moving around directories)
    new_blob_name: str, name of file in new directory in target bucket 
        ex. 'data/destination/file_name'
    """
    storage_client = storage.Client()
    source_bucket = storage_client.get_bucket(bucket_name)
    source_blob = source_bucket.blob(blob_name)
    destination_bucket = storage_client.get_bucket(new_bucket_name)

    # copy to new destination
    new_blob = source_bucket.copy_blob(
        source_blob, destination_bucket, new_blob_name)
    # delete in old destination
    source_blob.delete()
    
    print(f'File moved from {source_blob} to {new_blob_name}')

【讨论】:

    【解决方案2】:

    使用google-api-python-client,storage.objects.copy 页面上有一个示例。复制后,您可以使用storage.objects.delete删除源。

    destination_object_resource = {}
    req = client.objects().copy(
            sourceBucket=bucket1,
            sourceObject=old_object,
            destinationBucket=bucket2,
            destinationObject=new_object,
            body=destination_object_resource)
    resp = req.execute()
    print json.dumps(resp, indent=2)
    
    client.objects().delete(
            bucket=bucket1,
            object=old_object).execute()
    

    【讨论】:

    【解决方案3】:

    您可以使用 [1] 中记录的 GCS 客户端库函数读取一个存储桶并写入另一个存储桶,然后删除源文件。

    您甚至可以使用 [2] 中记录的 GCS REST API。

    链接:
    [1] - https://developers.google.com/appengine/docs/python/googlecloudstorageclient/functions
    [2] - https://developers.google.com/storage/docs/concepts-techniques#overview

    【讨论】:

    • 请注意,为 #1 记录的库是为与 App Engine 一起使用而设计的。如果您没有使用 App Engine,链接 #2 是您的最佳选择。
    【解决方案4】:
    def GCP_BUCKET_A_TO_B():                                                                           
        source_bucket = storage_client.get_bucket("Bucket_A_Name")
        filename = [filename.name for filename in 
        list(source_bucket.list_blobs(prefix=""))]
        for i in range (0,len(filename)):
            source_blob = source_bucket.blob(filename[i])
            destination_bucket = storage_client.get_bucket("Bucket_B_Name")
            new_blob = source_bucket.copy_blob(
                source_blob, destination_bucket, filename[i])  
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。
    猜你喜欢
    • 2015-05-07
    • 2016-02-10
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 2020-05-29
    • 2013-11-01
    • 2020-09-15
    相关资源
    最近更新 更多