1. Home
  2. /
  3. Gyan
  4. /
  5. s3 test
s3 test

Jan 19, 2022

Creating a Node JS project an example file for upload
——————-
npm init
npm install aws-sdk –save
Now, create a “index.js” file in the root folder and put the following content
const AWS = require(‘aws-sdk’);
const fs = require(‘fs’);
const path = require(‘path’);

//configuring the AWS environment
AWS.config.update({
accessKeyId: ““,
secretAccessKey: “
});

var s3 = new AWS.S3();
var filePath = “file.txt”;

//configuring parameters
var params = {
Bucket: ‘‘,
Body : fs.createReadStream(filePath),
Key : Date.now()+”_”+path.basename(filePath)
};

s3.upload(params, function (err, data) {
//handle error
if (err) {
console.log(“Error”, err);
}

//success
if (data) {
console.log(“Uploaded in:”, data.Location);
}
});