python - 谷歌云函数使用python将源存储桶的所有数据复制到另一个存储桶

标签 python google-cloud-platform google-cloud-functions google-cloud-storage

我想使用谷歌云功能将数据从一个存储桶复制到另一个存储桶。目前,我只能将单个文件复制到目标,但我想将所有文件、文件夹和子文件夹复制到目标存储桶。


from google.cloud import storage
def copy_blob(bucket_name= "loggingforproject", blob_name= "assestnbfile.json", destination_bucket_name= "test-assest", destination_blob_name= "logs"):
    """Copies a blob from one bucket to another with a new name."""
    bucket_name = "loggingforproject"
    blob_name = "assestnbfile.json"
    destination_bucket_name = "test-assest"
    destination_blob_name = "logs"

    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,
        )
    )

最佳答案

使用gsutil cp是一个不错的选择。但是,如果您想使用云函数复制文件 - 也可以实现。

目前,您的函数仅复制一个文件。为了复制存储桶的全部内容,您需要迭代其中的文件。

这是我为 HTTP 云函数编写并测试的代码示例 - 您可以将其用作引用:

MAIN.PY

from google.cloud import storage

def copy_bucket_files(request):
    """
    Copies the files from a specified bucket into the selected one.
    """

    # Check if the bucket's name was specified in the request
    if request.args.get('bucket'):
        bucketName = request.args.get('bucket')
    else:
        return "The bucket name was not provided. Please try again."

    try:
        # Initiate Cloud Storage client
        storage_client = storage.Client()
        # Define the origin bucket
        origin = storage_client.bucket(bucketName)
        # Define the destination bucket
        destination = storage_client.bucket('<my-test-bucket>')

        # Get the list of the blobs located inside the bucket which files you want to copy
        blobs = storage_client.list_blobs(bucketName)

        for blob in blobs:
            origin.copy_blob(blob, destination)

        return "Done!"

    except:
        return "Failed!"

要求.TXT

google-cloud-storage==1.22.0
<小时/>

如何调用该函数:

可以通过为触发该功能而提供的URL来调用它,方法是将该URL附加/?bucket=<name-of-the-bucket-to-copy> (名称不带 <> ):

https://<function-region>-<project-name>.cloudfunctions.net/<function-name>/?bucket=<bucket-name>

关于python - 谷歌云函数使用python将源存储桶的所有数据复制到另一个存储桶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59592828/

相关文章:

python - 如何在 matplotlib 颜色条中创建自定义断点?

python - 如何从 DataFrame 在谷歌存储中创建 Json 文件?

python - 如何使用 Python 以追加模式打开 GCS 存储桶中的文件?

google-app-engine - gcloud app deploy 停留在更新服务 [默认]...失败。应用程序启动错误...您是要运行 dotnet SDK 命令吗?

javascript - 如何在云功能中获取 firebase 数据库数据?

firebase - 如何解析 firebase 云函数上的多部分/表单数据?

python - 以视觉可读的方式将张量写入文件

python - 离散 pyplot 散点颜色条

python - 如何覆盖 scrapy 1.7.3 中的 file_path 函数?

google-cloud-platform - GCP 发布订阅 : "The request was aborted because there was no available instance." - Doesn't Retry on Failure