Goals
Need an endpoint (DELETE language/image/:course_id/:unit_id/:lesson_id/:word_id) for deleting an image file from S3.
Getting started
Here are some snippets of code on deleting data from AWS S3. Feel free to use them:
const deleteFile = async (filePath) => {
const params = {
Bucket: S3_BUCKET_NAME,
Key: filePath,
};
const s3 = getS3(S3_CREDENTIALS);
await s3.deleteObject(params).promise();
};
/**
* Delete's all of the files in a folder
* @param {String} folderName The id of the patient
* Source: https://stackoverflow.com/questions/20207063/how-can-i-delete-folder-on-s3-with-node-js
*/
const deleteFolder = async (folderName) => {
const params = {
Bucket: S3_BUCKET_NAME,
Prefix: `${folderName}/`,
};
const s3 = getS3(S3_CREDENTIALS);
// Gets up to 1000 files that need to be deleted
const listedObjects = await s3.listObjectsV2(params).promise();
if (listedObjects.Contents.length === 0) return;
const deleteParams = {
Bucket: S3_BUCKET_NAME,
Delete: { Objects: [] },
};
// Builds a list of the files to delete
listedObjects.Contents.forEach(({ Key }) => {
deleteParams.Delete.Objects.push({ Key });
});
// Deletes the files from S3
await s3.deleteObjects(deleteParams).promise();
// If there are more than 1000 objects that need to be deleted from the folder
if (listedObjects.IsTruncated) await deleteFolder(folderName, S3_CREDENTIALS);
};
Goals Need an endpoint (
DELETE language/image/:course_id/:unit_id/:lesson_id/:word_id
) for deleting an image file from S3.Getting started
Here are some snippets of code on deleting data from AWS S3. Feel free to use them: