Go language Implementation FASTDFS Distributed storage System WEBAPI Gateway

Source: Internet
Author: User


Work needs, the first time to use Go to combat projects.

Requirements: Implement a WEBAPI transit gateway with Golang, and upload some resource files to Fastdfs Distributed File Storage System via HTTP protocol.

First, Fastdfs and Golang docking code


Source code can clone down to see, go syntax is very simple

Basic use: (There is a test case code in CLIENT_TEST.GO)

Package Mainimport ("FMT"    "Io/ioutil"    "github.com/weilaihui/fdfs_client") Func main () {ff, _:= Ioutil. ReadFile ("1.jpg") fmt. Println ("image len:", Len (FF))/*hosts: = []string{""} port: = 22122 Minconns: = ten Maxconns: = connpool,_: = fdfs_client. Newconnectionpool (Hosts, Port, Minconns, Maxconns)*/Path:="client.conf"FDS, Error:=fdfs_client. Newfdfsclient (PATH)ifFDS = =Nil {fmt. Println ("Conn Error:%s", error)varTeststringFMT. SCANLN (&test)return} uploadresponse, err:= FDs. Uploadbybuffer (FF,"jpg")    ifUploadresponse = =Nil {fmt. Println ("Upload error:%s", Err)varTeststringFMT. SCANLN (&test)return} fmt. Println ("Group Name:", Uploadresponse.groupname) fmt. Println ("Remote file ID:", Uploadresponse.remotefileid)varTeststringFMT. SCANLN (&test)}

Second, the simple WebAPI gateway

Beego frame Go Circle is very famous, domestic university works, considering this project is small, temporarily not used up.

The go implementation of an API gateway is also fairly straightforward:

Package Mainimport ("FMT"    "net/http") Func Main () {http. Handlefunc ("/", func (rw http. Responsewriter, req *http. Request) {rw. Write ([]byte("Hello Go Web") }) http. Handlefunc ("/upload", upload) http. Listenandserve ("localhost:8888", nil) fmt. Println ("End.")}func upload (rw http. Responsewriter, req*http. Request) {fmt. Println ("Header", req. Header) fmt. Println ("Content-type", req. Header.get ("Content-type") FMT. Println ("Body", req. Body)
Get the full contents of body
Len: = req. ContentLength Body: = Make ([]byte, Len) req. Body.read (body) RW. Write ([]byte ("Response Body ...."))

PS: The above code is only the use of their own notes, because the first go not familiar, only for learning.

File Upload Transit, if it is a larger file, then the use of the data fragmented transmission method.

Go language Implementation FASTDFS Distributed storage System WEBAPI Gateway

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.