An error occurred (accessdenied) when calling the getobject operation: access denied lambda

Solve - S3 Access Denied when calling ListObjectsV2 #

The S3 error "(AccessDenied) when calling the ListObjectsV2 operation" occurs when we try to list the objects in an S3 bucket without having the necessary permissions.

An error occurred (accessdenied) when calling the getobject operation: access denied lambda

To solve the "(AccessDenied) when calling the ListObjectsV2 operation" error attach a policy that allows the ListBucket action on the bucket itself and the GetObject action on all of the bucket's objects to the IAM entity (user or role) that is trying to access the S3 bucket.

Edit the IAM entity (user or role) that grants permissions to the bucket and add the following policy.

Make sure to replace the YOUR_BUCKET placeholder with the name of your S3 bucket.

Copied!

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject" ], "Resource": [ "arn:aws:s3:::YOUR_BUCKET/*" ] }, { "Effect": "Allow", "Action": [ "s3:ListBucket" ], "Resource": [ "arn:aws:s3:::YOUR_BUCKET" ] } ] }

The first statement in the JSON policy allows the GetObject action on individual objects in sub-directories of the bucket.

The second statement in the policy allows the ListBucket action. It allows the IAM entity to list all of the bucket's objects.

After attaching the IAM policy to the IAM user or role that tries to list the bucket's objects, the error should be resolved.

Note that S3 is a globally distributed service and it might take a minute or two for the policy to take effect.

If the error is not resolved, you have to verify that the bucket policy does not deny access to the ListBucket or GetObject actions and that it does not have a condition that only allows a specific IP range to access the bucket's objects:

  1. Open your AWS S3 console and click on your bucket's name

  2. Click on the Permissions tab and scroll down to the Bucket Policy section

  3. Verify that your bucket policy does not deny the ListBucket or GetObject actions. An explicit Deny statement always overrides Allow statements.

If the bucket policy does not Deny the ListBucket or GetObject actions, but you still are unable to list your bucket's objects, add the following Bucket policy in the editor.

Make sure to replace the YOUR_BUCKET, YOUR_ACCOUNT_NUMBER, YOUR_USERNAME placeholders with real values.

Copied!

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::YOUR_ACCOUNT_NUMBER:user/YOUR_USERNAME" }, "Action": "s3:GetObject", "Resource": "arn:aws:s3:::YOUR_BUCKET/*" }, { "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::YOUR_ACCOUNT_NUMBER:user/YOUR_USERNAME" }, "Action": "s3:ListBucket", "Resource": "arn:aws:s3:::YOUR_BUCKET" } ] }

We allowed the GetObject and ListObject actions to a specific user in the account (the Principal field).

This policy allows an IAM user to invoke the GetObject and ListObject actions on the bucket, even if they don't have a policy which permits them to do that.

Further Reading #

  • Get the Size of a Folder in AWS S3 Bucket
  • How to Get the Size of an AWS S3 Bucket
  • Add a Bucket Policy to an AWS S3 Bucket
  • Configure CORS for an AWS S3 Bucket
  • Allow Public Read access to an AWS S3 Bucket
  • Copy a Local Folder to an S3 Bucket
  • Download a Folder from AWS S3
  • How to Rename a Folder in AWS S3
  • Copy Files and Folders between S3 Buckets
  • How to Delete a Folder from an S3 Bucket
  • Count Number of Objects in S3 Bucket
  • Download an Entire S3 Bucket - Complete Guide
  • AWS CDK Tutorial for Beginners - Step-by-Step Guide
  • How to use Parameters in AWS CDK

Why am I getting an access denied error when I use lambda function to upload files to an Amazon S3 bucket?

If the permissions between a Lambda function and an Amazon S3 bucket are incomplete or incorrect, then Lambda returns an Access Denied error.

What is GetObjectAcl?

PDF. Returns the access control list (ACL) of an object. To use this operation, you must have s3:GetObjectAcl permissions or READ_ACP access to the object. For more information, see Mapping of ACL permissions and access policy permissions in the Amazon S3 User Guide.

What is AmazonS3FullAccess policy?

AWS managed policy: AmazonS3FullAccess This policy grants permissions that allow full access to Amazon S3. To view the permissions for this policy, see AmazonS3FullAccess in the AWS Management Console.