Creating an image with gm and uploading to S3, v3

47 Views Asked by At

Many moons ago I made a tiny Lambda in NodeJS 8.x which created a PNG file to display a bunch of generated text and uploaded to S3 using s3.upload and it worked fine. I now want to transition to NodeJS 18.x and the v3 AWS SDK, but it seems everything has changed, while I have been doing no JS whatsoever (so rusty!) so I'm left scratching my head.

Here is a simplified snippet to show how my old function worked...

var s3 = new (require("aws-sdk")).S3();
var gm = require('gm').subClass({ imageMagick: true });

var createPngOnS3 = function() {
    gm(100,100, '#fff')
    .font("Helvetica.ttf", 20)
    .drawText(10, 60, "Hello")
    .stream('png', function(err, stdout, stderr) {
      if (err) {
          console.log("Error uploading data: ", err);
      } else {
          s3.upload({
            Bucket: "myBucket",
            Key: "test.png",
            Body: stdout,
            ContentType: "image/png"
          }, function(err, data) {
            console.log(err, data);
          });
      }
    });
};

Given wanting to keep the memory overheads to a minimum, I'd like to keep with streaming the image data, but I'm looking for pointers to how to use the v3 lib-storage Upload functionality. Any clues?

0

There are 0 best solutions below