简体   繁体   English

AWS Lambda、Python 无服务器处理程序与 url 模式/参数

[英]AWS Lambda, Python serverless handler with url pattern / parameters

I am trying to set up a lambda function using Python and serverless.我正在尝试使用 Python 和无服务器设置 lambda function。 I was able to perform simple POST/GET requests and have them to proxy to my lambda function just fine.我能够执行简单的 POST/GET 请求并让它们代理到我的 lambda function 就好了。

Now I am struggling getting something like this to work with serverless:现在我正在努力让这样的东西与无服务器一起工作:

GET /my_function/{foo}

In a way that I could get it by:我可以通过以下方式获得它:

http://....aws.../my_function/bar

This is what I'm trying:这就是我正在尝试的:

serverless.yml无服务器.yml

functions:
  my_function:
    handler: handler.my_function
    events:
      - http: GET /my_function/{foo}

It deployed to aws, but I am not able to get the value.它部署到 aws,但我无法获得价值。 It says there is no key for the event:它说该事件没有密钥:

handler.py:处理程序.py:

def sitemap_entries(event, context):
  s = MyNiceClass(event['foo'])
  ...

What am I missing?我错过了什么?

PS: It works on local invoke when I send --data '{"foo": "bar"}' PS:当我发送--data '{"foo": "bar"}'时,它适用于本地调用

Thank you谢谢

If you use the Lambda Proxy Integration (which is the default setting so unless you've changed it you'll be using it) then the path parameters are inside event in an attribute called pathParameters , so to access them use:如果您使用Lambda 代理集成(这是默认设置,除非您更改它,否则您将使用它)然后路径参数在名为pathParameters的属性中的event内部,因此要访问它们,请使用:

event.get('pathParameters', {}).get('foo', None)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS Lambda 和 Python 中的 Open CV 的无服务器问题 - Serverless issue with AWS Lambda and Open CV in Python AWS Lambda函数处理程序python - aws lambda function handler python 带有类的 AWS Lambda Python 处理程序 - AWS Lambda Python Handler with class 如何在 S3 上存储大型 Python 依赖项(适用于无服务器的 AWS Lambda) - How to Store Large Python Dependencies on S3 (for AWS Lambda with Serverless) 无服务器 aws python lambda 无法从父目录导入模块 - serverless aws python lambda unable to import module from parent directory 使用无服务器函数(例如 AWS Lambda)/Python“分叉并加入” - "Fork and Join" with serverless functions (e.g. AWS Lambda) / Python 使用无服务器框架将 Python package 部署到 AWS lambda 时出错 - Error deploying Python package to AWS lambda using Serverless framework Python 中的 AWS Lambda:在 Lambda 函数处理程序中导入父包/目录 - AWS Lambda in Python: Import parent package/directory in Lambda function handler 在 Python 中为 AWS Lambda 实施聚合器模式 - Implementing aggregator pattern for AWS Lambda in Python 如何配置serverless.yml或AWS API网关或AWS Lambda处理程序以从POST请求读取request.header - How to configure serverless.yml or AWS API gateway or AWS lambda handler to read request.headers from POST request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM