Reputation: 11755
I have a Node.JS app using mLab as a DB and also AWS/S3 to store files. I just implemented a functionality allowing the user to erase information from the mLab DB, this works fine.
The one thing I still have to do is to erase possible relevant information from AWS/S3, and this I don't know how to do yet.
I have already browsed the net and seen a couple of things, but nothing quite satisfying.
Can someone tell exactly (if possible clearly and simply) what I need to do (necessary npm module if there is, etc...) to be able to delete objects from my AWS/S3 bucket. A good tutorial on the subject would also be fine if there is any.
Upvotes: 0
Views: 256
Reputation: 427
Using the SDK for JavaScript (v3) is easier; you just need to follow a few steps to delete an object.
First install the @aws-sdk/client-s3 package:
Using npm:(more package installer)
npm install @aws-sdk/client-s3
Import DeleteObjectCommand
and S3Client
from @aws-sdk/client-s3
import { DeleteObjectCommand, S3Client } from "@aws-sdk/client-s3";
Create a new instance:
const s3Client = new S3Client({});
Create an asynchronous function for delete an object:
export const deleteObject = async () => {
const deleteCommand = new DeleteObjectCommand({
Bucket: "bucket_name",
Key: "your_object_name_with_file_extension", // kayes_note.txt
});
try {
const response = await s3Client.send(deleteCommand);
console.log(response);
} catch (err) {
console.error(err);
}
};
For more info, see official documentation here.
Upvotes: 0
Reputation: 31761
AWS provides an SDK for JavaScript. First, ensure that you have set your credentials in whatever way makes sense for you. Next install the sdk:
npm i aws-sdk
Deleting an object from a bucket:
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const params = {
Bucket: 'examplebucket',
Key: 'objectkey.jpg'
};
s3.deleteObject(params, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
else {
console.log(data); // successful response
}
});
A couple of notes:
Upvotes: 1