我试图为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"
运行gcloud(将实际变量替换为占位符)
gcloud api-gateway api-configs create uploadconfig --api=[API] --openapi-spec=openapi.yaml --project=[MYPROJECT] --backend-auth-service-account=[ACCOUNT]
这会导致错误消息:
错误:(gcloud.api-gateway.api-configs.create) INVALID_ARGUMENT:不能转换为服务配置。位置:"未知位置";:错误google.protobuf.Struct: "http:重复消息字段。消息'UploadFileRequest'引用的字段'不能映射为HTTP参数。"地点:"未知地点";:错误http:循环消息字段google.protobuf.Struct.FieldsEntry。方法1.xxxxxxx中消息"UploadFileRequest"引用的值。不能映射为HTTP参数。">
我用不同的api配置和后端验证了gcloud命令。
配置本身似乎很好,即它与Swagger编辑器验证,但gcloud不会接受它。如何定义通过API网关上传文件?
由于每个文件上传端点不接受type: file
作为文件上传参数,应使用type: string
代替。我试着用改变的参数配置,它验证了与Swagger编辑器的结果在这里。
使用SwaggerHub和OpenAPI 2规范,这就是我如何让这个文件上传工作。如果有任何问题请告诉我。这是用YAML编写的,而不是JSON规范。它需要一些阅读和一些实验,但这里是Gcloud文档:https://cloud.google.com/storage/docs/uploading-objects和SwaggerHub信息:https://swagger.io/docs/specification/2-0/file-upload/
/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 formData request.