中看到了示例代码
我需要将文件上传到S3,我想知道我应该使用哪个boto3 api调用?
我在boto3文档中找到了两个方法:
- http://boto3.readthedocs.io/en/latest/reference/services/s3.html S3.Client.upload_file
- http://boto3.readthedocs.io/en/latest/reference/customizations/s3.html
是否使用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()))
最好在客户机上使用该方法。它们是相同的,但是使用客户机方法意味着您不必自己设置东西。
您可以使用Client:低级服务访问:我在https://www.techblog1.com/2020/10/python-3-how-to-communication-with-aws.html