2016-09-17 160 views
0

我需要上传文件到S3,我想知道哪个boto3 API调用我应该使用?AWS S3桶上传/传输与boto3

我已经找到了boto3文档中的两种方法:

难道我用的是client.upload_file()...

#!/usr/bin/python 
import boto3 
session = Session(aws_access_key_id, aws_secret_access_key, region) 
s3 = session.resource('s3') 
s3.Bucket('my_bucket').upload_file('/tmp/hello.txt', 'hello.txt') 

或者我使用S3Transfer.upload_file()...

#!/usr/bin/python 
import boto3 
session = Session(aws_access_key_id, aws_secret_access_key, region) 
S3Transfer(session).upload_file('/tmp/hello.txt', 'my_bucket', 'hello.txt') 

任何意见,将不胜感激。提前致谢。

。 。 。

可能的解决方案......

# http://boto3.readthedocs.io/en/latest/reference/services/s3.html#examples 
# http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.put_object 
# http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.get_object 

client = boto3.client("s3", "us-west-1", aws_access_key_id = "xxxxxxxx", aws_secret_access_key = "xxxxxxxxxx") 


with open('drop_spot/my_file.txt') as file: 
    client.put_object(Bucket='s3uploadertestdeleteme', Key='my_file.txt', Body=file) 


response = client.get_object(Bucket='s3uploadertestdeleteme', Key='my_file.txt') 

print("Done, response body: {}".format(response['Body'].read())) 
+0

如果合适,您可以使用[AWS Command-Line Interface(CLI)](http://aws.amazon.com/cli/)复制文件。使用'aws s3 cp'或'aws s3 sync'命令,它会为你做所有的辛苦工作。 –

+0

对于相同的[https://docs.minio.io/docs/minio-client-complete-guide#mirror](https://docs.minio),您也可以使用Minio Client的'mc mirror'或'mc cp' .io/docs/minio-client-complete-guide#mirror)希望它有帮助。 – koolhead17

回答

1

最好是使用方法在客户端上。他们是一样的,但使用客户端方法意味着你不必自己设置一些东西。