I need to upload files to S3 and I was wondering which boto3 api call I should use?
我需要将文件上传到S3,我想知道我应该使用哪个boto3 api调用?
I have found two methods in the boto3 documentation:
我在boto3文档中找到了两个方法:
- http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.upload_file
- http://boto3.readthedocs.io/en/latest/reference/services/s3.html#S3.Client.upload_file
- http://boto3.readthedocs.io/en/latest/reference/customizations/s3.html
- http://boto3.readthedocs.io/en/latest/reference/customizations/s3.html
Do I use the client.upload_file() ...
我是否使用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')
or do I use S3Transfer.upload_file() ...
或者我使用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')
Any suggestions would be appreciated. Thanks in advance.
任何建议,将不胜感激。提前致谢。
. . .
。 。 。
possible solution...
可能解决方案
# 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()))
1 个解决方案
#1
1
It's better to use the method on the client. They're the same, but using the client method means you don't have to setup things yourself.
最好在客户端上使用该方法。它们是相同的,但使用客户端方法意味着您不必自己设置。
#1
1
It's better to use the method on the client. They're the same, but using the client method means you don't have to setup things yourself.
最好在客户端上使用该方法。它们是相同的,但使用客户端方法意味着您不必自己设置。