当前位置:网站首页>Grpc: how to implement the restful API for file uploading?
Grpc: how to implement the restful API for file uploading?
2022-06-24 03:06:00 【Trespass 】
Introduce
This article will show you how to gRPC File upload in microservice Restful API?
Why do you need such an article ?
gRPC We can go through Streaming To transfer large files to each other , But by grpc-gateway on gRPC We can't achieve . therefore , Need to bypass gRPC Directly in grpc-gateway Add API.
We will use rk-boot To start up gRPC service .
Please visit the following address for a complete tutorial :https://rkdev.info/cnhttps://rkdocs.netlify.app/cn ( spare )
install
go get github.com/rookie-ninja/rk-boot go get github.com/rookie-ninja/rk-grpc
Quick start
rk-boot Integrated by default grpc-gateway, And will start by default .
1. establish boot.yaml
---
grpc:
- name: greeter # Name of grpc entry
port: 8080 # Port of grpc entry
enabled: true # Enable grpc entry2. establish main.go
Be careful ,grpcEntry.GwMux.HandlePath() Be sure to write boot.Bootstrap() after , Otherwise Panic.
package main
import (
"context"
"fmt"
"github.com/rookie-ninja/rk-boot"
"github.com/rookie-ninja/rk-grpc/boot"
"net/http"
)
// Application entrance.
func main() {
// Create a new boot instance.
boot := rkboot.NewBoot()
// Bootstrap
boot.Bootstrap(context.Background())
// Get grpc entry with name
grpcEntry := boot.GetEntry("greeter").(*rkgrpc.GrpcEntry)
// Attachment upload from http/s handled manually
grpcEntry.GwMux.HandlePath("POST", "/v1/files", handleBinaryFileUpload)
// Wait for shutdown sig
boot.WaitForShutdownSig(context.Background())
}
func handleBinaryFileUpload(w http.ResponseWriter, req *http.Request, params map[string]string) {
err := req.ParseForm()
if err != nil {
http.Error(w, fmt.Sprintf("failed to parse form: %s", err.Error()), http.StatusBadRequest)
return
}
f, header, err := req.FormFile("attachment")
if err != nil {
http.Error(w, fmt.Sprintf("failed to get file 'attachment': %s", err.Error()), http.StatusBadRequest)
return
}
defer f.Close()
fmt.Println(header)
//
// Now do something with the io.Reader in `f`, i.e. read it into a buffer or stream it to a gRPC client side stream.
// Also `header` will contain the filename, size etc of the original file.
//
}3. verification
$ curl -X POST -F "[email protected]" localhost:8080/v1/files
边栏推荐
- Disk and file system (Simplified)
- 2022-2028 global genome editing mutation detection kit industry survey and trend analysis report
- How to build a speech synthesis server
- [51nod] 2653 section XOR
- Why enterprises need fortress machines
- Kibana report generation failed due to custom template
- 2022-2028 global aircraft front wheel steering system industry research and trend analysis report
- Is your database "cloud native"?
- IOS development - multithreading - thread safety (3)
- Storage crash MySQL database recovery case
猜你喜欢
![[51nod] 3216 Awards](/img/94/fdb32434d1343040d711c76568b281.jpg)
[51nod] 3216 Awards
![[51nod] 2106 an odd number times](/img/af/59b441420aa4f12fd50f5062a83fae.jpg)
[51nod] 2106 an odd number times

2022-2028 global aircraft audio control panel system industry research and trend analysis report

2022-2028 global cell-based seafood industry research and trend analysis report
![[51nod] 3395 n-bit gray code](/img/b5/2c072a11601de82cb92ade94672ecd.jpg)
[51nod] 3395 n-bit gray code

Simple and beautiful weather code

2022-2028 global marine clutch industry research and trend analysis report

2022-2028 global cancer biopsy instrument and kit industry research and trend analysis report

2022-2028 global marine wet exhaust hose industry research and trend analysis report

2022-2028 global genome editing mutation detection kit industry survey and trend analysis report
随机推荐
What is cloud desktop and how to connect to the server? What does the mainstream architecture of cloud desktop include?
[51nod] 3047 displacement operation
Hook principle
RI Geng series: tricks of using function pointers
Tencent cloud CVM starts IPv6
Process kill problem
Why enterprises need fortress machines
October 27, 2021: curriculum. You must take numcourses this semester
What is the difference between cloud desktop cloud terminal and server? What are the advantages of cloud desktop?
How to build a private cloud desktop with a server? How to use cloud desktop?
Windowsvpn client is coveted by vulnerabilities, 53% of companies face supply chain attacks | global network security hotspot
The most comprehensive arrangement of safe operation solutions from various manufacturers
What are the benefits of rack servers for cloud desktops? What can cloud desktop do?
How to check the progress of trademark registration? Where can I find it?
2022-2028 global aircraft wireless intercom system industry research and trend analysis report
Implementing an ORM framework against SQL injection with builder mode
2022-2028 global aircraft audio control panel system industry research and trend analysis report
Cloud call: one line of code is directly connected to wechat open interface capability
Tencent cloud CIF engineering efficiency summit ends perfectly
2022-2028 global indoor pressure monitor and environmental monitor industry research and trend analysis report