将路径参数传递给 Google 的 Cloud Function 端点

pai*_*seq 5 google-cloud-endpoints aws-lambda google-cloud-functions openapi

我正在关注关于在我的 Cloud Function 前面设置Google Cloud 端点(而不是 AWS API Gateway)的Google教程。我正在触发我的 Cloud Function 以触发 AWS lambda 函数,并且我正在尝试从 OpenAPI规范定义的端点传递 a 。path parameter

路径参数是 URL 路径的可变部分。它们通常用于指向集合中的特定资源,例如由 ID 标识的用户。一个 URL 可以有多个路径参数,每个参数都用花括号 { } 表示。

paths:   /users/{id}:
    get:
      parameters:
        - in: path
          name: id   # Note the name is the same as in the path
          required: true
          schema: 
            type: integer
Run Code Online (Sandbox Code Playgroud)

GET /users/{id}

我的 openapi.yaml

swagger: '2.0'
info:
  title: Cloud Endpoints + GCF
  description: Sample API on Cloud Endpoints with a Google Cloud Functions backend
  version: 1.0.0
host: HOST
x-google-endpoints:
- name: "HOST"
  allowCors: "true
schemes:
  - https
produces:
  - application/json
paths:
  /function1/{pathParameters}:
    get:
      operationId: function1
      parameters:
      - in: path
        name: pathParameters
        required: true
        type: string
      x-google-backend:
        address: https://REGION-FUNCTIONS_PROJECT_ID.cloudfunctions.net/function1
      responses:
        '200':
          description: A successful response
          schema:
            type: string
Run Code Online (Sandbox Code Playgroud)

我在使用端点 URL 时遇到的错误https://REGION-FUNCTIONS_PROJECT_ID.cloudfunctions.net/function1/conversations是来自我的 AWS lambda 函数的 TypeError

StatusCode:200, FunctionError: "Unhandled", ExecutedVersion: "$LATEST". Payload: "errorType":"TypeError", errorMessage:"Cannot read property 'startsWith' of undefined..."
Run Code Online (Sandbox Code Playgroud)

网上说的

var path = event.pathParameters;
...
...
if (path.startsWith('conversations/'){...};
Run Code Online (Sandbox Code Playgroud)

我的pathvar 未定义。

我最初认为我的 Google 函数没有正确通过,pathParameters但是当我使用 triggering event 测试我的 Google 函数时{"pathParameters":"conversations"},我的 Lambda 成功返回了有效负载。

我的谷歌云功能:

let AWS = require('aws-sdk');

AWS.config.update({
  accessKeyId: 'key',
  secretAccessKey: 'secret',
  region: 'region'
})

let lambda = new AWS.Lambda();

exports.helloWorld = async(req,res) => {
  let params = {
     FunctionName:'lambdafunction',
     InvocationType: "RequestRespone",
     Payload: JSON.stringify(req.body)
  };

  res.status(200).send(await lambda.invoke(params, function(err,data){
      if(err){throw err}
      else{
         return data.Payload
      }
   }).promise());
}
Run Code Online (Sandbox Code Playgroud)

编辑 1:

看到这个 Google Group post,我尝试添加到我的openapi.yaml文件中path_translation: APPEND_PATH_TO_ADDRESS,但仍然没有运气。

...
paths:
  /{pathParameters}:
    get:
     ...
     x-google-backend:
       address: https://tomy.cloudfunctions.net/function-Name
       path_translation: APPEND_PATH_TO_ADDRESS
Run Code Online (Sandbox Code Playgroud)

@Arunmainthan Kamalanathan 在评论中提到,直接使用触发器事件在 AWS 和 Google Cloud 中进行测试{"pathParameters":"conversations"}并不等同于req.body从我的 Google 函数传递到 AWS lambda。我认为这是发生错误的地方——我没有正确地在有效负载中传递我的路径参数。

编辑2:

有这个 Stackoverflow帖子关于使用req.path. 当我console.log(req.path)得到/console.log(req.params)得到 时{'0': '' },由于某种原因,我的路径参数没有从 Cloud Endpoint URL 正确传递到我的 Google 函数。

Neb*_*tic 5

在 openapi.yaml 中指定多个路径/路由时,我遇到了同样的问题。这完全取决于您放置 x-google-backend 的位置(顶级与操作级)。这对 path_translation 的行为有影响。您也可以自己覆盖 path_translation,正如文档清楚描述的那样:

路径翻译:[ APPEND_PATH_TO_ADDRESS | 常量地址]

选修的。设置 ESP 在发出目标后端请求时使用的路径转换策略。

注意:当 x-google-backend 用于 OpenAPI 规范的顶层时,path_translation 默认为 APPEND_PATH_TO_ADDRESS,当 x-google-backend 用于 OpenAPI 规范的操作级别时,path_translation 默认为 CONSTANT_ADDRESS。有关路径转换的更多详细信息,请参阅了解路径转换部分。

这意味着,如果您希望将路径作为路径参数而不是后端的查询参数附加,则应遵循以下场景:

场景 1:您是否有一个云函数来x-google-backend.address处理 openapi 规范中的所有路径?放在x-google-backend顶层。

场景2:您是否有多个云功能对应不同的路径?置于x-google-backend操作级别并设置x-google-backend.path_translationAPPEND_PATH_TO_ADDRESS


Aru*_*han 0

当您的调用类型为 时RequestRespone,您可以直接从event,您可以直接从lambda 参数

查看Google函数的‘Payload’参数:

let params = {
   FunctionName:'lambdafunction',
   InvocationType: "RequestRespone",
   Payload: JSON.stringify({ name: 'Arun'})
};

res.status(200).send(await lambda.invoke(params)...)
Run Code Online (Sandbox Code Playgroud)

还有 Lambda 部分:

exports.handler = function(event, context) {
  context.succeed('Hello ' + event.name);
};
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助。