:wave: I want to upload image to my s3 bucket whic...
# help
k
👋 I want to upload image to my s3 bucket which should be publicly accessible. Currently when I stick image url in the browser i get
AccessDenied
My sst bucket:
Copy code
const bucket = new Bucket(stack, 'uploads', {
		cors: [
			{
				maxAge: '1 day',
				allowedOrigins: ['*'],
				allowedHeaders: ['*'],
				allowedMethods: ['GET', 'PUT', 'POST', 'DELETE', 'HEAD'],
			},
		],
	});
My lambda:
Copy code
const s3 = new AWS.S3({
	accessKeyId: process.env.AWS_S3_ACCESS_KEY_ID,
	secretAccessKey: process.env.AWS_S3_SECRET_ACCESS_KEY
});

async function uploadImage(blob) {
	const uploadedImage = await s3
		.upload({
			Bucket: process.env.AWS_S3_BUCKET_NAME,
			Key: `${new Date().getTime()}.jpeg`,
			Body: blob,
		})
		.promise();

	return uploadedImage.Location;
}
r
To make it publicly accessible I think you need to add a bucket policy
k
@Ross Coundon can I do that in Bucket construct?
r
I don’t believe so, here’s an example I found. I think you need this https://github.com/aws/aws-cdk/tree/main/packages/@aws-cdk/aws-s3#permissions
k
Added policy in aws console/s3. All good now