<small id='ClCKQ'></small><noframes id='ClCKQ'>

    <i id='ClCKQ'><tr id='ClCKQ'><dt id='ClCKQ'><q id='ClCKQ'><span id='ClCKQ'><b id='ClCKQ'><form id='ClCKQ'><ins id='ClCKQ'></ins><ul id='ClCKQ'></ul><sub id='ClCKQ'></sub></form><legend id='ClCKQ'></legend><bdo id='ClCKQ'><pre id='ClCKQ'><center id='ClCKQ'></center></pre></bdo></b><th id='ClCKQ'></th></span></q></dt></tr></i><div id='ClCKQ'><tfoot id='ClCKQ'></tfoot><dl id='ClCKQ'><fieldset id='ClCKQ'></fieldset></dl></div>

        <bdo id='ClCKQ'></bdo><ul id='ClCKQ'></ul>
    1. <legend id='ClCKQ'><style id='ClCKQ'><dir id='ClCKQ'><q id='ClCKQ'></q></dir></style></legend>
      <tfoot id='ClCKQ'></tfoot>

      Google云功能,使用python将源Bucket的所有数据复制到另一个Bucket

      时间:2024-08-10

        • <bdo id='4EKVx'></bdo><ul id='4EKVx'></ul>
            <tbody id='4EKVx'></tbody>

              <tfoot id='4EKVx'></tfoot>
                <i id='4EKVx'><tr id='4EKVx'><dt id='4EKVx'><q id='4EKVx'><span id='4EKVx'><b id='4EKVx'><form id='4EKVx'><ins id='4EKVx'></ins><ul id='4EKVx'></ul><sub id='4EKVx'></sub></form><legend id='4EKVx'></legend><bdo id='4EKVx'><pre id='4EKVx'><center id='4EKVx'></center></pre></bdo></b><th id='4EKVx'></th></span></q></dt></tr></i><div id='4EKVx'><tfoot id='4EKVx'></tfoot><dl id='4EKVx'><fieldset id='4EKVx'></fieldset></dl></div>
                <legend id='4EKVx'><style id='4EKVx'><dir id='4EKVx'><q id='4EKVx'></q></dir></style></legend>

                <small id='4EKVx'></small><noframes id='4EKVx'>

              • 本文介绍了Google云功能,使用python将源Bucket的所有数据复制到另一个Bucket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想使用Google云功能将数据从一个存储桶复制到另一个存储桶。此时,我只能将单个文件复制到目标,但我希望将所有文件、文件夹和子文件夹复制到我的目标存储桶。

                
                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!"
                

                REQUIREMENTS.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>
                

                这篇关于Google云功能,使用python将源Bucket的所有数据复制到另一个Bucket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:将CSV写入Google云存储 下一篇:如何在Django Admin中设置另一个内联标题?

                相关文章

                1. <legend id='N1d7n'><style id='N1d7n'><dir id='N1d7n'><q id='N1d7n'></q></dir></style></legend>
                2. <tfoot id='N1d7n'></tfoot>
                  <i id='N1d7n'><tr id='N1d7n'><dt id='N1d7n'><q id='N1d7n'><span id='N1d7n'><b id='N1d7n'><form id='N1d7n'><ins id='N1d7n'></ins><ul id='N1d7n'></ul><sub id='N1d7n'></sub></form><legend id='N1d7n'></legend><bdo id='N1d7n'><pre id='N1d7n'><center id='N1d7n'></center></pre></bdo></b><th id='N1d7n'></th></span></q></dt></tr></i><div id='N1d7n'><tfoot id='N1d7n'></tfoot><dl id='N1d7n'><fieldset id='N1d7n'></fieldset></dl></div>
                      <bdo id='N1d7n'></bdo><ul id='N1d7n'></ul>

                  1. <small id='N1d7n'></small><noframes id='N1d7n'>