boto3 S3Transfer引发错误AttributeError:'ResourceMeta'对象没有属性'events'

问题描述 投票:0回答:2

我是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'
amazon-s3 boto3 python-3.5
2个回答
0
投票

您需要使用client,因为它是S3Transfer所期望的。您可以在此处检查它:boto3.s3.transfer.S3Transfer

如果选中boto3 docs,则将resourceServiceResource)描述为:

A resource representing Amazon Simple Storage Service (S3)

[client as:

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

而且正如您在文档中所看到的,尽管您可以用两者完成相似的任务,但它们不是同一件事。


0
投票

由于尚未有人回答。原来我需要使用

boto3.client('s3')

我尝试过boto3.resource('s3'),因为github post建议这么做。欢迎任何可以解释差异的专家。

© www.soinside.com 2019 - 2024. All rights reserved.