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

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

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/):

package main

import (
	"io/ioutil"
	"net/http"
)

func main() {
	handler := http.HandlerFunc(handleRequest)
	http.Handle("/photo", handler)
	http.ListenAndServe(":8080", nil)
}

func handleRequest(w http.ResponseWriter, r *http.Request) {
	fileBytes, err := ioutil.ReadFile("test.png")
	if err != nil {
		panic(err)
	}
	w.WriteHeader(http.StatusOK)
	w.Header().Set("Content-Type", "application/octet-stream")
	w.Write(fileBytes)
	return
}
英文:

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/):

package main

import (
	"io/ioutil"
	"net/http"
)

func main() {
	handler := http.HandlerFunc(handleRequest)
	http.Handle("/photo", handler)
	http.ListenAndServe(":8080", nil)
}

func handleRequest(w http.ResponseWriter, r *http.Request) {
	fileBytes, err := ioutil.ReadFile("test.png")
	if err != nil {
		panic(err)
	}
	w.WriteHeader(http.StatusOK)
	w.Header().Set("Content-Type", "application/octet-stream")
	w.Write(fileBytes)
	return
}

答案1

得分: 2

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

import (
	"io/ioutil"
	"net/http"

	"github.com/gin-gonic/gin"
)

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

	r.GET("/photo", photoHandler)

	if err := r.Run(":8080"); err != nil {
		panic(err)
	}
}

func photoHandler(c *gin.Context) {

	// 在这里进行其他检查

	// 读取文件
	fileBytes, err := ioutil.ReadFile("test.png")
	if err != nil {
		panic(err)
	}

	c.Data(http.StatusOK, "image/png", fileBytes)
}

你可以在这里查看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.

import (
	"io/ioutil"
	"net/http"

	"github.com/gin-gonic/gin"
)

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

	r.GET("/photo", photoHandler)

	if err := r.Run(":8080"); err != nil {
		panic(err)
	}
}

func photoHandler(c *gin.Context) {

	// do the other checks here

	// read the file
	fileBytes, err := ioutil.ReadFile("test.png")
	if err != nil {
		panic(err)
	}

	
	c.Data(http.StatusOK, "image/png", fileBytes)
}

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:

确定