Integration with AWS S3
What You'll Need
Backbench account - Sign Up, the personal account will always remain free to use.
Amazon Web Services account - Sign Up, create an account, create an IAM user on IAM panel, generate AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
Prerequisites for the app
Set the above generated keys along with AWS_DEFAULT_REGION with their values as an evironment variable.
AWS_ACCESS_KEY_ID xxxxxxx
AWS_SECRET_ACCESS_KEY xxxxxxxxx
AWS_DEFAULT_REGION us-east-1
Operation 1: Listing all files along with metadata in a storage bucket of AWS S3.
Backend
Select +, in the upper right corner to create a Module. For example, say "Listing_objects.js" and select CREATE or hit Enter.
Copy and paste the code module from below.
Replace BUCKET NAME and BUCKET KEY in the code.
Select save.
var AWS = require("aws-sdk");
var s3 = new AWS.S3();
module.exports.endpoint= function(req,cb) {
var myBucket = "BUCKET NAME";
var myKey = "BUCKET KEY";
var params = { Bucket: "test-0023" };
s3.listObjects(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
cb(undefined,data);
});
};
Operation 2: Uploading file in a storage bucket.
Backend
Select +, in the upper right corner to create a Module. For example, say "Uploading_object.js" and select CREATE or hit Enter.
Copy and paste the code module from below.
Replace BUCKET NAME and BUCKET KEY in the code.
Select save.
var AWS = require("aws-sdk");
var s3 = new AWS.S3();
module.exports.endpoint= function(req,cb) {
var params = {
Bucket: "BUCKET NAME";
Key: "BUCKET KEY";
};
s3.putObject(params, function(err, data) {
if (err){
console.log(err, err.stack);
}
console.log(data);
cb(undefined,data);
});
};
Operation 3: Deleting a file in a storage bucket.
Backend
Select +, in the upper right corner to create a Module. For example, say "deleting_object.js" and select CREATE or hit Enter.
Copy and paste the code module from below.
Replace BUCKET NAME and BUCKET KEY in the code.
Select save.
var AWS = require("aws-sdk");
var s3 = new AWS.S3();
module.exports.endpoint= function(req,cb) {
var params = {
Bucket: "BUCKET NAME";
Key: "BUCKET KEY";
};
s3.deleteObject(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data);
cb(undefined,data);
});
};
Last updated