如何通过 aws API 网关从 aws lambda 中的简单 curl post 请求获取文件名
How to get the file name from a simple curl post request in aws lambda throu aws API gateway
我有以下 curl 请求,
curl -X POST "https://foo123.execute-api.us-east-1.amazonaws.com/default/test-2" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "file=@wiki.png;type=image/png"
由于现有的管道结构,我无法更改此 curl 请求的当前架构,我想在使用 aws API 网关时在 lambda 函数中访问此文件的名称作为触发器。
我知道我可以简单地在 header 中发送文件名,但这对我要使用它的管道不起作用,所以我的问题是。
鉴于我无法更改我的 curl 请求,我如何在 aws 的 lambda 函数中访问此文件的名称。
为了进一步阐明我在 flask APIs 中的问题,我们可以通过简单地使用类似的东西来获取此上传文件的名称。
args = upload_parser.parse_args()
uploaded_file = args['file']
required_file_name = uploaded_file.filename
有趣的问题,实际上您将正文的值作为 base64 编码的图像获取,您将不得不使用任何现有的库来解析它的名称和正文的内容,或者编写您自己的解析器,您可以请参考下面的代码片段:
import cv2
import os
import base64
import numpy as np
import email
def http_api(event):
post_data = base64.b64decode(event['body'])
# fetching content-type
try:
content_type = event["headers"]['Content-Type']
except:
content_type = event["headers"]['content-type']
# concate Content-Type: with content_type from event
ct = "Content-Type: "+content_type+"\n"
# parsing message from bytes
msg = email.message_from_bytes(ct.encode()+post_data)
if msg.is_multipart():
multipart_content = {}
# retrieving form-data
for part in msg.get_payload():
# checking if filename exist as a part of content-disposition header
if part.get_filename():
# fetching the filename
file_name = part.get_filename()
multipart_content[part.get_param('name', header='content-disposition')] = part.get_payload(decode=True)
img_str = multipart_content["file"]
nparr = np.fromstring(img_str, np.uint8)
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
return image
我有以下 curl 请求,
curl -X POST "https://foo123.execute-api.us-east-1.amazonaws.com/default/test-2" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "file=@wiki.png;type=image/png"
由于现有的管道结构,我无法更改此 curl 请求的当前架构,我想在使用 aws API 网关时在 lambda 函数中访问此文件的名称作为触发器。
我知道我可以简单地在 header 中发送文件名,但这对我要使用它的管道不起作用,所以我的问题是。
鉴于我无法更改我的 curl 请求,我如何在 aws 的 lambda 函数中访问此文件的名称。
为了进一步阐明我在 flask APIs 中的问题,我们可以通过简单地使用类似的东西来获取此上传文件的名称。
args = upload_parser.parse_args()
uploaded_file = args['file']
required_file_name = uploaded_file.filename
有趣的问题,实际上您将正文的值作为 base64 编码的图像获取,您将不得不使用任何现有的库来解析它的名称和正文的内容,或者编写您自己的解析器,您可以请参考下面的代码片段:
import cv2
import os
import base64
import numpy as np
import email
def http_api(event):
post_data = base64.b64decode(event['body'])
# fetching content-type
try:
content_type = event["headers"]['Content-Type']
except:
content_type = event["headers"]['content-type']
# concate Content-Type: with content_type from event
ct = "Content-Type: "+content_type+"\n"
# parsing message from bytes
msg = email.message_from_bytes(ct.encode()+post_data)
if msg.is_multipart():
multipart_content = {}
# retrieving form-data
for part in msg.get_payload():
# checking if filename exist as a part of content-disposition header
if part.get_filename():
# fetching the filename
file_name = part.get_filename()
multipart_content[part.get_param('name', header='content-disposition')] = part.get_payload(decode=True)
img_str = multipart_content["file"]
nparr = np.fromstring(img_str, np.uint8)
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
return image