Skip to content

Instantly share code, notes, and snippets.

@navjotdhanawat
Created July 3, 2018 07:17
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save navjotdhanawat/92f8683ddfc5bf99c6bd47ce6dedaa4e to your computer and use it in GitHub Desktop.
Save navjotdhanawat/92f8683ddfc5bf99c6bd47ce6dedaa4e to your computer and use it in GitHub Desktop.
Create json and upload to s3 bucket using nodejs.
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!!!');
}
});
@chamix
Copy link

chamix commented Jul 12, 2021

Thanks, I was looking for Buffer.from ... :)

@DennisLoska
Copy link

good stuff thanks 👍

@haithai91
Copy link

could you please give me advice, on the reason why we use Buffer.from to convert STRING to Buffer?

@Victor-ARC
Copy link

Thanks! I had the same idea of using Buffer but I wasn't sure about the S3 configuration to store it as JSON.
Cheers mate :)

@Victor-ARC
Copy link

could you please give me advice, on the reason why we use Buffer.from to convert STRING to Buffer?

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. The Buffer 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.

@Kareszrk
Copy link

Kareszrk commented Dec 7, 2022

thank you

@devAshutoshkarn
Copy link

Thanks brother

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment