3

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

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • Are you uploading to the closest AWS region? How long does it take to upload the same file using the awscli? How long does downloading from S3 take? – jarmod Mar 07 '22 at 01:28
  • Yes I upload to the closest region which is london, but I have tried multiple which didnt change the speed. I will try aws cli and ill let you know! – henkdevijfde Mar 07 '22 at 08:09
  • 1
    I'm experiencing the same exact problem; base64 encoding and all. Did you ever resolve this? – shoe May 12 '22 at 07:46

1 Answers1

0

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!");
     }
   });