Copy a file from client to server via rest end point using golang

huangapple go评论155阅读模式
英文:

Copy a file from client to server via rest end point using golang

问题

我想通过服务器暴露的 REST 端点将文件从客户端复制到服务器。我在 Stack Overflow 上参考了各种问题和答案,但是没有得到清晰的图片。

我只想要一个在 Golang 中的示例客户端和服务器代码,用于从客户端复制文件并保存到服务器上。

提前感谢。

英文:

I want to copy a file from the client to the server through the rest end point exposed by the server. I referred the various questions and answers in stackoverflow but I could not get a clear picture of it.

I just want a sample client and server code in golang to copy the file from client and save it on the server.

Thanks in advance.

答案1

得分: 0

方向:服务器到客户端

好的,让我们从服务器端开始。请查看我在CSVStorageServer服务器中的WebLoad.go文件:(Github链接

在第17行,我定义了Web服务器的处理程序。该方法将根据需要构建一个zip文件并将其发送到浏览器。与您的问题相关的重要部分是第77到82行。在这里,我设置了客户端的标头,例如内容长度和类型。第82行将整个数据发送到客户端。它将字节从按需zip文件复制到传输线上。

在客户端上,您可以触发一个GET请求并存储结果。这是一个示例:https://golang.org/pkg/net/http/#example_Get

使用http.Get(...触发GET请求。使用ioutil.ReadAll(res.Body)从服务器读取所有字节并将其存储到一个变量中。然后,您可以将字节写入磁盘或在内存中处理它。

希望这个答案对您有帮助。

祝好,Thorsten

编辑#1:
关于REST端点,请参考服务器定义(Github链接)。第16行定义了此处理程序的REST端点。在这种情况下,它作为/load可用。您可以在此处使用任何类似REST的路径,例如/open/file/USERID/send等。

方向:客户端到服务器

要将文件从客户端复制到服务器端,需要类似的操作。在客户端上,需要一个POST请求,格式为multipart/form-data。这是一个很好的例子:博客文章链接。此示例还考虑了服务器部分。相关的客户端部分是函数func postFile(filename string, targetUrl string) error { ... }

对于服务器部分,这是一个示例:Github链接。此示例从客户端接收文件并将其写入MongoDB数据库。相关部分如下:

第39行从客户端读取文件:file, fileHeader, fileError := request.FormFile("file")。结果是对上传文件的句柄。

第60行将源(浏览器或Go客户端)中的所有字节复制到目标(这里是MongoDB):_, errCopy := io.Copy(newFile, file)

编辑#2:

这是一个完整的工作示例:https://github.com/SommerEngineering/Example010,其中客户端和服务器在同一个程序中。将其拆分为两个程序应该很容易。

英文:

Direction: Server to Client

So -- both sides are in Go? Okay, let's start with the server side. See my WebLoad.go file from my CSVStorageServer server: (Link to Github)

At line 17, I define the handler for the web server. This method will build a zip file on-demand and send it to the browser. The important part regarding to your question are line 77 up to 82. Here, I set the headers for the client, e.g. content length and type. Line 82 sends the whole data to the client side. It copies the bytes from the on-demand zip file to the wire.

On the client side, you trigger e.g. a GET request and store the result. Here an example: https://golang.org/pkg/net/http/#example_Get

With http.Get(... you trigger the GET request. With ioutil.ReadAll(res.Body) you read all bytes from the server and store it to a variable. Afterwards, you could write the bytes to the disk or process it in-memory.

I hope, this answer helps you.

Best regards, Thorsten

Edit #1:
Regarding the REST end-point, cf. the server definition (link to Github). Line 16 defines the REST end-point for this handler. In this case, it gets available as /load. You could use any REST-like path here, e.g. /open/file/USERID/send, etc.

Direction: Client to Server

In order to copy a file from client to server side, similar operations are necessary. On the client side, a POST request is necessary as multipart/form-data. Here is a good example for this: Link to a blog post. This example considers also the server part. The relevant client part is the function func postFile(filename string, targetUrl string) error { ... }.

For the server part, here an own example: Link to Github. This example receives an file from the client and writes it to a MongoDB database. The relevant parts are:

Line 39 read the file from the client: file, fileHeader, fileError := request.FormFile("file") The result is a handle to this uploaded file.

Line 60 copies all bytes from the source (browser or Go client) into a destination (here, the MongoDB): _, errCopy := io.Copy(newFile, file).

Edit #2:

Here is a full working example: https://github.com/SommerEngineering/Example010 where client and server are in the same program. It should be easy to split it into two programs.

huangapple
  • 本文由 发表于 2017年5月25日 14:09:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/44173461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定