S3 NodeJS Operations
Important Note: All methods are simplified versions of the actual code
For simplicity I have compiled all aws s3 operations functions into methods that are simplified versions of the actual code. This way you can use/perform basic operations directly. But if required to perform complex operations, I have also provided the complete parameters body in comment in every function of s3.js file. Feel free to modify them as per your requirement.
Make sure to update the region in aws-s3.js file, currently it is set to mumbai region.
Steps to get started
Step 1: Import S3 Module
// Import the module from s3.js
var s3op = require('./aws-s3');
Step 2: Call your method (Important: Use await for all calls)
var bucketName = "test-bucket-delete-later-22"
await s3op.createBucket(bucketName);
Functions Descriptions
Function - createBucket, putObject, deleteObject, deleteObjects and deleteBucket return true if everything goes well, else they return false if any error occurs. If required you can also put a check on function as shown in example below:
var flag = await s3op.deleteObject(bucketName, "test3.txt");
if(flag) {
statusMessage = "File deleted!"
} else {
statusMessage = "File not deleted!"
}
Note to Self
-
What is “Quiet” Key in params JSON of
deleteObjects
function?Answer: The operation supports two modes for the response: verbose and quiet. By default, the operation uses verbose mode in which the response includes the result of deletion of each key in your request. In quiet mode the response includes only keys where the delete operation encountered an error. For a successful deletion, the operation does not return any information about the delete in the response body.
-
Function
deleteBucket
doesn’t work if Bucket contains any object -
Be cautious while writing functions from aws-sdk, sometime functions executes twice.
Avoid writing functions this way (function snippets that are mentioned in aws documents)
await s3.listObjectsV2(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log(data); // successful response }).promise();
Instead write like this
await s3.listObjectsV2(params).promise().then((data) => { console.log(data) }).catch((err) => { console.log('Error: fetching List of Objects from bucket [' + bucketName + ']'); console.log(err); });