expressjsHow do I use Express.js to upload a file to Amazon S3?
The following example demonstrates how to use Express.js to upload a file to Amazon S3.
// require the AWS SDK
const AWS = require('aws-sdk');
// configure the keys for accessing AWS
AWS.config.update({
accessKeyId: 'ACCESS_KEY_ID',
secretAccessKey: 'SECRET_ACCESS_KEY'
});
// create an instance of S3
const s3 = new AWS.S3();
// define the bucket and file name
const bucketName = 'BUCKET_NAME';
const fileName = 'FILE_NAME';
// read content from the file
const fileContent = fs.readFileSync(fileName);
// set up the parameters for upload
const uploadParams = {
Bucket: bucketName,
Key: fileName,
Body: fileContent
};
// upload the file to S3
s3.upload(uploadParams, function (err, data) {
if (err) {
console.log('Error', err);
}
if (data) {
console.log('Uploaded in:', data.Location);
}
});
Output example
Uploaded in: <S3_Location>
- Require the AWS SDK -
const AWS = require('aws-sdk');
- Configure the keys for accessing AWS -
AWS.config.update({ accessKeyId: 'ACCESS_KEY_ID', secretAccessKey: 'SECRET_ACCESS_KEY' });
- Create an instance of S3 -
const s3 = new AWS.S3();
- Define the bucket and file name -
const bucketName = 'BUCKET_NAME'; const fileName = 'FILE_NAME';
- Read content from the file -
const fileContent = fs.readFileSync(fileName);
- Set up the parameters for upload -
const uploadParams = { Bucket: bucketName, Key: fileName, Body: fileContent };
- Upload the file to S3 -
s3.upload(uploadParams, function (err, data) { ... });
Helpful links
More of Expressjs
- How do I use adm-zip with Express.js?
- How do I download a zip file using Express.js?
- How do I find Express.js tutorials on YouTube?
- How do I set the time zone in Express.js?
- How can I set up X-Frame-Options in ExpressJS?
- How do I use Express.js to handle x-www-form-urlencoded data?
- How can I use Express.js to generate a zip response?
- How can I use Express.js to yield results?
- How can I use the x-forwarded-for header in Express.js?
- How do I download a zip file using Express.js?
See more codes...