【问题标题】:Can't create s3 resource/client in boto3无法在 boto3 中创建 s3 资源/客户端
【发布时间】:2017-09-13 22:22:47
【问题描述】:

编辑:我相信这个回溯源于依赖关系的某种问题。使用 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

【问题讨论】:

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


    【解决方案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

    【讨论】:

      【解决方案2】:

      我刚刚在 boto3 上遇到了同样的问题,最终不得不将我的 lambda 运行的 Python 版本从 Python 3.6 降级到 Python 2.7。如果您为此使用无服务器框架,您的 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
      

      【讨论】:

        【解决方案3】:

        python 3.6AWS Lambda 也有同样的问题。

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

        你应该使用futures==2.2.0

        【讨论】:

          【解决方案4】:

          如果您在 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

          【讨论】:

          • 凭据是否导致错误/异常?我知道我不应该将凭据硬编码,但我只是想把一个工作样本放在一起。我尝试了您建议的代码,但我仍然有相同/相似的回溯
          • 当您不在 EC2 上运行时,您可以将这些凭证设置为环境变量。这样,相同的代码可以在任何环境中工作。
          猜你喜欢
          • 2022-11-05
          • 1970-01-01
          • 1970-01-01
          • 2020-02-06
          • 2016-09-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-09
          相关资源
          最近更新 更多