首页 > 解决方案 > 无法列出 s3 存储桶,出现类似“AttributeError: module 'boto3' has no attribute 'client'”的错误

问题描述

我收到如下错误:

AttributeError: module 'boto3' has no attribute 'client'

我的python片段如下所示

[ansible@ip-172-31-40-59 python]$ cat lists3buckets.py
#import boto3
#s3 = boto3.resource('s3')
#obj = s3.Object(bucketname, itemname)
#body = obj.get()['Body'].read()

#import boto3
#from botocore import ClientError
#s3 = boto3.client('s3')
#for bucket in s3.list_buckets():
#    print(bucket)

import boto3
from botocore.exceptions import ClientError
s3=boto3.client('s3')
for i in s3.list_buckets():
    print(i)

注意:我也尝试了注释代码,但没有运气。

我的环境是 Amazon Linux 2 机器,并且已经安装了 boto3 和 botocore。

[ansible@ip-172-31-40-59 python]$ pip3 install boto3 --upgrade --user
Collecting boto3
  Using cached https://files.pythonhosted.org/packages/85/c6/00e25614edf5ac025684bcc7af4c750187249ea7abbfc19dac05b95e83e8/boto3-1.14.22.tar.gz
Requirement already up-to-date: botocore<1.18.0,>=1.17.22 in /usr/local/lib/python3.7/site-packages (from boto3)
Requirement already up-to-date: jmespath<1.0.0,>=0.7.1 in /home/ansible/.local/lib/python3.7/site-packages (from boto3)
Requirement already up-to-date: s3transfer<0.4.0,>=0.3.0 in /home/ansible/.local/lib/python3.7/site-packages (from boto3)
Requirement already up-to-date: urllib3<1.26,>=1.20; python_version != "3.4" in /home/ansible/.local/lib/python3.7/site-packages (from botocore<1.18.0,>=1.17.22->boto3)
Requirement already up-to-date: python-dateutil<3.0.0,>=2.1 in /home/ansible/.local/lib/python3.7/site-packages (from botocore<1.18.0,>=1.17.22->boto3)
Requirement already up-to-date: docutils<0.16,>=0.10 in /home/ansible/.local/lib/python3.7/site-packages (from botocore<1.18.0,>=1.17.22->boto3)
Requirement already up-to-date: six>=1.5 in /home/ansible/.local/lib/python3.7/site-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.18.0,>=1.17.22->boto3)
Installing collected packages: boto3
  Found existing installation: boto3 1.14.17
    Uninstalling boto3-1.14.17:
      Successfully uninstalled boto3-1.14.17
  Running setup.py install for boto3 ... done
Successfully installed boto3-1.14.22
[ansible@ip-172-31-40-59 python]$ pip3 install botocore --upgrade --user
Requirement already up-to-date: botocore in /usr/local/lib/python3.7/site-packages
Requirement already up-to-date: jmespath<1.0.0,>=0.7.1 in /home/ansible/.local/lib/python3.7/site-packages (from botocore)
Requirement already up-to-date: urllib3<1.26,>=1.20; python_version != "3.4" in /home/ansible/.local/lib/python3.7/site-packages (from botocore)
Requirement already up-to-date: python-dateutil<3.0.0,>=2.1 in /home/ansible/.local/lib/python3.7/site-packages (from botocore)
Requirement already up-to-date: docutils<0.16,>=0.10 in /home/ansible/.local/lib/python3.7/site-packages (from botocore)
Requirement already up-to-date: six>=1.5 in /home/ansible/.local/lib/python3.7/site-packages (from python-dateutil<3.0.0,>=2.1->botocore)

我正在执行如下脚本:

[ansible@ip-172-31-40-59 python]$ !p
python3 lists3buckets.py
Traceback (most recent call last):
  File "lists3buckets.py", line 14, in <module>
    s3=boto3.client('s3')
AttributeError: module 'boto3' has no attribute 'client'

我不明白为什么一个简单的片段不应该工作?

标签: pythonamazon-web-servicesamazon-s3

解决方案


推荐阅读