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

Jan 23, 2022

const AWS = require(‘aws-sdk’);
const fs = require(‘fs’);
const path = require(‘path’);
const axios = require(‘axios’);

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

var s3 = new AWS.S3();

var filePath = “file.txt”;

//configuring parameters
var params = {
Bucket: ‘<bucket>’,
Body : fs.createReadStream(filePath),
Key : Date.now()+”_”+path.basename(filePath),
ContentType: ‘application/octet-stream’
};

s3.getSignedUrl(‘putObject’, params, function (err, url) {
fs.writeFileSync(“./url.txt”, url);
axios({
method: “put”,
url,
data: fs.readFileSync(filePath),
headers: {
“Content-Type”: “application/octet-stream”
}
})
.then((result) => {
console.log(‘result’);
}).catch((err) => {
console.log(‘err’,err);
});
});