Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

upload file to s3 using pre signed url javascript

const AWS = require('aws-sdk');
require('dotenv').config();

const region = process.env.REGION;
const accessKeyId = process.env.ACCESS_KEYID;
const secretAccessKey = process.env.SECRET_ACCESSKEY;
const bucketName = process.env.BUCKETNAME;
AWS.config.update({
    accessKeyId,
    secretAccessKey,
    region
});

const s3 = new AWS.S3();
const uploadtos3 = (key, body, encoding, fileType, callback) => {
	console.log('test')
	const param = {
	Bucket : bucketName,
	Key : key,
	Body : body,
	ContentEncoding : encoding,
	ContentType : fileType,
	};
	
	s3.putObject(param , (err , data) => {
	if(err) {
		console.log("Some error occured during uploading to AWS S3");
		console.log(err);
		console.log("Responding back to client site ...");
		console.log({error : "Please try again later"});
		console.log("Responded back to client site");
		return callback('failed')
	}
	console.log("Uploaded successfully");
	console.log({data});
	console.log({success : true});
	console.log(param)
		callback('success')
	})
}
 
PREVIOUS NEXT
Tagged: #upload #file #pre #signed #url #javascript
ADD COMMENT
Topic
Name
9+8 =