【问题标题】:AWS S3 Bucket Upload/Transfer with boto3使用 boto3 上传/传输 AWS S3 存储桶
【发布时间】:2020-10-03 00:41:18
【问题描述】:

我需要将文件上传到 S3,我想知道应该使用哪个 boto3 api 调用?

我在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()))

【问题讨论】:

标签: amazon-web-services amazon-s3 boto3 s3-bucket


【解决方案1】:

最好在客户端使用该方法。它们是相同的,但使用客户端方法意味着您不必自己进行设置。

【讨论】:

    【解决方案2】:

    你可以使用客户端:低级服务访问:我在https://www.techblog1.com/2020/10/python-3-how-to-communication-with-aws.html看到了一个示例代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-02
      • 2015-12-07
      • 2018-10-25
      • 1970-01-01
      • 1970-01-01
      • 2018-04-25
      • 2017-03-13
      相关资源
      最近更新 更多