【发布时间】:2020-10-03 00:41:18
【问题描述】:
我需要将文件上传到 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()))
【问题讨论】:
-
如果合适,您可以使用AWS Command-Line Interface (CLI) 复制文件。使用
aws s3 cp或aws s3 sync命令,它会为您完成所有艰苦的工作。 -
您也可以使用 Minio 客户端的
mc mirror或mc cp获取相同的 https://docs.minio.io/docs/minio-client-complete-guide#mirror,希望对您有所帮助。
标签: amazon-web-services amazon-s3 boto3 s3-bucket