Python Boto3 AWS分段上传语法

时间:2015-06-19 15:39:09

标签: python amazon-web-services amazon-s3 boto boto3

我使用AWS成功进行身份验证,并使用Bucket对象上的'put_object'方法上传文件。现在我想使用multipart API来完成大文件。我在这个问题中找到了接受的答案: How to save S3 object to a file using boto3

但是在尝试实现时,我遇到了“未知方法”错误。我究竟做错了什么?我的代码如下。谢谢!

## Get an AWS Session
self.awsSession = Session(aws_access_key_id=accessKey,
aws_secret_access_key=secretKey,
aws_session_token=session_token,
region_name=region_type)  

 ...          

# Upload the file to S3
s3 = self.awsSession.resource('s3')
s3.Bucket('prodbucket').put_object(Key=fileToUpload, Body=data) # WORKS
#s3.Bucket('prodbucket').upload_file(dataFileName, 'prodbucket', fileToUpload) # DOESNT WORK
#s3.upload_file(dataFileName, 'prodbucket', fileToUpload) # DOESNT WORK

2 个答案:

答案 0 :(得分:2)

尚未将upload_file方法移植到存储区资源。现在,您需要直接使用客户端对象来执行此操作:

client = self.awsSession.client('s3')
client.upload_file(...)

答案 1 :(得分:0)

Libcloud S3 wrapper透明地为您处理所有部分的拆分和上传。

使用upload_object_via_stream方法:

from libcloud.storage.types import Provider
from libcloud.storage.providers import get_driver

# Path to a very large file you want to upload
FILE_PATH = '/home/user/myfile.tar.gz'

cls = get_driver(Provider.S3)
driver = cls('api key', 'api secret key')

container = driver.get_container(container_name='my-backups-12345')

# This method blocks until all the parts have been uploaded.
extra = {'content_type': 'application/octet-stream'}

with open(FILE_PATH, 'rb') as iterator:
    obj = driver.upload_object_via_stream(iterator=iterator,
                                          container=container,
                                          object_name='backup.tar.gz',
                                          extra=extra)

有关S3 Multipart功能的官方文档,请参阅AWS Official Blog