node.js - Proper request template mapping or process in order to upload a photo to s3 using Serverless Framework -


i using serverless version 1.0.0 , node version 5.7.1

i have endpoint updating photo of table in mysql database. prior inserting said photo, uploading formdata browser s3, , update photo url using return image url.

the problem don't know proper way define request mapping template in serverless.yml extract photo, , path parameters , $context variable principal id

here current serverless.yml function definition

updateboardphoto:   handler: handler.updateboardphoto   events:     - http:        path: boards/{boardid}/photo        method: put        integration: lambda        parameters:          headers:            authorization: false          body:            photo: true          paths:            boardid: true        request:          passthrough: when_no_templates          template:            multipart/form-data: '{"principalid" : "$context.authorizer.principalid", "body" : $input.json("$"), "boardid": "$input.params(''boardid'')"' 

now here handler have:

function updateboardphoto(event, context, callback) { var photo = event.body.photo; var boardid = event.boardid; var principal = utils.processprincipalid(event.principalid);  var s3filename = randomstring.generate(10) + ".jpg"; var s3 = new aws.s3(); s3.putobject({     bucket: config.upload_bucket,     key: s3filename,     body: photo,     acl: 'public-read', }, function (err, data) {     if (err) {         throw err;     } else {         console.log(data);         context.succeed(data);     } }); 

attempt 1

i tried use when_no_templates passthrough option, , defined no template photo buffer input variable, , no boardid. upload photo s3.

request:   passthrough: when_no_templates  // event variable get: { photo: <buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 02 1c 00 00 00 ac 08 06 00 00 00 30 ab 75 20 00 00 00 19 74 45 58 74 53 6f 66 74 77 61 72 65 00 ... >, 

isoffline: true, stagevariables: {} }

attempt 2

using ff request definition:

request:   passthrough: when_no_match   template:     multipart/form-data: '{"principalid" : "$context.authorizer.principalid", "body" : $input.json("$"), "boardid": "$input.params(''boardid'')"'  // event variable { isoffline: true, stagevariables: {} } 

i see no variables in event variable @ all! no photo, nor boardid.

can tell me i'm doing wrong? using postman test.

i think passing image json format, can use $input.body access entire body in template. should put single single quote around parameter name instead of double single quote.

like, multipart/form-data: '{"principalid" : "$context.authorizer.principalid", "body" : "$input.body", "boardid": "$input.params('boardid')"'


fyi:

api gateway doesn't support binary data currently. recommend send base64 encoded string of image api gateway, base64 decoded before put s3 in lambda function.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -