0

I'm trying to upload from my lambda (nodejs express) to s3 bucket. But whenever I upload, and look for my uploaded file in S3, it only shows a white small box. 1

I already try converting my file buffer to base64, but it still not working. My uploaded file only show if I upload it using my local api(localhost).

Here's my code:

// multer middleware
const multer = require("multer");
const helpers = require("../helpers/image-upload-helper");
const storage =multer.memoryStorage();

let upload = multer({
   storage: storage,
   fileFilter: helpers.imageFilter,
   }).any();

//controller
 try {
    if(req.files){
      for (const file of req.files) {
        const ImageName = randomImageNameGenerator.randomImageName()
        const params = {
            Bucket: process.env.BUCKET_NAME,
            Key: ImageName,
            Body: file.buffer, 
            ContentType : file.mimetype,
        }
        const command = new PutObjectCommand(params)
        const myData =  await s3.send(command)
      }
    }


//log of my command 
PutObjectCommand {
  middlewareStack: {
    add: [Function: add],
    addRelativeTo: [Function: addRelativeTo],
    clone: [Function: clone],
    use: [Function: use],
    remove: [Function: remove],
    removeByTag: [Function: removeByTag],
    concat: [Function: concat],
    applyToStack: [Function: cloneTo],
    identify: [Function: identify],
    resolve: [Function: resolve]
  },
  input: {
    Bucket: 'my-bucket',
    Key: 'b4651myrandomkey8efab90aba02e53767f247f231cfb7ee431e34877bf21ab1bd655b3789',
    Body: <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 01 f8 00 00 00 f5 08 06 00 00 00 bc c1 e7 15 00 00 00 01 73 52 47 42 00 ae ce 1c e9 00 00 20 00 ... 10640 more bytes>,
    ContentType: 'image/png'
  }
}

// log of myData 

{
  '$metadata': {
    httpStatusCode: 200,
    requestId: '6C1EM009PP420NRK',
    extendedRequestId: 'ZfGR4AR4mElYOSGes68YqEegptyO5PY5iPCvplP89wr1nqT4DZHwo0D0bl5qyZ7aAB0HaDaTAKU=',
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  ETag: '"96425366df243451e35a941524b2a019a6ad2b"',
  ServerSideEncryption: 'ABDS256',
  VersionId: 'rpgj.L5AwGNCcKVzatIY5zHf_SYVNWt0'
}

Note: I didn't see any error in my cloud watch

1 Example of what the white box looks like enter image description here

  • Sorry, what do you mean by "a white small box"? – shimo Oct 25 '22 at 22:53
  • A picture of white small box instead of the image I have uploaded – Frozen Elsa Oct 26 '22 at 23:15
  • I am having a similar issue, are you still experiencing this? I am curious if you have tried the solution outlined here: https://stackoverflow.com/questions/69833454/using-lambda-to-get-image-from-s3-returns-a-white-box-in-python assuming you have API Gateway in front of your Lambda. – Gibron Nov 10 '22 at 22:20
  • 1
    Yes I didnt know that I need to add binary media types and also allow headers in my api gateway. Also need to convert file buffer to base64. It is now solved. – Frozen Elsa Nov 11 '22 at 23:27
  • Happy its solved! Please post your solution as an answer and then mark it as answered for others who arrive here trying to solve the same problem. – Gibron Nov 14 '22 at 19:41

1 Answers1

0

For those that arrived here as I did with the same issue, this answer solved it for me:
Using Lambda to get image from S3 returns a white box in Python

And for those using serverless, this is also relevant:
Serverless I image upload to S3 broken after deploy, local worked only

Gibron
  • 1,350
  • 1
  • 9
  • 28