【问题标题】:How can I download a .zip file over a http to grpc transcoded endpoint?如何通过 http 将 .zip 文件下载到 grpc 转码端点?
【发布时间】:2021-03-13 12:10:36
【问题描述】:

我有如下定义的 grpc 端点。 grpc 端点返回一个 .zip 文件。它在 grpc 通道上工作正常,但我在通过 REST 端点下载它时遇到问题。

我使用 envoy 进行 http 转码。

我现在在 REST 端点上的问题是下载响应标头始终设置为 application/json 而不是 application/zip 以使 zip 下载正常工作。

知道如何指示特使在转码期间设置正确的标头,以便 REST 下载能够正常工作吗?

  // Download build
  //
  // Download build
  rpc DownloadBuild(DownloadBuildRequest) returns (stream DownloadResponse) {
    option (google.api.http) = {
      get : "/v4/projects/{projectId}/types/{buildType}/builds/{buildVersion}/.download"
      headers: {}
    };
    option (grpc.gateway.protoc_gen_swagger.options.openapiv2_operation) = {
      description: "Download build.";
      summary: "Download build.";
      tags: "Builds";
      produces: "application/zip";
      responses: {
        key: "200"
        value: {
          description: "Download build";
        }
      }
      responses: {
        key: "401"
        value: {
          description: "Request could not be authorized";
        }
      }
      responses: {
        key: "404"
        value: {
          description: "Build not found";
        }
      }
      responses: {
        key: "500"
        value: {
          description: "Internal server error";
        }
      }
    };
  }

【问题讨论】:

    标签: grpc envoyproxy


    【解决方案1】:

    好的,我找到了部分解决问题的方法。基本上我将不得不使用 google.api.HttpBody 作为响应并在其中设置内容类型。 https://github.com/googleapis/googleapis/blob/master/google/api/httpbody.proto

    https://www.envoyproxy.io/docs/envoy/latest/configuration/http/http_filters/grpc_json_transcoder_filter#sending-arbitrary-content

    我在设置下载的文件名和扩展头时仍有问题。

    【讨论】:

      【解决方案2】:

      要设置文件名头,我必须定义一个服务器拦截器,类似于这里提到的:

      How to pass data from grpc rpc call to server interceptor in java

        private class TrailerCall<ReqT, RespT> extends SimpleForwardingServerCall<ReqT, RespT> {
      
          public TrailerCall(final ServerCall<ReqT, RespT> delegate) {
              super(delegate);
          }
          
          @Override
          public void sendHeaders(Metadata headers) {
              headers.merge(RESPONSE_HEADERS_HOLDER_KEY.get());
              super.sendHeaders(headers);
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-06
        相关资源
        最近更新 更多