【问题标题】:import-im6.q16: not authorized `boto3' @ error/constitute.c/WriteImage/1037import-im6.q16: 未授权“boto3”@error/constitute.c/WriteImage/1037
【发布时间】:2020-01-10 12:37:02
【问题描述】:

我使用 AWS CLI 。我安装了 boto3 并尝试运行该代码。我有三个错误

import boto3
s3 = boto3.resource('s3')
# Get list of objects for indexing
images=[('afridi.jpg','Shahid Afridi'),
   ('sakib.jpg','Sakib Al Hasan'),
   ('kohli.jpg','Birat Kohli'),
   ('masrafi.jpg','Mashrafe Bin Mortaza'),
   ('ganguli.jpg','Sourav Ganguly')
  ]

遍历列表以将对象上传到 S3

for image in images:
    file = open(image[0],'rb')
    object = s3.Object('taifur12345bucket',image[0])
    ret = object.put(Body=file,
               Metadata={'FullName':image[1]}
               )

错误

import-im6.q16: not authorized `boto3' @ error/constitute.c/WriteImage/1037.
./code1.py: line 2: syntax error near unexpected token `('
./code1.py: line 2: `s3 = boto3.resource('s3')'

【问题讨论】:

  • 您是否在~/.aws/credentials 或您的代码中设置了您的访问密钥和秘密密钥?

标签: python amazon-web-services boto3 aws-cli


【解决方案1】:

您的脚本缺少顶部的 shebang 行 #!/usr/bin/env python3。因此,您的脚本由bash shell 运行,而不是由 Python 解释器运行。 bash 不理解 Python 代码,因此会出现错误。

将 shebang 行添加到脚本顶部,然后重试。

【讨论】:

    【解决方案2】:

    如果您不是从获得许可的 ec2 实例运行此代码,则需要适当地配置您的凭据。

    see configuration section here

    【讨论】:

    • 我做了所有这些配置和同样的错误
    猜你喜欢
    • 2019-08-28
    • 2020-07-25
    • 2019-04-25
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    相关资源
    最近更新 更多