如何使用 multipart/form-data 创建将 pdf 文件作为输入的 AWS Lambda/API 网关 python 函数?

How to create an AWS Lambda/API gateway python function that takes a pdf file as input using multipart/form-data?

我已经为此苦苦挣扎了一段时间。我需要在 API 网关中创建一个链接到 lambda 函数的资源,该函数将 pdf 文件作为输入作为 multipart/form-data POST 请求发送。为了简单起见,我现在只是返回文件。

当我尝试使用以下 curl 调用 API 时,我从 AWS 收到 Internal server error。有没有人成功地将 pdf 文件发送到 Lambda 而无需使用 S3 技巧(上传到 S3)?

提前感谢大家的提示。

Commands/Files:

卷曲

curl -vvv -X POST -H "Content-Type: multipart/form-data" -F "content=@file.pdf" https://...MYAPIHERE.../pdf

我目前正在使用无服务器和 python3。

以下是我的文件:

Servelerlss.yaml

function:
  pdf:
    handler: handler.pdf
    events:
      - http:
          path: /pdf
          method: post 
          integration: lambda
          request:
            template:
              application/json: "$input.json('$')"
          response:
            headers:
              Content-Type: "'aplication/json'"

handler.py

def pdf(event, context):
    pdf = event.get('content')
    out = {'statusCode': 200,
           'isBase64Encoded': False,
           'headers': {"content-type": "application/json"},
           'body': json.dumps({
               'input':  pdf,
               'inputType': 'url',
               #'tags': list(tags.keys()),
               'error': None})}
    return(out)

在 AWS 支持团队的帮助下,经过大量 google 我终于设法解决了这个问题。

原来API网关检查传入请求中的headers:"Content-Type"或"Accept"并将其与的设置相匹配Binary Media Type 决定哪个负载被认为是二进制的。这意味着我们需要指定两种内容类型(multipart/form-data、application/pdf)作为二进制媒体类型.

可以通过使用 serverless-apigw-binary 并将这些添加到 serverless.yaml:

plugins:
  - serverless-apigw-binary 

custom:
  apigwBinary:
    types:           #list of mime-types
      - 'multipart/form-data'
      - 'application/pdf'

但由于 lambda 需要 application/json 格式的有效负载来自 API 网关,因此无法直接传递二进制数据。因此,ContentHandling 的设置应设置为“CONVERT_TO_TEXT”。在 yaml 文件中,这转化为:

contentHandling: CONVERT_TO_TEXT

Kris Gohlson 在 serverless-thumbnail 解决了最后的问题。谢谢你的克里斯。我只是想知道你是怎么想出来的...


Serverless.yaml

plugins:
  - serverless-apigw-binary 

custom:
  apigwBinary:
    types:           #list of mime-types
      - 'multipart/form-data'
      - 'application/pdf'

function:
  pdf:
    handler: handler.pdf
    events:
      - http:
          path: /pdf
          method: post 
          integration: lambda
          request:
            contentHandling: CONVERT_TO_TEXT
            passThrough: WHEN_NO_TEMPLATES
            template:
              application/pdf: "{'body': $input.json('$')}"
              multipart/form-data: "{'body': $input.json('$')}"
          response:
            contentHandling: CONVERT_TO_BINARY
            headers:
              Content-Type: "'aplication/json'"