【发布时间】:2020-11-11 10:03:38
【问题描述】:
我正在寻找一种将文件从 AWS S3 复制到 GCS 的 Python 方法。
我不想打开/读取文件然后使用 blob.upload_from_string() 方法。我想“按原样”转移它。
我不能使用“gsutils”。我使用的库的范围是 gcloud、boto3(也尝试了 s3fs)。
这是一个使用 blob.upload_from_string() 方法的简单示例(似乎有效),我试图避免该方法,因为我不想打开/读取文件。我无法使用 blob.upload_from_file() 方法使其工作,因为 GCS api 需要一个我无法正确提供的可访问、可读、类似文件的对象。
我错过了什么?有什么建议吗?
import boto3
from gcloud import storage
from oauth2client.service_account import ServiceAccountCredentials
GSC_Token_File = 'path/to/GSC_token'
s3 = boto3.client('s3', region_name='MyRegion') # im running from AWS Lambda, no authentication required
gcs_credentials = ServiceAccountCredentials.from_json_keyfile_dict(GSC_Token_File)
gcs_storage_client = storage.Client(credentials=gcs_credentials, project='MyGCP_project')
gcs_bucket = gcs_storage_client.get_bucket('MyGCS_bucket')
s3_file_to_load = str(s3.get_object(Bucket='MyS3_bucket', Key='path/to/file_to_copy.txt')['Body'].read().decode('utf-8'))
blob = gcs_bucket.blob('file_to_copy.txt')
blob.upload_from_string(s3_file_to_load)
【问题讨论】:
标签: python amazon-s3 google-cloud-storage boto3 gcloud