简体   繁体   English

GCP API 网关中的文件上传

[英]FIle upload in GCP API Gateway

I'm trying to create the following api config for GCP API Gateway (ommitted real backend URL):我正在尝试为GCP API 网关创建以下api config (省略了真实的后端 URL):

swagger: '2.0'
info:
  title: upload
  description: upload
  version: 1.0.0
schemes:
  - https
produces:
  - application/json
security:
  - api_key: [ ]

paths:
  /upload:
    post:
      summary: uploads a file.
      consumes:
        - multipart/form-data
      operationId: uploadFile
      parameters:
        - in: formData
          name: file
          description: The file to upload.
          required: true
          type: file
      responses:
        '200':
          description: upload successful
      x-google-backend:
        address: https://XXXXX.XXXXX
        path_translation: APPEND_PATH_TO_ADDRESS    

securityDefinitions:
  api_key:
    type: "apiKey"
    name: "key"
    in: "query" 

Running gcloud (replaced real variables with placeholders)运行 gcloud(用占位符替换实变量)

gcloud api-gateway api-configs create uploadconfig --api=[API] --openapi-spec=openapi.yaml --project=[MYPROJECT] --backend-auth-service-account=[ACCOUNT]

This results in this error message :这会导致此错误消息

ERROR: (gcloud.api-gateway.api-configs.create) INVALID_ARGUMENT: Cannot convert to service config.错误:(gcloud.api-gateway.api-configs.create)INVALID_ARGUMENT:无法转换为服务配置。 '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." '位置:“未知位置”种类:错误消息:“http:消息'UploadFileRequest'引用的重复消息字段'google.protobuf.Struct.fields'无法映射为HTTP参数。” location: "unknown location" kind: ERROR message: "http: cyclic message field 'google.protobuf.Struct.FieldsEntry.value' referred to by message 'UploadFileRequest' in method 'method 1.xxxxxxx.UploadFile' cannot be mapped as an HTTP parameter."位置:“未知位置”种类:错误消息:“http:循环消息字段 'google.protobuf.Struct.FieldsEntry.value' 方法'方法 1.xxxxxxx.UploadFile' 中的消息'UploadFileRequest'引用不能映射为HTTP 参数。”

I verified the gcloud command with a different api configurations and backends.我使用不同的 api 配置和后端验证了 gcloud 命令。

The config itself seems fine, ie it validates with Swagger editor, still gcloud won't accept it.配置本身似乎很好,即它使用 Swagger 编辑器验证,但 gcloud 仍然不会接受它。 How do I define a file upload via API Gateway?如何通过 API 网关定义文件上传?

根据文件上传端点不接受文件上传参数的type: file ,应该使用type: string 。我尝试使用更改的参数进行配置,并使用 Swagger 编辑器验证结果在此处

With SwaggerHub and OpenAPI 2 spec, this is how I got this file upload to work.使用 SwaggerHub 和 OpenAPI 2 规范,这就是我让这个文件上传工作的方式。 Let me know if you have any questions.如果您有任何问题,请告诉我。 This is written in YAML as opposed to JSON spec.这是用 YAML 而不是 JSON 规范编写的。 It took some reading and some experimentation, but here's the Gcloud documentation: https://cloud.google.com/storage/docs/uploading-objects And the SwaggerHub info as well: https://swagger.io/docs/specification/2-0/file-upload/它需要一些阅读和一些实验,但这里是 Gcloud 文档: https://cloud.google.com/storage/docs/uploading-objects以及 SwaggerHub 信息: https://swagger.io/docs/specification/ 2-0/文件上传/

 /upload/storage/v1/b/{bucket}/o?uploadType=media&name={objectName}: post: summary: Upload an object directly to a bucket description: Upload an object directly to a bucket consumes: - multipart/form-data produces: - application/json parameters: - in: header name: Authorization type: string required: true - in: path name: bucket required: true type: string description: The name of the bucket to upload to. - in: path name: objectName required: true type: string description: The name the object will receive in the bucket. - in: header name: Content-Type type: string required: true description: The content type of the upload. Ex. text/plain - in: formData name: fileToUpload type: file description: The file to upload. responses: 200: description: OK 204: description: Success - No Content 400: description: Bad Request 401: description: Insufficient Privileges 404: description: Not Found

It DOES accept type: file in the formData request.它接受类型:formData 请求中的文件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM