AWS S3 Uploading Image To Bucket - VERY Slow

Viewed 720

I'm trying to upload an image to S3 with the Node AWS-SDK.

First I encode an image to BASE64 and then make a Buffer with Buffer.from. I then send the request to S3 with S3.upload or S3.PutObject. It is working however it is extremely slow...

An image of size 150kb uploaded to a S3 Bucket takes around 30 - 60 seconds! I have tried setting different regions, but with no effect.

The code is like follows:


let buf = Buffer.from(
         base64.replace(/^data:image\/\w+;base64,/, ""),
         "base64"
       );


const uploadParams = {
         Bucket: "BUCKETNAME",
         Body: BUFFERFILE,
         Key: fileName,
         ContentEncoding: "base64",
         ContentType: "image/jpeg",
       };

       s3.upload(uploadParams, function (err, data) {
         if (err) {
           console.log(err);
           console.log("Error uploading data: ", data);
         } else {
           console.log("succesfully uploaded the image!");
         }
       });

Any help is appreciated! I am stuck at this point for a long time now...

1 Answers

This looks perfectly fine, how are you measuring the elapsed time, are you measuring the time from the PUT to the response/callback? (Code below)

Are you running this within AWS Cloud (on an EC2 instance/Lambda/Fargate container) ?

let buf = Buffer.from(
     base64.replace(/^data:image\/\w+;base64,/, ""),
     "base64"
   );

   console.time('s3Upload');
   s3.upload(uploadParams, function (err, data) {
     console.timeEnd('s3Upload');
     if (err) {
       console.log(err);
       console.log("Error uploading data: ", data);
     } else {
       console.log("succesfully uploaded the image!");
     }
   });
Related