Created
July 3, 2018 07:17
-
-
Save navjotdhanawat/92f8683ddfc5bf99c6bd47ce6dedaa4e to your computer and use it in GitHub Desktop.
Create json and upload to s3 bucket using nodejs.
This file contains hidden or 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
var AWS = require('aws-sdk'); | |
AWS.config.update({ region: 'us-east-1' }); | |
var s3 = new AWS.S3(); | |
var obj = { | |
firstname: "Navjot", | |
lastname: "Dhanawat" | |
}; | |
var buf = Buffer.from(JSON.stringify(obj)); | |
var data = { | |
Bucket: 'bucket-name', | |
Key: 'filename.json', | |
Body: buf, | |
ContentEncoding: 'base64', | |
ContentType: 'application/json', | |
ACL: 'public-read' | |
}; | |
s3.upload(data, function (err, data) { | |
if (err) { | |
console.log(err); | |
console.log('Error uploading data: ', data); | |
} else { | |
console.log('succesfully uploaded!!!'); | |
} | |
}); |
thank you
Thanks brother
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Basically, as I understand it, to transfer data across the internet (in this case, the content of a file) it needs to be represented as chunks of binary information that can be segmented and indexed while the transfer is undergoing. A simple
string
would not do the trick since its representation –while at a machine level can be read as binary– is not formatted correctly, I think it is technically just a pointer to a memory range where it is stored. TheBuffer
here actually creates –or rather, replicates– the binary information that represents the content of the string variable and makes it available as a variable itself.