1.通过gin处理用户http上传文件官方示例
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"io"
"net/http"
"os"
"path/filepath"
"strings"
)
func uploadHandle(c *gin.Context) {
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, int64(30<<20)) // 限制文件大小 30M
c.Request.MultipartReader()
fmt.Println("uploadHandle")
contentType := c.Request.Header.Get("Content-Type")
if !strings.HasPrefix(contentType, "multipart/form-data") {
c.String(http.StatusBadRequest, "Content-Type must be multipart/form-data")
return
}
name := c.PostForm("name")
email := c.PostForm("email")
fmt.Println(name)
// Source
file, err := c.FormFile("file")
if err != nil {
c.String(http.StatusBadRequest, "get form err: %s", err.Error())
return
}
filename := filepath.Base(file.Filename)
if err := c.SaveUploadedFile(file, "./file/"+filename); err != nil {
c.String(http.StatusBadRequest, "upload file err: %s", err.Error())
return
}
c.String(http.StatusOK, "File %s uploaded successfully with fields name=%s and email=%s.", file.Filename, name, email)
}
func main() {
router := gin.Default()
// Set a lower memory limit for multipart forms (default is 32 MiB)
router.MaxMultipartMemory = 8 << 20 // 8 MiB // 限制上传文件时最大内存,超出会写入临时文件中
router.Static("/", "./public")
router.POST("/upload", uploadHandle)
router.Run(":8888")
}
-
MaxMultipartMemory
是限制上传文件时最大内存,超出会写入临时文件中,并不是限制上传文件大小
-
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, int64(30<<20))
可以限制文件上传大小
- 上传文件过程中,可以明显看到磁盘变动,内部变动很小,所以此方法是将用户文件先存入临时文件中,然后在保存到指定文件夹
当然也可以将保存的临时文件在写入minio中,但是我想直接将用户文件流写入minio
2.使用MultipartReader
直接拿到用户上传的文件流
func uploadHandle2(c *gin.Context) {
contentType := c.Request.Header.Get("Content-Type")
if strings.Contains(contentType, "multipart/form-data") {
// Get MultipartReader
reader, err := c.Request.MultipartReader()
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
// Traverse form fields and file streams
for {
part, err := reader.NextPart()
if err == io.EOF {
break
}
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
formName := part.FormName()
fmt.Println("FormName", formName)
// Handle form fields
if formName != "file" {
// Get form field value
value, err := io.ReadAll(part)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
fmt.Println(formName, string(value))
} else { // Handle file streams
// Get file name
fileName := part.FileName()
fmt.Println("fileName", fileName)
// Create file
file, err := os.Create("./file/" + fileName)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
defer file.Close()
// Write file stream to file
_, err = io.Copy(file, part)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.String(http.StatusOK, "File %s uploaded successfully.", fileName)
}
}
} else {
c.String(http.StatusBadRequest, "Content-Type must be multipart/form-data")
}
}
- 这里这里直接将用户上传的文件流写入到文件流,后续我们也可以将用户文件流直接写入minio中
- 对于上传大文件这种方式更有优势,减少本地磁盘的占用
3.将用户文件流写入minio中
func uploadHandle3(c *gin.Context) {
// Get MultipartReader.
reader, err := c.Request.MultipartReader()
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
expiration := time.Now().AddDate(0, 0, 7) // 文件七天后删除
// Traverse form fields and file streams.
for {
part, err := reader.NextPart()
if err == io.EOF {
break
}
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
formName := part.FormName()
fmt.Println("FormName", formName)
// Handle form fields.
if formName != "file" {
// Get form field value
value, err := io.ReadAll(part)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
fmt.Println(formName, string(value))
} else { // Handle file streams.
// Get file name
fileName := part.FileName()
fmt.Println("fileName", fileName, "Content-Type", part.Header.Get("Content-Type"))
// Create a new file in MinIO.
info, err := minioClient.PutObject(c.Request.Context(), bucketName, fileName, part, -1, minio.PutObjectOptions{ContentType: part.Header.Get("Content-Type"), Expires: expiration})
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
fmt.Printf("%+v", info)
//fmt.Println(info)
c.String(http.StatusOK, "File %s uploaded successfully.", fileName)
}
}
}
4.完整代码
package main
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
var endpoint = "localhost:9000"
var accessKeyID = "toQVQar56ALcXP4k0oNv"
var secretAccessKey = "7styo8rKK73AqZZlJ9qC4rtIJH74NOtnmG9YD9oN"
var useSSL = false
var bucketName = "testbucket"
var location = "us-east-1"
var minioClient *minio.Client
func uploadHandle3(c *gin.Context) {
// Get MultipartReader.
reader, err := c.Request.MultipartReader()
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
expiration := time.Now().AddDate(0, 0, 7) // 文件七天后删除
// Traverse form fields and file streams.
for {
part, err := reader.NextPart()
if err == io.EOF {
break
}
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
formName := part.FormName()
fmt.Println("FormName", formName)
// Handle form fields.
if formName != "file" {
// Get form field value
value, err := io.ReadAll(part)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
fmt.Println(formName, string(value))
} else { // Handle file streams.
// Get file name
fileName := part.FileName()
fmt.Println("fileName", fileName, "Content-Type", part.Header.Get("Content-Type"))
// Create a new file in MinIO.
info, err := minioClient.PutObject(c.Request.Context(), bucketName, fileName, part, -1, minio.PutObjectOptions{ContentType: part.Header.Get("Content-Type"), Expires: expiration})
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
fmt.Printf("%+v", info)
//fmt.Println(info)
c.String(http.StatusOK, "File %s uploaded successfully.", fileName)
}
}
}
func uploadHandle2(c *gin.Context) {
contentType := c.Request.Header.Get("Content-Type")
if strings.Contains(contentType, "multipart/form-data") {
// Get MultipartReader
reader, err := c.Request.MultipartReader()
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
// Traverse form fields and file streams
for {
part, err := reader.NextPart()
if err == io.EOF {
break
}
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
formName := part.FormName()
fmt.Println("FormName", formName)
// Handle form fields
if formName != "file" {
// Get form field value
value, err := io.ReadAll(part)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
fmt.Println(formName, string(value))
} else { // Handle file streams
// Get file name
fileName := part.FileName()
fmt.Println("fileName", fileName)
// Create file
file, err := os.Create("./file/" + fileName)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
defer file.Close()
// Write file stream to file
_, err = io.Copy(file, part)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.String(http.StatusOK, "File %s uploaded successfully.", fileName)
}
}
} else {
c.String(http.StatusBadRequest, "Content-Type must be multipart/form-data")
}
}
func uploadHandle(c *gin.Context) {
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, int64(30<<20)) // 限制文件大小 30M
c.Request.MultipartReader()
fmt.Println("uploadHandle")
contentType := c.Request.Header.Get("Content-Type")
if !strings.HasPrefix(contentType, "multipart/form-data") {
c.String(http.StatusBadRequest, "Content-Type must be multipart/form-data")
return
}
name := c.PostForm("name")
email := c.PostForm("email")
fmt.Println(name)
// Source
file, err := c.FormFile("file")
if err != nil {
c.String(http.StatusBadRequest, "get form err: %s", err.Error())
return
}
filename := filepath.Base(file.Filename)
if err := c.SaveUploadedFile(file, "./file/"+filename); err != nil {
c.String(http.StatusBadRequest, "upload file err: %s", err.Error())
return
}
c.String(http.StatusOK, "File %s uploaded successfully with fields name=%s and email=%s.", file.Filename, name, email)
}
func main() {
ctx := context.Background()
var err error
// Initialize minio client object.
minioClient, err = minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: useSSL,
})
if err != nil {
log.Fatalln(err)
}
// Make a new bucket called testbucket.
err = minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: location})
if err != nil {
// Check to see if we already own this bucket (which happens if you run this twice)
exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
if errBucketExists == nil && exists {
log.Printf("We already own %s\n", bucketName)
} else {
log.Fatalln(err)
}
} else {
log.Printf("Successfully created %s\n", bucketName)
}
router := gin.Default()
// Set a lower memory limit for multipart forms (default is 32 MiB)
router.MaxMultipartMemory = 8 << 20 // 8 MiB // 限制上传文件时最大内存,超出会写入临时文件中
router.Static("/", "./public")
router.POST("/upload", uploadHandle)
router.POST("/upload2", uploadHandle2)
router.POST("/upload3", uploadHandle3)
router.Run(":8888")
}
网友评论