在GCP API网关中上传文件

发布于 2025-02-13 13:12:40 字数 1476 浏览 2 评论 0 原文

后端URL)创建以下 API配置

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" 

我正在尝试为 GCP API Gateway (OMMUTTENT的真实

网关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:无法转换为服务配置。 '位置:“未知位置” 类型:错误 消息:“ http:重复消息字段'google.protobuf.struct.fields'通过消息'uploadfilerequest'所引用的'无法映射为http参数。” 位置:“未知位置” 类型:错误 消息:“ http:循环消息字段'google.protobuf.struct.fieldsentry.value'通过消息'uploadfilerequest'方法'方法'方法'方法1.xxxxxxxx.uploadfile'无法映射为HTTP参数。

我使用不同的API配置和后端验证了GCLOUD命令。

配置本身看起来不错,即使用Swagger Editor进行验证,GCLOUD仍然不接受。 如何通过API网关定义上传文件?

I'm trying to create the following api config for GCP API Gateway (ommitted real backend 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 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.
'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.xxxxxxx.UploadFile' cannot be mapped as an HTTP parameter."

I verified the gcloud command with a different api configurations and backends.

The config itself seems fine, i.e. it validates with Swagger editor, still gcloud won't accept it.
How do I define a file upload via API Gateway?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光磨忆 2025-02-20 13:12:40

As per file upload Endpoints does not accept the type: file for file upload parameters, type: string should be used instead.And I tried config with changed parameters and it validates with Swagger editor results are here.

若水微香 2025-02-20 13:12:40

使用SwaggerHub和OpenAPI 2规格,这就是我将此文件上传工作的方式。让我知道您是否有任何疑问。这是用YAML而不是JSON规格编写的。它花了一些阅读和一些实验,但这是GCLOUD文档: htttps:htttps:// cloud。 google.com/storage/docs/uploading-objects
以及SwaggerHub信息:

  /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

它确实接受类型:formdata请求中的文件。

With SwaggerHub and OpenAPI 2 spec, this is how I got this file upload to work. Let me know if you have any questions. This is written in YAML as opposed to JSON spec. 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/

  /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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文