无服务器框架 API 网关 Websockets 二进制数据

时间:2021-02-08 06:29:26

标签: websocket binary aws-api-gateway serverless-framework

有人知道如何在无服务器框架中将 websocket 的内容处理策略设置为二进制吗?

我有一个 websocket 定义如下:

  my_websocket:
    handler: src/handler.handler
    description: My websocket
    events:
      - websocket:
          route: $default
      - websocket:
          route: $connect
      - websocket:
          route: $disconnect

我的处理程序需要二进制数据

https://docs.aws.amazon.com/apigateway/latest/developerguide/websocket-api-develop-binary-media-types.html

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apitgateway-method-integration.html

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigatewayv2-integrationresponse.html

我需要将 ContentHandlingContentHandlingStrategy 设置为 CONVERT_TO_BINARY,但我不确定如何实现。

这些包似乎都没有处理 websockets:

  • serverless-apigw-binary
  • serverless-apigwy-binary

设置任一道具(或两者)失败:

    events:
      - websocket:
          route: $default
          ContentHandling: CONVERT_TO_BINARY
          ContentHandlingStrategy: CONVERT_TO_BINARY
      - websocket:
          route: $connect
          ContentHandling: CONVERT_TO_BINARY
          ContentHandlingStrategy: CONVERT_TO_BINARY
      - websocket:
          route: $disconnect
          ContentHandling: CONVERT_TO_BINARY
          ContentHandlingStrategy: CONVERT_TO_BINARY

如下:

Serverless: Configuration warning:
Serverless:   at 'functions.my_websocket.events[0].websocket': unrecognized property 'contentHandling'
Serverless:   at 'functions.my_websocket.events[0].websocket': unrecognized property 'ContentHandlingStrategy'
Serverless:   at 'functions.my_websocket.events[1].websocket': unrecognized property 'contentHandling'
Serverless:   at 'functions.my_websocket.events[1].websocket': unrecognized property 'ContentHandlingStrategy'
Serverless:   at 'functions.my_websocket.events[2].websocket': unrecognized property 'contentHandling'
Serverless:   at 'functions.my_websocket.events[2].websocket': unrecognized property 'ContentHandlingStrategy'

值得注意的是,这可以(由朋友)使用 cdk 实现,如下所示:

    const messageIntegration = new CfnIntegration(this, `${name}-message-route-lambda-integration`, {
      apiId: api.ref,
      integrationType: 'AWS_PROXY',
      integrationUri: 'arn:aws:apigateway:' + config['region'] + ':lambda:path/2015-03-31/functions/' + messageFunc.functionArn + '/invocations',
      credentialsArn: role.roleArn,
      contentHandlingStrategy: 'CONVERT_TO_BINARY', // see http://amzn.to/39DkYP4
    })

我正在寻找一种 sls 方法,因为我的所有基础设施都在 sls 中管理,我不想吸收技术债务迁移(除非我必须这样做)

1 个答案:

答案 0 :(得分:0)

我通过 base64 编码我的二进制数据并使用 $default 处理程序“解决”了这个问题,它允许任意数据类型