【问题标题】:Cloud Function in Python3 - copy from Google Cloud Bucket to another Google Cloud BucketPython3 中的云函数 - 从 Google Cloud Bucket 复制到另一个 Google Cloud Bucket
【发布时间】:2021-01-07 08:04:46
【问题描述】:

Google 有 Cloud Storage 数据传输选项,可以从一个存储桶复制到另一个存储桶,但这只有在两个存储桶都在同一个项目中时才有效。使用 gutil -m rsync -r -d 是作为 cron 运行的一个简单选项,但我们正在将所有 bash 迁移到 python3。所以我需要一个 python 3 脚本将其用作谷歌云功能,每周将整个存储桶从 project1 复制到 project2 中的另一个存储桶。

Language: python 3
app     : Cloud Function
Process : Copy one bucket to another
Source Project: project1
Source bucket : bucket1
Dest Project: project2
Dest Bucket: bucket2
pseudo cmd: rsync -r gs://project1/bucket1 gs://project2/bucket2

任何快速且可读的 python 3 代码脚本都可以做到这一点。

【问题讨论】:

    标签: python-3.x google-cloud-storage


    【解决方案1】:

    执行此操作的 python 脚本会真的很慢。 我会使用 Dataflow (apache bream) 批处理来执行此操作。你可以很容易地在 python3 中编写代码。

    基本上你需要:

    • 一个操作列出所有文件。
    • 一个 shuffle() 操作将负载分配给多个工作人员。
    • 从源实际复制到目标的一个操作。

    好处是 Google 会为您扩展工作人员,而且不会花费太多时间。 您需要为移动数据所需的存储操作和千兆字节 + cpu 付费。

    【讨论】:

    • 确实 py3 会很慢,但这是我的要求。我对 abt 数据流不太了解,但感谢您提及它,出于学习的好奇心,我会挖掘它,
    【解决方案2】:

    Rsync 不是无法通过storage rest API 中的单个请求执行的操作,并且gsutil 在 Cloud Functions 上不可用,因此无法通过 python 脚本对两个存储桶进行 rsync。

    您可以创建一个函数以使用startup script 启动preemptible VM,该函数在存储桶之间执行rsync,并在完成rsync 操作后关闭实例。

    通过使用虚拟机而不是无服务器服务,您可以避免长时间 rsync 进程可能产生的任何超时。

    抢占式虚拟机在停止前最多可以运行 24 小时,并且仅在实例开启时收费(磁盘存储将独立于状态收费)

    如果虚拟机在一分钟前关闭,则不会按使用量收费。

    对于这种方法,首先需要在存储桶中创建一个 bash 脚本,这将由可抢占式 VM 在启动时执行,例如:

    #! /bin/bash
    gstuil rsync -r gs://mybucket1 gs://mybucket2 
    
    sudo init 0 #this is similar to poweroff, halt or shutdown -h now
    

    之后,您需要使用启动脚本创建一个抢占式 VM,我推荐一个 f1-micro 实例,因为存储桶之间的 rsync 命令不需要太多资源。

    1.- 转到VM Instances page

    2.- 点击创建实例。

    3.- 在创建新实例页面上,填写您的实例的属性。

    4.- 点击管理、安全、磁盘、网络、单租。

    5.在身份和 API 访问部分中,选择有权读取 Cloud Storage 中的启动脚本文件和要同步的存储桶的服务帐号

    1. 选择允许完全访问所有云 API。

    7.- 在可用性策略下,将抢占性选项设置为开。 此设置禁用实例的自动重启,并将主机维护操作设置为终止。

    8.- 在 Metadata 部分,提供 startup-script-url 作为元数据键。

    9.- 在“值”框中,提供启动脚本文件的 URL,格式为 gs://BUCKET/FILE 或 https://storage.googleapis.com/BUCKET/FILE

    10.点击创建创建实例。

    使用此配置,每次启动您的实例时,脚本也会执行。

    这是启动VM的python函数(如果这是可抢占的,则独立)

    def power(request):
        import logging
        # this libraries are mandatory to reach compute engine api
        from googleapiclient import discovery
        from oauth2client.client import GoogleCredentials
    
        # the function will take the service account of your function
        credentials = GoogleCredentials.get_application_default()
    
        # this line is to specify the api that we gonna use, in this case compute engine    
        service = discovery.build('compute', 'v1', credentials=credentials, cache_discovery=False)
    
        # set correct log level (to avoid noise in the logs)
        logging.getLogger('googleapiclient.discovery_cache').setLevel(logging.ERROR)
    
        # Project ID for this request.
        project = "yourprojectID"  # Update placeholder value.
        zone = "us-central1-a"  # update this to the zone of your vm
        instance = "myvm"  # update with the name of your vm
    
        response = service.instances().start(project=project, zone=zone, instance=instance).execute()
    
        print(response)
        return ("OK")
    

    requirements.txt 文件

    google-api-python-client
    oauth2client
    flask
    

    您可以通过 Cloud Scheduler 来安排您的功能:

    1. Create a service accountfunctions.invoker permission 在你的函数中
    2. Create new Cloud scheduler job
    3. 以 cron 格式指定频率。
    4. 将 HTTP 指定为目标类型。
    5. 一如既往地添加您的云函数和方法的 URL。
    6. 从 Auth 标头下拉列表中选择令牌 OIDC
    7. 在服务帐户文本框中添加服务帐户电子邮件。
    8. audience field中只需要写函数的URL,不需要任何附加参数

    在云调度程序上,我使用这些 URL 来实现我的功能

    https://us-central1-yourprojectID.cloudfunctions.net/power

    我使用了这些观众

    https://us-central1-yourprojectID.cloudfunctions.net/power

    请替换代码中的yourprojectID,在网址和区域us-central1

    【讨论】:

    • @JAHermadez 好吧,正如我的问题中提到的那样,我非常擅长在我们的环境中使用 python3。我们所有的环境都完全不同,场景也是如此。为了实现这一点,我们已经有内部调度程序并编写了一个 bash gsutil -m rsync -r -d 并安排它来完成这项工作,但我的要求是使用 py3 来执行存储桶复制。感谢您对此方法的详细解释。
    • @SAGARBHOOSHAN 您希望移动多少文件?多少 GB?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    • 1970-01-01
    • 2017-11-08
    • 2020-06-13
    • 2018-10-09
    • 2018-02-22
    • 2019-03-02
    相关资源
    最近更新 更多