使用Gin在Golang中提供文件端点。

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

serve file endpoint in golang with gin

问题

我想要为动态加载的用户文件提供服务(假设是简单的文件存储),但在发送实际文件之前,我想要添加一些检查(比如用户是否被禁止)。我知道在gin中有一种方法可以提供整个目录的服务,也有一种方法可以将文件作为附件发送(https://stackoverflow.com/questions/31638447/how-to-server-a-file-from-a-handler-in-golang),但是否有一种方法可以将文件直接作为实际图像发送回浏览器(而不是下载附件提示),就像这个纯Go语言示例中一样(https://golangbyexample.com/image-http-response-golang/):

  1. package main
  2. import (
  3. "io/ioutil"
  4. "net/http"
  5. )
  6. func main() {
  7. handler := http.HandlerFunc(handleRequest)
  8. http.Handle("/photo", handler)
  9. http.ListenAndServe(":8080", nil)
  10. }
  11. func handleRequest(w http.ResponseWriter, r *http.Request) {
  12. fileBytes, err := ioutil.ReadFile("test.png")
  13. if err != nil {
  14. panic(err)
  15. }
  16. w.WriteHeader(http.StatusOK)
  17. w.Header().Set("Content-Type", "application/octet-stream")
  18. w.Write(fileBytes)
  19. return
  20. }
英文:

I would like to serve dynamically loaded user files (let's assume simple file storage) but I want to add some checks before sending actual file (like was the user banned). I know there is a way to serve whole directory in gin, there is also way to send file as attachment (https://stackoverflow.com/questions/31638447/how-to-server-a-file-from-a-handler-in-golang) but is there a way to simply send back file as actual image to show in browser (without download attachment prompt) as in this pure golang example (https://golangbyexample.com/image-http-response-golang/):

  1. package main
  2. import (
  3. "io/ioutil"
  4. "net/http"
  5. )
  6. func main() {
  7. handler := http.HandlerFunc(handleRequest)
  8. http.Handle("/photo", handler)
  9. http.ListenAndServe(":8080", nil)
  10. }
  11. func handleRequest(w http.ResponseWriter, r *http.Request) {
  12. fileBytes, err := ioutil.ReadFile("test.png")
  13. if err != nil {
  14. panic(err)
  15. }
  16. w.WriteHeader(http.StatusOK)
  17. w.Header().Set("Content-Type", "application/octet-stream")
  18. w.Write(fileBytes)
  19. return
  20. }

答案1

得分: 2

是的,使用go-gin是可以实现的。你可以使用gin上下文的Data方法。

  1. import (
  2. "io/ioutil"
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. )
  6. func main() {
  7. r := gin.Default()
  8. r.GET("/photo", photoHandler)
  9. if err := r.Run(":8080"); err != nil {
  10. panic(err)
  11. }
  12. }
  13. func photoHandler(c *gin.Context) {
  14. // 在这里进行其他检查
  15. // 读取文件
  16. fileBytes, err := ioutil.ReadFile("test.png")
  17. if err != nil {
  18. panic(err)
  19. }
  20. c.Data(http.StatusOK, "image/png", fileBytes)
  21. }

你可以在这里查看gin提供的示例代码链接

英文:

Yes, it is possible with go-gin. You can use Data method of gin context.

> Data writes some data into the body stream and updates the HTTP code.

  1. import (
  2. "io/ioutil"
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. )
  6. func main() {
  7. r := gin.Default()
  8. r.GET("/photo", photoHandler)
  9. if err := r.Run(":8080"); err != nil {
  10. panic(err)
  11. }
  12. }
  13. func photoHandler(c *gin.Context) {
  14. // do the other checks here
  15. // read the file
  16. fileBytes, err := ioutil.ReadFile("test.png")
  17. if err != nil {
  18. panic(err)
  19. }
  20. c.Data(http.StatusOK, "image/png", fileBytes)
  21. }

Check gin provided sample code here

huangapple
  • 本文由 发表于 2023年5月17日 16:03:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269810.html
匿名

发表评论

匿名网友

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

确定