Gin implements upload / download services

In this article, let's take a look at the file upload service in {Gin}.

Create project

mkdir ginupload && cd ginupload
go mod init ginupload
go get -u github.com/gin-gonic/gin
touch main.go

We create a "ginupload" directory to store code, and use "go mod" to manage dependencies.

Then, add "gin" as a third-party dependency to the project through "go get", and then create "main" Go is used to write code.

Single file upload

func main() {
    router := gin.Default()
    router.POST("/upload", func(c *gin.Context) {
        file, _ := c.FormFile("file")
        log.Println(file.Filename)

        c.SaveUploadedFile(file, "./" + file.Filename)

        c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename))
    })
    router.Run(":8080")
}

You can see that the code to implement the file upload service with {Gin} is very simple. Let's use curl to test

curl -X POST http://localhost:8080/upload \
  -F "file=@/tmp/upload.go" \
  -H "Content-Type: multipart/form-data"

After execution, you can see} upload in the current directory Go} This file.

Multi file upload

In addition to providing single file upload, Gin also provides multi file upload, which is very convenient for the demand of multiple files.

func main() {
    router := gin.Default()
    router.POST("/upload", func(c *gin.Context) {
        form, _ := c.MultipartForm()
        files := form.File["files"]

        for _, file := range files {
            log.Println(file.Filename)

            c.SaveUploadedFile(file, "./" + file.Filename)
        }
        c.String(http.StatusOK, fmt.Sprintf("%d files uploaded!", len(files)))
    })
    router.Run(":8080")
}

There is little difference between multi file upload and single file. A few lines of code realize the function of file upload.

Also use curl to test the function of file upload

curl -X POST http://localhost:8080/upload \
  -F "files=@/tmp/test1.zip" \
  -F "files=@/tmp/test2.zip" \
  -H "Content-Type: multipart/form-data"

If there is no problem, there will be {test1. 0 in the current directory Zip and test2 Zip these two files.

Limit file size

In addition to the above file upload function, Gin also provides the function of limiting the file upload size, which will be of great use in practical projects.

router := gin.Default()
router.MaxMultipartMemory = 8 << 20

Use MaxMultipartMemory to configure the size limit of file upload. The default is 32M.

File download service

Although , go , provides file service by default, the default file service will expose all files in the whole folder. It is not convenient if we want to do more control, while , Gin , provides file download service

func main() {
    router := gin.Default()

    router.GET("/local/file", func(c *gin.Context) {
        c.File("local/file.go")
    })
}

The file service provided by Gin , allows us to easily control, even dynamic files, with more customized playing methods than the default.

summary

The file upload and download service provided by Gin , is very convenient and makes up for the shortcomings of the default file service in some scenarios. If the default file service can't meet your needs, you can try , Gin.

reference resources

  • gin-gonic/gin: Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance – up to 40 times faster. If you need smashing performance, get yourself some Gin.[3]

reference material

[1]

gin-gonic/gin: Gin is a HTTP web framework written in Go (Golang).: https://github.com/gin-gonic/gin

Keywords: network Network Protocol p2p

Added by nemesis1931 on Sun, 23 Jan 2022 22:53:22 +0200