/ jonathan dion / posts

gzip json to aws s3 using nodejs

Today, I had to upload a json file to s3 with gzip compression using nodejs.

I thought would be a great idea to share a sample:

const zlib = require('zlib')
const util = require('util')

const AWS = require('aws-sdk')
const s3 = new AWS.S3();

const gzip = util.promisify(zlib.gzip)

async function main() {
  try {
    const data = JSON.stringify({ name: "somedata" })
    const buffer = new Buffer.from(data)
    const compressed = await gzip(buffer)

    const params = {
      Body: compressed,
      Bucket: "my-bucket-here",
      Key: "my-data.json.gz",
      ContentType: 'application/json',
      ContentEncoding: "gzip",
    };

    await s3.putObject(params).promise()
  } catch(e) {
    console.log(e);
  }
}

main()

You can find more samples and scripts on my repo