'List objects in an AWS S3 bucket (deployed using Serverless)
I seem to be getting permission issues and can't find any SO answer to resolve this.
My Lambda function is simple (note -- I am using Babel to transpile):
import AWS from "aws-sdk";
import { success, failure} from "./libs/response-lib";
export async function main(event, context, callback) {
const s3 = new AWS.S3();
const params = {
Bucket: "mybucket",
};
try {
const result = await s3.listObjectsV2(params);
return success(result);
}
catch(e) {
return failure({ status: false, message: e })
}
}
When I deploy using Serverless and hit the endpoint it gives me, I get back a 500 error.
I have this included in my serverless.yml
file and have turned off "block all public access" for the S3 bucket itself but it seems like I am missing more?
iamRoleStatements:
- Effect: Allow
Action:
- s3:ListBucket
Resource: "arn:aws:s3:::mybucket"
Solution 1:[1]
Okay, so AWS' S3 methods are asynchronous, so I needed to have it return a promise:
.
.
.
const result = await s3.listObjectsV2(params).promise();
.
.
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |