无法将谷歌云中的blob从一个bucket复制到另一个bucket,请查找下面的代码

2024-10-02 18:28:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将文件从一个Google云存储桶复制到GCP中的另一个存储桶,该功能已成功部署,但不起作用

请参见以下示例代码:

from google.cloud import storage
import os
def copy(
    bucket_name, blob_name, destination_bucket_name, destination_blob_name
):
    """Copies a blob from one bucket to another with a new name."""
    bucket_name = "ups_vikky_test56"
    blob_name = "city.csv"
    destination_bucket_name = "ups_vikky_test57"
    destination_blob_name = "city.csv" + "temp"


    storage_client = storage.Client()

    source_bucket = storage_client.bucket(bucket_name)
    source_blob = source_bucket.blob(blob_name)
    destination_bucket = storage_client.bucket(destination_bucket_name)

    blob_copy = source_bucket.copy_blob(
        source_blob, destination_bucket, destination_blob_name
    )

    print(
        "Blob {} in bucket {} copied to blob {} in bucket {}.".format(
            source_blob.name,
            source_bucket.name,
            blob_copy.name,
            destination_bucket.name,
        )
    )

Tags: csvtonamefromimportclientcitysource
1条回答
网友
1楼 · 发布于 2024-10-02 18:28:35

我测试了代码,它工作正常。首先,在部署代码之前,确保云函数的入口点是正确的。它必须是要执行的源代码中的函数名,在本例中为copy

最后,如果您使用HTTP触发器进行部署,则必须始终传递一个参数,并且即使未使用该参数,也需要包含该参数。如果您使用的是后台函数,则必须有两个参数。请参阅本documentation中的概述和示例

下面是一个带有HTTP触发器的函数示例:

from google.cloud import storage
import os
def copy(request):
    """Copies a blob from one bucket to another with a new name."""
    bucket_name = "ups_vikky_test56"
    blob_name = "city.csv"
    destination_bucket_name = "ups_vikky_test57"
    destination_blob_name = "city.csv" + "temp"

    # ... the rest of your logic

相关问题 更多 >