Swagger / OpenAPI 规范的特点是文件上传被 Google 个端点拒绝

Swagger / OpenAPI spec featuring file upload rejected by Google Endpoints

我的目标是设置一个简单的 API 通过 Google 端点上传文件。

这是我的简化 OpenAPI 规范,根据 Swagger 验证有效:

swagger: "2.0"
info:
  title: "JSON Ingester"
  description: "Receive JSON files, transform and load them."
  version: "1.0.0"

host: "project-id.appspot.com"
schemes:
  - "https"

paths:
  /uploadFile:
    post:
      operationId: uploadFile
      consumes:
        - multipart/form-data
      parameters:
        - 
          in: formData
          name: upfile
          type: file
          description: file
      responses:
        200:
          description: "File uploaded."
        400:
          description: "Error during file upload."

我总是以这条晦涩的错误消息结束:

user@cloudshell:~/google-cloud-json-ingester (project-id)$ gcloud endpoints services deploy ./openapi.yaml
ERROR: (gcloud.endpoints.services.deploy) INVALID_ARGUMENT: Cannot convert to service config.
'location: "openapi.yaml: Operation \'post\' in path \'/uploadFile\'"
message: "Operation does not require an API key; callers may invoke the method without specifying an associated API-consuming project. To enable API key all the SecurityRequirement Objects (https://gi
thub.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#security-requirement-object) inside security definition must reference at least one SecurityDefinition of type : \'apiKey\'."
 location: "unknown location"
kind: ERROR
message: "http: repeated message field \'google.protobuf.Struct.fields\' referred to by message \'UploadFileRequest\' cannot be mapped as an HTTP parameter."
 location: "unknown location"
kind: ERROR
message: "http: cyclic message field \'google.protobuf.Struct.FieldsEntry.value\' referred to by message \'UploadFileRequest\' in method \'method 1.project_id_appspot_com.UploadFile\' cannot
 be mapped as an HTTP parameter."
 location: "unknown location"
kind: ERROR
message: "http: cyclic message field \'google.protobuf.ListValue.values\' referred to by message \'UploadFileRequest\' in method \'method 1.project_id_appspot_com.UploadFile\' cannot be mapp
ed as an HTTP parameter."
'

我 运行 不知道可能是什么原因。

有什么建议吗?

看起来它至少需要一些身份验证:https://cloud.google.com/endpoints/docs/openapi/authentication-method

我还认为 Cloud Endpoints 不支持 type: file,所以你必须使用 type: string 并使用等价物to curl -X POST -F "file_upload=@file.txt" http://myservice.com/endpoint 上传.

我 运行 今天解决了这个问题并创建了一个重现。我认为您的问题可能出在以下顺序:

in: formData
name: upfile

如果您仍然遇到此问题,请尝试颠倒顺序:

name: upfile
in: formData