Search code examples
pythonamazon-web-servicesaws-lambdaboto3

Boto3 exception NoCredentialsError


I am trying to test sample AWS lamda code through this tutorial https://docs.aws.amazon.com/AmazonS3/latest/userguide/tutorial-s3-object-lambda-uppercase.html#ol-upper-step6

Everything works except the code in Step 7, that is:

 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")

I have correctly copied ARN and bucket name, but still I get exception:

        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

What am I missing?


Solution

  • You need to establish a session with your AWS account.

    Establish a session in Python:

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

    Or, configure your CLI:

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