如何通过 API 网关在 AWS Lambda 中将 "pdf" & "html" 文件作为 "events" 传递?
How to pass "pdf" & "html" files as "events" in AWS Lambda via API Gateway?
我正在尝试将 "pdf" 或 "html" 文件直接传递给 lambda 函数。但是我不明白应该以正确的格式接收它?
例如:我能够理解如何使用以下代码通过 lambda 函数传递 "image" 文件:但是我如何发送 pdf 或 html 文件?
def write_to_file(save_path, data):
with open(save_path, "wb") as f:
f.write(base64.b64decode(data))
def ocr(img):
ocr_text = pytesseract.image_to_string(img, config = "eng")
return ocr_text
def lambda_handler(event, context=None):
write_to_file("/tmp/photo.jpg", event["body"])
im = Image.open("/tmp/photo.jpg")
try:
ocr_text = ocr(im)
except Exception as e:
print(e)
# Return the result data in json format
return {
"statusCode": 200,
"body": ocr_text
}
编辑:我试图通过 API 网关(二进制)而不是通过 S3 直接传递 "pdf" 或 "html"。
您可以使用 API 网关内容类型转换。
你可以参考这个documentation
谢谢。但经过大量在线搜索和 try/repeat,能够找到 html 文件的答案。类似的东西也应该适用于 pdf。
import json
import bs4
from bs4 import BeautifulSoup
from bs4.element import Comment
import base64
def tag_visible(element):
if element.parent.name in ['style', 'script', 'head', 'title', 'meta','table', '[document]']:
return False
if isinstance(element, Comment):
return False
return True
def lambda_handler(event, context):
# This will work for testing purpose only
#soup = BeautifulSoup(event["body"], "html.parser")
# This will work when you actually upload files
file_upload = base64.b64decode(event["body"])
soup = BeautifulSoup(file_upload, "html.parser")
print(soup)
texts = soup.findAll(text=True)
visible_texts = filter(tag_visible, texts)
full_text = str(u" ".join(t.strip() for t in visible_texts))
return {
"statusCode": 200,
"body": json.dumps(full_text)
}
此外,在 API 网关中 - 您需要进行以下 2 项更改:
- 在二进制媒体类型中添加 /
- 在方法响应下 - 添加 "Content-Type" = "application/html"
我正在尝试将 "pdf" 或 "html" 文件直接传递给 lambda 函数。但是我不明白应该以正确的格式接收它?
例如:我能够理解如何使用以下代码通过 lambda 函数传递 "image" 文件:但是我如何发送 pdf 或 html 文件?
def write_to_file(save_path, data):
with open(save_path, "wb") as f:
f.write(base64.b64decode(data))
def ocr(img):
ocr_text = pytesseract.image_to_string(img, config = "eng")
return ocr_text
def lambda_handler(event, context=None):
write_to_file("/tmp/photo.jpg", event["body"])
im = Image.open("/tmp/photo.jpg")
try:
ocr_text = ocr(im)
except Exception as e:
print(e)
# Return the result data in json format
return {
"statusCode": 200,
"body": ocr_text
}
编辑:我试图通过 API 网关(二进制)而不是通过 S3 直接传递 "pdf" 或 "html"。
您可以使用 API 网关内容类型转换。
你可以参考这个documentation
谢谢。但经过大量在线搜索和 try/repeat,能够找到 html 文件的答案。类似的东西也应该适用于 pdf。
import json
import bs4
from bs4 import BeautifulSoup
from bs4.element import Comment
import base64
def tag_visible(element):
if element.parent.name in ['style', 'script', 'head', 'title', 'meta','table', '[document]']:
return False
if isinstance(element, Comment):
return False
return True
def lambda_handler(event, context):
# This will work for testing purpose only
#soup = BeautifulSoup(event["body"], "html.parser")
# This will work when you actually upload files
file_upload = base64.b64decode(event["body"])
soup = BeautifulSoup(file_upload, "html.parser")
print(soup)
texts = soup.findAll(text=True)
visible_texts = filter(tag_visible, texts)
full_text = str(u" ".join(t.strip() for t in visible_texts))
return {
"statusCode": 200,
"body": json.dumps(full_text)
}
此外,在 API 网关中 - 您需要进行以下 2 项更改:
- 在二进制媒体类型中添加 /
- 在方法响应下 - 添加 "Content-Type" = "application/html"