aws / aws-sdk-js

AWS SDK for JavaScript in the browser and Node.js (In Maintenance Mode, End-of-Life on 09/08/2025). The AWS SDK for JavaScript v3 in the browser and Node.js is available here: https://github.com/aws/aws-sdk-js-v3
https://aws.amazon.com/developer/language/javascript/
Apache License 2.0
7.6k stars 1.55k forks source link

S3 SDK... -_- #4660

Closed xanderbilla closed 4 months ago

xanderbilla commented 4 months ago

Describe the bug

Hello, greeting... I don't know if this is bug or what but I'm trying to fix this from 8 hours and I'm stuck. First it was saying this mosule not found that module not found....fixed anyway...now it's saying Access denied...even the S3 permission is attached allowing all the actions to the lambda function.

Expected Behavior

It should work

Current Behavior

Here is the code to List all the objects (just in case)

import { S3Client, ListObjectsCommand } from "@aws-sdk/client-s3";

export const handler = async (event) => {
    const method = event.httpMethod;

    if (method === 'GET') {
        return handleGetRequest();
    }

    return handleInvalidMethod();
};

function handleInvalidMethod() {
    return {
        statusCode: 405,
        body: JSON.stringify('Method Not Allowed')
    };
}

async function handleGetRequest() {
    try {
        const s3Client = new S3Client();
        const params = {
            Bucket: 'bkt-summer-training-s3' // Replace with your S3 bucket name
        };

        const data = await s3Client.send(new ListObjectsCommand(params)); // Add this line

        if (data.Contents.length === 0) {
            return {
                statusCode: 200,
                body: JSON.stringify('Bucket is empty')
            };
        }
        return data.Contents;
    } catch (error) {
        console.error('Error retrieving list of objects:', error);
    }
}

Here is the log

2024-07-13T18:18:53.208Z    3bafc967-dc8d-45d3-b166-a10685a791d8    ERROR   Error retrieving list of objects: AccessDenied: Access Denied
    at throwDefaultError (/var/runtime/node_modules/@aws-sdk/node_modules/@smithy/smithy-client/dist-cjs/index.js:838:20)
    at /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/smithy-client/dist-cjs/index.js:847:5
    at de_CommandError (/var/runtime/node_modules/@aws-sdk/client-s3/dist-cjs/index.js:4756:14)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/middleware-serde/dist-cjs/index.js:35:20
    at async /var/runtime/node_modules/@aws-sdk/middleware-signing/dist-cjs/index.js:225:18
    at async /var/runtime/node_modules/@aws-sdk/node_modules/@smithy/middleware-retry/dist-cjs/index.js:320:38
    at async /var/runtime/node_modules/@aws-sdk/middleware-sdk-s3/dist-cjs/index.js:97:20
    at async /var/runtime/node_modules/@aws-sdk/middleware-sdk-s3/dist-cjs/index.js:120:14
    at async /var/runtime/node_modules/@aws-sdk/middleware-logger/dist-cjs/index.js:33:22 {

Reproduction Steps

Created Lambda function with default role. -> Created API gateway -> created resources (CORS enabled) -> Cretaed method in that resource (GET method only) with the function I created and enabled Proxy integration.

After writing the code in Lambda function -> Attached a new policy in the current role which is -

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::arn:aws:s3:::bkt-summer-training-s3/*"
        }
    ]
}

Possible Solution

No response

Additional Information/Context

No response

SDK version used

v3

Environment details (OS name and version, etc.)

I'm using AWS Lamda Code Editor with latest build of nodejs