Skip to content

Instantly share code, notes, and snippets.

@samvirkar144
Forked from jkasun/index.js
Created May 6, 2020 10:12
Show Gist options
  • Save samvirkar144/e3250e8bbdcdee547267ef3dadb843c0 to your computer and use it in GitHub Desktop.
Save samvirkar144/e3250e8bbdcdee547267ef3dadb843c0 to your computer and use it in GitHub Desktop.
Uploading Files to S3 With Node.js
const fs = require('fs');
const AWS = require('aws-sdk');
// Enter copied or downloaded access id and secret here
const ID = '';
const SECRET = '';
// Enter the name of the bucket that you have created here
const BUCKET_NAME = 'test-bucket-1242tsr';;
// Initializing S3 Interface
const s3 = new AWS.S3({
accessKeyId: ID,
secretAccessKey: SECRET
});
const uploadFile = (fileName) => {
// read content from the file
const fileContent = fs.readFileSync(fileName);
// setting up s3 upload parameters
const params = {
Bucket: BUCKET_NAME,
Key: 'cat.jpg', // file name you want to save as
Body: fileContent
};
// Uploading files to the bucket
s3.upload(params, function(err, data) {
if (err) {
throw err
}
console.log(`File uploaded successfully. ${data.Location}`)
});
};
// Enter the file you want to upload here
uploadFile('cat.jpg');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment