Python 如何将PDF发布到AWS lambda

Python 如何将PDF发布到AWS lambda,python,post,aws-lambda,Python,Post,Aws Lambda,我已经安排好了 def lambda_handler(event, context): return { 'statusCode': 200, 'body': json.dumps(event) } 我想在PDF文件中发布,以便我可以在lambda函数中对其进行操作 这是我的POSTcode import requests headers = { 'X-API-KEY':'1234', 'Content-type': 'mult

我已经安排好了

def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'body': json.dumps(event)
    }
我想在PDF文件中发布,以便我可以在lambda函数中对其进行操作

这是我的
POST
code

import requests

headers = {
    'X-API-KEY':'1234',
    'Content-type': 'multipart/form-data'}

files = {
    'document': open('my.pdf', 'rb')
}

r = requests.post(url, files=files,  headers=headers)

display(r)
display(r.text)
我得到一个错误:

<Response [400]>
'{"message": "Could not parse request body into json: Unexpected character (\\\'-\\\' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value

这只是PDF文件的一部分,我找不到

我想出来了。花了我很多时间,但我想我明白了。本质上,它是关于编码和解码字节。根本不需要接触API网关

请求:

HEADERS = {'X-API-KEY': '12345'}
data = '{"body" : "%s"}' % base64.b64encode(open(path, 'rb').read())
r = requests.post(url, data=data, headers=HEADERS)
在兰姆达

from io import BytesIO
def lambda_handler(event, context):
    pdf64 = event["body"]

    # Need this line as it does 'b'b'pdfdatacontent'.
    pdf64 = pdf64[2:].encode('utf-8')

    buffer = BytesIO()
    content = base64.b64decode(pdf64)
    buffer.write(content)

我发现这对我来说非常有效:

要求 Lambda-蟒蛇3.8
不太清楚为什么,但对我来说,原始请求中的base64编码(即使使用urlsafe)破坏了文件,并且在Lambda中不再被识别为PDF,因此OP的答案对我不起作用。

看起来你是在阅读恼人的配置文件:)我已经有一段时间没有尝试过这样做了,很高兴你能找到另一个解决办法。我对Lambda的这个项目不满意,所以我从来没有回避过这个问题。
from io import BytesIO
def lambda_handler(event, context):
    pdf64 = event["body"]

    # Need this line as it does 'b'b'pdfdatacontent'.
    pdf64 = pdf64[2:].encode('utf-8')

    buffer = BytesIO()
    content = base64.b64decode(pdf64)
    buffer.write(content)
import requests


file_loc = 'path/to/test.pdf'
data = open(file_loc,'rb').read() #this is a bytes object
r = requests.post(url, data=data)
r.ok #returns True (also a good idea to check r.text

#one-liner
requests.post(url, data=open(file_loc,'rb').read())
import io, base64

body = event["body"]
attachment = base64.b64decode(body.encode()) #this is a bytes object
buff = io.BytesIO(attachment) #this is now useable - read/write etc.

#one-liner
buff = io.BytesIO(base64.b64decode(event["body"].encode()))