Boto3 异常 NoCredentialsError

Boto3 exception NoCredentialsError

我正在尝试通过本教程测试示例 AWS lamda 代码https://docs.aws.amazon.com/AmazonS3/latest/userguide/tutorial-s3-object-lambda-uppercase.html#ol-upper-step6

除第 7 步中的代码外,一切正常,即:

 import boto3

 s3 = boto3.client('s3')
 print("printing boto version: " + boto3.__version__)


def getObject(bucket, key):
   objectBody = s3.get_object(Bucket = bucket, Key = key)
   print(objectBody["Body"].read().decode("utf-8"))
   print("\n")

 print('Original object from the S3 bucket:')
 
# Replace the two input parameters of getObject() below with
# the S3 bucket name that you created in Step 1 and
# the name of the file that you uploaded to the S3 bucket in Step 2
 getObject(<<My Bucket name>>,
      "tutorial.txt")

 print('Object transformed by S3 Object Lambda:')
# Replace the two input parameters of getObject() below with
# the ARN of your S3 Object Lambda access point that you saved earlier and
# the name of the file with the transformed data (which in this case is
# the same as the name of the file that you uploaded to the S3 bucket
# in Step 2)
 getObject(<<MY_ARN>>,
      "tutorial.txt")

我已经正确复制了 ARN 和存储桶名称,但仍然出现异常:

        File "/Users/deepaksharma/object-lamba/env/lib/python3.8/site-packages/botocore/hooks.py", line 212, in _emit
        response = handler(**kwargs)
       File "/Users/deepaksharma/object-lamba/env/lib/python3.8/site-packages/botocore/signers.py", line 95, in handler
       return self.sign(operation_name, request)
       File "/Users/deepaksharma/object-lamba/env/lib/python3.8/site-packages/botocore/signers.py", line 167, in sign
         auth.add_auth(request)
     File "/Users/deepaksharma/object-lamba/env/lib/python3.8/site-packages/botocore/auth.py", line 401, in add_auth
      raise NoCredentialsError()
     botocore.exceptions.NoCredentialsError: Unable to locate credentials

我错过了什么?

您需要与您的 AWS 账户建立会话。

在Python中建立会话:

import boto3

session = boto3.Session( 
         aws_access_key_id='<your_access_key_id>', 
         aws_secret_access_key='<your_secret_access_key>')

s3 = boto3.client('s3')

或者,配置您的 CLI:

aws configure
<enter key>
<enter secret>
<enter region>
<enter output format>