【问题标题】:Serverless Framework API Gateway Websockets Binary Data无服务器框架 API 网关 Websockets 二进制数据
【发布时间】:2021-05-11 18:37:36
【问题描述】:

有人知道如何在无服务器框架中将 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,但我不确定如何实现。

这些软件包似乎都不能处理 websocket:

  • 无服务器-apigw-二进制
  • 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 中管理的,我不想承担迁移的技术债务(除非我必须这样做)

【问题讨论】:

  • 点赞。这个问题非常清楚和详细。回到这个问题,我认为 sls 还没有此选项可用于转换为二进制文件。也许将来,他们可能会根据要求引入

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


【解决方案1】:

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

【讨论】:

  • 如果您只是要将所有内容路由到默认处理程序,则不需要对二进制数据进行 base64 编码。
  • @Spankied 你可能是对的——回想起来,我不确定我是否应该接受我的答案;我仍然希望有更好的解决方案
  • 这是迄今为止我见过的最好的解决方案,其他的需要你开发自己的协议。我最终完全放弃了这个任务的 websocket,只使用一个 http 端点,用户可以将数据上传到 S3,其他人可以检索它。
猜你喜欢
  • 1970-01-01
  • 2015-07-22
  • 2021-04-30
  • 2019-10-07
  • 1970-01-01
  • 2021-05-06
  • 2020-01-18
  • 2020-11-08
  • 1970-01-01
相关资源
最近更新 更多