Whenever I try to delete an object with the Javascript SDK, I get an error saying access denied. However, I can upload files correctly. Can someone help me with this?
My policies for the bucket:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObject"
],
"Resource": "arn:aws:s3:::twitter-clone-image-storage-zrs/*"
}
]
}
My function for deleting files:
const s3 = new S3({
region,
accessKeyId,
secretAccessKey,
});
export const deleteFile = (picture_key) => {
const params = {
Bucket: bucketName,
Key: picture_key,
};
return s3.deleteObject(params).promise();
};
2
Answers
You can try below code, if npm aws module properly installed,
Then, you can use the following code to delete an object from an S3 bucket:
Replace ‘REGION’ with the region where your S3 bucket is located, and replace ‘YOUR_BUCKET_NAME’ and ‘OBJECT_KEY’ with the name of your bucket and the key of the object you want to delete, respectively.
Make sure that the IAM role or user associated with your AWS credentials has the necessary permissions to delete objects from the S3 bucket.
If you want to delete all objects in a bucket, you can use the following code: