Skip to content

Instantly share code, notes, and snippets.

@ideabrian
Forked from jkasun/index.js
Created January 8, 2021 21:06
Show Gist options
  • Save ideabrian/03cd3834b5baa99106f7ba6d0782d59b to your computer and use it in GitHub Desktop.
Save ideabrian/03cd3834b5baa99106f7ba6d0782d59b 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