Upload binary image file

I’m trying to upload a binary file directly to a lambda for storing in s3.
It seems to work except the outcome has the same size but is not a readable png.
Any idea which conversation is missing?

  //support binary files
  if (typeof event === 'string'){    
    params.Body = new Buffer(event, "binary");
    params.Key = 'binary/' + Math.round(Math.random()*1000000);
    if (event.substr(0,10).indexOf('PNG') > -1){
        params.Key += '.png';
    }
  }

Test via

serverless invoke local --function hello --path bot.png -l true

You need to make sure the object’s ContentType (aka. MIME type) is set correctly - the CLI tool will take care of this for you, but if you’re doing your upload with the SDK you will need to do it yourself.

Have you thought about using direct upload to S3 instead of going via Lambda? See http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-authentication-HTTPPOST.html

1 Like

I tried that and hit the wall. AWS Docs are fuzzy and their example only works with text files. The most I came forward is to upload a corrupted image using this AWS S3 service proxy approach.