这尚不受支持,但是,有一个临时解决方法可以使其正常工作。您应该添加options到openapi.yaml. 此外,get和options操作都应该指向同一个云函数,因为该options请求随后充当了云函数的预热请求。就延迟而言,这是最有效的设置。这是一个简化的示例:
paths:
  /helloworld:
    get:
      operationId: getHelloWorld
      x-google-backend:
        address: $CLOUD_FUNCTION_ADDRESS
      responses:
        '200':
          description: A successful response
    options:
      operationId: corsHelloWorld
      x-google-backend:
        address: $CLOUD_FUNCTION_ADDRESS
      responses:
        '200':
          description: A successful response
然后,在您的云函数后端,您还必须处理预检请求 ( source )。Google 文档还提供了一个带有 authentication的示例,其中包含一些额外的标头。这是一个没有身份验证的示例:
def cors_enabled_function(request):
    # For more information about CORS and CORS preflight requests, see
    # https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
    # for more information.
    # Set CORS headers for the preflight request
    if request.method == 'OPTIONS':
        # Allows GET requests from any origin with the Content-Type
        # header and caches preflight response for an 3600s
        headers = {
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Methods': 'GET',
            'Access-Control-Allow-Headers': 'Content-Type',
            'Access-Control-Max-Age': '3600'
        }
        return ('', 204, headers)
    # Set CORS headers for the main request
    headers = {
        'Access-Control-Allow-Origin': '*'
    }
    return ('Hello World!', 200, headers)
注意:API 网关没有以适当的方式管理预检请求的缺点会导致两次运行云功能的惩罚。但是您的第二个请求应该总是非常快,因为第一个请求充当预热请求。