无法在boto3中创建s3资源/客户端

时间:2017-09-13 22:22:47

标签: python-3.x amazon-web-services aws-sdk boto3

编辑:我认为这种追溯源于依赖关系的某种问题。使用pip升级软件包没有用,但是我创建了一个新的文件夹并从头开始安装并且已经解决了

我是一名Python新手,所以我正在努力调试我写作的AWS Lambda。

我已将其缩小到这行代码s3_client = botoSession.resource('s3'),这是一个带有语法错误的长回溯:语法无效。 botoSession变量仅用于凭证 - botoSession = boto3.session.Session(aws_access_token, aws_secret_access_token)

我还尝试过s3_client = boto3.client('s3')s3_client = boto3.resource('s3')s3_client = botoSession.resource('s3')

当我使用botoSession.client('ses', region)时,发送电子邮件时没有问题。

我发现Error: client = boto3.client('s3') | AWS Elastic Beanstalk Worker Environment似乎是一个类似的问题,但它似乎相当陈旧,我无法弄清楚解决方案是什么。我尝试添加 import sys sys.path = [p for p in sys.path if not p.endswith('futures-3.0.3-py3.4.egg')] 在我的档案顶部似乎没有用。

整个追溯如下:

Traceback (most recent call last):
  File "smartsheetExporter.py", line 45, in <module>
    s3_client = botoSession.resource('s3')
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/boto3/session.py", line 389, in resource
    aws_session_token=aws_session_token, config=config)
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/boto3/session.py", line 263, in client
    aws_session_token=aws_session_token, config=config)
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/botocore/session.py", line 836, in create_client
    client_config=config, api_version=api_version)
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/botocore/client.py", line 65, in create_client
    cls = self._create_client_class(service_name, service_model)
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/botocore/client.py", line 90, in _create_client_class
    base_classes=bases)
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/botocore/hooks.py", line 227, in emit
    return self._emit(event_name, kwargs)
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/botocore/hooks.py", line 210, in _emit
    response = handler(**kwargs)
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/boto3/utils.py", line 61, in _handler
    module = import_module(module)
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/boto3/utils.py", line 52, in import_module
    __import__(name)
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/boto3/s3/inject.py", line 15, in <module>
    from boto3.s3.transfer import create_transfer_manager
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/boto3/s3/transfer.py", line 127, in <module>
    from s3transfer.exceptions import RetriesExceededError as \
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/s3transfer/__init__.py", line 134, in <module>
    import concurrent.futures
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/concurrent/futures/__init__.py", line 8, in <module>
    from concurrent.futures._base import (FIRST_COMPLETED,
  File "/Users/nihar/LocalDocs/PythonPractice/Smartsheet-Emailer-Lambda/concurrent/futures/_base.py", line 381
    raise exception_type, self._exception, self._traceback
                        ^
SyntaxError: invalid syntax

4 个答案:

答案 0 :(得分:1)

每当发生奇怪的事情时,更新事情总是一个好主意:

sudo pip install pip --upgrade
sudo pip install boto --upgrade
sudo pip install boto3 --upgrade
sudo pip install awscli --upgrade

如果您使用的是Python 3,请尝试使用pip3代替pip

答案 1 :(得分:0)

我在boto3上遇到了同样的问题,最终不得不将我的lambda运行的Python版本从Python 3.6降级到Python 2.7。如果您为此使用Serverless Framework,则您的serverless.yml文件如下所示。

provider:
  name: aws
  runtime: python3.6
  memorySize: 3008

cool_function:
    name: cool-function
    description: This lambda goes and performs magic.
    handler: cool_function.lambda_handler
    runtime: python2.7
      - schedule:
          rate: rate(4 hours)
    timeout: 180

答案 2 :(得分:0)

我在 python 3.6 AWS Lambda 上遇到了相同的问题。

我找到了另一个帮助我here的答案。

您应该使用futures==2.2.0

答案 3 :(得分:-1)

如果您在实例上将角色分配的Amazon EC2实例上运行代码,那么您只需要:

import boto3
s3_client = boto3.client('s3')
s3_resource = boto3.resource('s3') # Pick whichever is wish to use

如果您在Amazon EC2实例上,则可以:

import boto3
session = boto3.Session(aws_access_key_id='AKIAxxx',aws_secret_access_key='yyy')
s3_client = session.client('s3')
s3_resource = session.resource('s3')

当然,您应该永远不会将您的凭据放在代码文件中。相反,将它们放在credentials file(最简单的是通过aws configure)或环境变量中。这样,它们就不会被复制到任何代码库中。

请参阅:Boto3 credentials