我有下面的lambda函数,由API网关调用
import sys
import logging
import pymysql
import json
rds_host="rds.amazonaws.com"
name="name"
password="pass"
db_name="DB"
port = 3306
def save_events(event):
result = []
conn = pymysql.connect(rds_host, user=name, passwd=password, db=db_name,
connect_timeout=30)
Bid=pymysql.escape_string("3")
with conn.cursor(pymysql.cursors.DictCursor) as cur:
cur.execute("select exid,exercise_name,image from exercise where bid = 3")
result = cur.fetchall()
cur.close()
print ("Data from RDS...")
print (result)
workout = json.dumps(result)
workouts=(workout.replace("\"", "'"))
def lambda_handler(event, context):
save_events(event)
return workouts
现在在api网关中的get方法的集成请求中,我应该在映射模板中添加什么来从用户获取json格式的数据,以及如何在查询中传递该用户值(例如:select exid,exercise_name,from exercise from exercise where bid =“用户提供的价值”)。 AM是AWS和后端开发的初学者。提前致谢
你可以传递查询参数。为此你必须在集成请求和映射模板中提及querystring参数将json / application添加到lambda代码中的map参数。 cur.execute(“select exid,exercise_name,exercise from sports = 3”)替换此行
cur.execute("select exid,exercise_name,image from exercise where bid = event['params']['querystring']['parameter_name']")
有关更多信息,请查看此pass parameter to lambda from api gateway