Created
September 24, 2019 16:45
-
-
Save jkasun/b0e2b818e0271db9c1363f637dacf12e to your computer and use it in GitHub Desktop.
Uploading Files to S3 With Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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