首页 > 解决方案 > import-im6.q16: 未授权“boto3”@error/constitute.c/WriteImage/1037

问题描述

我使用 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')'

标签: pythonamazon-web-servicesboto3aws-cli

解决方案


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

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


推荐阅读