How to do serverless binary configuration right

馋奶兔 提交于 2019-12-13 03:20:21

问题


I started with this example.

... ended up with this failing code.

I wanted to return a simple pdf file.

I find that SLS is not converting to binary.

Here is the serverless.yml file.

functions:
  hello:
    handler: handler.hello
    description: Can we display a PDF?
    events:
      - http:
          path: hello
          method: get
          contentHandling: CONVERT_TO_BINARY

plugins:
  - serverless-apigw-binary
  - serverless-apigwy-binary

custom:
  apigwBinary:
    types:
      - 'application/pdf'

And here is the handler

'use strict';

var fs = require('fs');

module.exports.hello = (event, context, callback) => {
    fs.readFile("templates/i-130.pdf", function (err, data) {
        callback(null, {
            statusCode: 200,
            headers: {'Content-Type': 'application/pdf'},
            body: JSON.stringify({
                message: data.toString('base64')
            })
        });
    });
};

The problems I see are:

  • content is NOT converted to binary
  • header is NOT set to application/pdf

How can this be fixed?

AND for the sake of others, can you just fix the code so it works?

来源:https://stackoverflow.com/questions/54317704/how-to-do-serverless-binary-configuration-right

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!