将路径参数传递给Google的Cloud Endpoint功能 [英] Passing a path parameter to Google's Endpoint for Cloud Function

查看:161
本文介绍了将路径参数传递给Google的Cloud Endpoint功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遵循Google的教程我的Cloud Function前面的 Google Cloud端点(不是AWS API Gateway).我正在触发我的Cloud Function来触发一个AWS Lambda函数,并且我试图从OpenAPI

I am following Google's tutorial on setting up Google Cloud endpoint (not AWS API Gateway) in front of my Cloud Function. I am triggering my Cloud Function to trigger an AWS lambda function, AND I am trying to pass a path parameter from my Endpoint as defined by OpenAPI spec.

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

Path parameters are variable parts of a URL path. They are typically used to point to a specific resource within a collection, such as a user identified by ID. A URL can have several path parameters, each denoted with curly braces { }.

paths:   /users/{id}:
    get:
      parameters:
        - in: path
          name: id   # Note the name is the same as in the path
          required: true
          schema: 
            type: integer

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

当我使用Endpoint URL https://REGION-FUNCTIONS_PROJECT_ID.cloudfunctions.net/function1/conversations时遇到的错误是我的AWS lambda函数中的TypeError

The error I get when I use Endpoint URL https://REGION-FUNCTIONS_PROJECT_ID.cloudfunctions.net/function1/conversations is a TypeError from my AWS lambda function

StatusCode:200, FunctionError: "Unhandled", ExecutedVersion: "$LATEST". Payload: "errorType":"TypeError", errorMessage:"Cannot read property 'startsWith' of undefined..."

是说在线

var path = event.pathParameters;
...
...
if (path.startsWith('conversations/'){...};

我的path var未定义.

最初,我认为我的Google Function无法正确传递pathParameters,但是当我使用触发事件{"pathParameters":"conversations"}测试我的Google函数时,我的Lambda成功返回了有效载荷.

I initially thought my Google Function was not correctly passing pathParameters but when I tested my Google function using triggering event {"pathParameters":"conversations"}, my Lambda returns the payload successfully.

我的Google Cloud功能:

My Google Cloud Function:

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());
}

看到此Google网上论坛发布,我尝试将文件添加到openapi.yaml文件path_translation: APPEND_PATH_TO_ADDRESS,但仍然没有运气.

Seeing this Google Group post, I tried adding to my openapi.yaml file path_translation: APPEND_PATH_TO_ADDRESS, yet still I'm having no luck.

...
paths:
  /{pathParameters}:
    get:
     ...
     x-google-backend:
       address: https://tomy.cloudfunctions.net/function-Name
       path_translation: APPEND_PATH_TO_ADDRESS

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

@Arunmainthan Kamalanathan mentioned in the comments that testing in AWS and Google Cloud directly with trigger event {"pathParameters":"conversations"} is not equivalent to passing req.body from my Google function to AWS lambda. I think this is where my error is occurring -- I'm not correctly passing my path parameter in the payload.

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

There is this Stackoverflow post concerning passing route parameters to Cloud Functions using req.path. When I console.log(req.path) I get / and console.log(req.params) I get {'0': '' }, so for some reason my path parameter is not getting passed correctly from Cloud Endpoint URL to my Google function.

推荐答案

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

I was running into the same issue when specifying multiple paths/routes within my openapi.yaml. It all depends on where you place the x-google-backend (top-level versus operation-level). This has implications on the behaviour of the path_translation. You could also overwrite path_translation yourself, as the documentation clearly describes:

path_translation:[APPEND_PATH_TO_ADDRESS | CONSTANT_ADDRESS]

path_translation: [ APPEND_PATH_TO_ADDRESS | CONSTANT_ADDRESS ]

可选.设置ESP在制作时使用的路径转换策略 目标后端请求.

Optional. Sets the path translation strategy used by ESP when making target backend requests.

注意:在OpenAPI的顶层使用x-google-backend时 规范,path_translation默认为APPEND_PATH_TO_ADDRESS, 并且在操作级别使用x-google-backend 在OpenAPI规范中,path_translation默认为CONSTANT_ADDRESS. 有关路径翻译的更多详细信息,请参见了解 路径翻译部分.

Note: When x-google-backend is used at the top level of the OpenAPI specification, path_translation defaults to APPEND_PATH_TO_ADDRESS, and when x-google-backend is used at the operation level of the OpenAPI specification, path_translation defaults to CONSTANT_ADDRESS. For more details on path translation, please see the Understanding path translation section.

示例1:您是否具有一个具有多个路径的云功能?将x-google-backend放在顶层.

Example 1: Do you have one cloud function with multiple paths? Put x-google-backend at the top-level.

示例2:您是否具有对应于不同路径的多个云功能?将x-google-backend放在操作级别,以便您可以配置不同的目标.

Example 2: Do you have multiple cloud functions corresponding to different paths? Put x-google-backend at the operation-level, so you can configure different targets.

这篇关于将路径参数传递给Google的Cloud Endpoint功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆