Are you confused about how to integrate file upload and download into gRPC-Gateway?
Try gRPC-Gateway-file, it is a plugin of the gRPC-Gateway.
It allows you to define upload and download api in the gRPC service proto file.
- For upload, you no longer have to manually add routes to the mux
- For download, It supports Resume Transfer Protocol.
- Best of all, you can implement them directly in gRPC service.
- Get it.
go get -u github.com/black-06/grpc-gateway-file
- Defining gRPC proto file.
The result of download api, and the request of upload api, must be "stream google.api.HttpBody"
import "google/api/annotations.proto";
import "google/api/httpbody.proto";
service Service {
rpc DownloadFile (XXX) returns (stream google.api.HttpBody) {
option (google.api.http) = { get: "/api/file/download" };
};
rpc UploadFile (stream google.api.HttpBody) returns (XXX) {
option (google.api.http) = { post: "/api/file/upload", body: "*" };
};
}
- Generate golang code as usual.
- Using gRPC-Gateway-file in gRPC-Gateway.
import gatewayfile "github.com/black-06/grpc-gateway-file"
mux := runtime.NewServeMux(
gatewayfile.WithFileIncomingHeaderMatcher(),
gatewayfile.WithFileForwardResponseOption(),
gatewayfile.WithHTTPBodyMarshaler(),
)
- Done, enjoy it
A more complete example is here
HTTPBodyMarshaler will change the Delimiter of all server-stream to empty.
Be careful if you have other server streams api.
More context see grpc-ecosystem/grpc-gateway#2557