【问题标题】:How to invoke gsutil or use path of GCS objects to move data from GCS to s3 bucket using cloud function如何使用云功能调用 gsutil 或使用 GCS 对象的路径将数据从 GCS 移动到 s3 存储桶
【发布时间】:2019-06-07 11:53:37
【问题描述】:

我正在尝试使用 GC 函数(相当于 AWS Lambda)将文件从 GCS 移动到 s3 存储桶。为了实现它,我尝试了 3 种不同的方法。在方法 1 中我得到错误,而在其他 2 个选项中我没有得到错误,文件实际上没有被复制过来。

有人可以帮忙吗?

另外两种方法都标有#,我已经分别尝试过。

s3_client.upload_file 不起作用,因为它需要源文件的路径,当我提供“gs:///30327570.pdf”时,它会说

'不存在这样的文件或目录'

gustil 命令正确执行,没有错误,但在 s3 存储桶中没有创建新文件。

import os
from google.cloud import storage
import boto3
import subprocess

s3_client=boto3.client('s3',aws_access_key_id='XYZ',aws_secret_access_key='ABC')
client = storage.Client()
def hello_gcs(data, context):
    bucket = client.get_bucket(data['bucket'])
    blob = bucket.blob(data['name'])
   #subprocess.call(['gsutil -m rsync -r gs://<google_bucket_name>/30327570.pdf s3://<aws_bucket_name>'], shell=True)
    subprocess.call(['gsutil cp gs://<google_bucket_name>/30327570.pdf s3://<aws_bucket_name>'], shell=True)
   #s3_client.upload_file('gs://<google_bucket_name>/30327570.pdf','<aws_bucket_name>','30327570.pdf')

【问题讨论】:

    标签: python amazon-s3 google-cloud-platform google-cloud-storage google-cloud-functions


    【解决方案1】:

    如果gsutil rsync 不起作用,您可以尝试使用rclone,或将过程反转为migrate data from S3 to GCS

    【讨论】:

      【解决方案2】:

      虽然这是用 JavaScript 编写的,但这里有一个 Google Cloud Function,用于将文件从 GCS 存储桶同步到 S3 存储桶:

      https://github.com/pendo-io/gcs-s3-sync

      【讨论】:

        【解决方案3】:

        我尝试以同样的方式将文件从 GCS 复制到 S3,但它还不能正常工作。 @Karan:我读到您找到了使用云功能将文件从 GCS 复制到 S3 的解决方案。可以在这里发布您的最终脚本吗?

        错误消息是“找不到文件”,我尝试设置文件的完整路径,但它也不起作用。

        def hello_gcs(event, context):
        s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
        
        file = event
        
        try:
            print(f"Processing file: {file['name']}.")
            s3.upload_file(f"gs://<GCS_BUCKET_NAME>/{file['name']}", "<S3_BUCKET_NAME", "textfile.txt")
            print(f"Processed file: {file['name']} successful.")
            return True
        except FileNotFoundError:
            print(f"The file was not found: {file['name']}")
            return False
        except NoCredentialsError:
            print(f"Credentials not available {file['name']}")
            return False
        

        【讨论】:

          猜你喜欢
          • 2021-01-17
          • 2022-11-24
          • 2022-07-06
          • 2020-09-17
          • 2020-06-16
          • 1970-01-01
          • 2017-11-06
          • 2015-06-04
          • 1970-01-01
          相关资源
          最近更新 更多