boto3 S3Transfer throws error AttributeError: 'ResourceMeta' object has no attribute 'events'

boto3 S3Transfer throws error AttributeError: 'ResourceMeta' object has no attribute 'events'

我是 AWS 和 Boto3 的新手,我正在尝试将我的 json 文件上传到 s3。我已将 s3FullAccess IAM 角色分配给我的 ec2。我安装了 python3.5 和 boto3。我正在尝试下面的代码。

from boto3.s3.transfer import S3Transfer
import boto3
s3_client = boto3.resource('s3')
transfer = S3Transfer(s3_client)
bucket_name='test-bucket-oracle'
prefix='configurations'
transfer.upload_file('/home/ec2-user/temp/config.json', bucket_name, prefix+'configs3.json', ExtraArgs={'ServerSideEncryption': "AES256"})

并低于错误。非常感谢任何帮助。

Traceback (most recent call last):
  File "transfers3.py", line 4, in <module>
    transfer = S3Transfer(s3_client)
  File "/home/ec2-user/.local/lib/python3.5/site-packages/boto3/s3/transfer.py", line 259, in __init__
    self._manager = create_transfer_manager(client, config, osutil)
  File "/home/ec2-user/.local/lib/python3.5/site-packages/boto3/s3/transfer.py", line 160, in create_transfer_manager
    return TransferManager(client, config, osutil, executor_cls)
  File "/home/ec2-user/.local/lib/python3.5/site-packages/s3transfer/manager.py", line 264, in __init__
    self._register_handlers()
  File "/home/ec2-user/.local/lib/python3.5/site-packages/s3transfer/manager.py", line 514, in _register_handlers
    self._client.meta.events.register_first(
AttributeError: 'ResourceMeta' object has no attribute 'events'

因为还没有人回答。原来我需要使用

boto3.client('s3')

我尝试了 boto3.resource('s3'),因为 github post 推荐这样做。欢迎任何可以解释差异的专家。

您需要使用 client,因为这是 S3Transfer 所期望的。您可以在这里查看:boto3.s3.transfer.S3Transfer

如果您勾选 boto3 docs,则 resource (ServiceResource) 描述为:

A resource representing Amazon Simple Storage Service (S3)

client 为:

A low-level client representing Amazon Simple Storage Service (S3):

正如您在文档中看到的那样,它们不是一回事,尽管您可以使用两者完成类似的任务。