golang 中常用的文件上传框架包括:gorilla/mux:轻量级、高性能,适用于高并发应用。cloud storage:谷歌云平台提供的托管文件存储服务,可扩展且可靠。s3 manager:对 amazon s3 提供全面支持,可执行各种操作。
GoLang 框架中文件上传的开源实现
文件上传是 Web 开发中的常见任务。随着 GoLang 的兴起,出现了许多开源库来简化文件上传过程。本文将介绍几个流行的 GoLang 文件上传框架。
带有实战案例 的框架
1. Gorilla/mux
特点:轻量级、高性能,适用于高并发应用 实战案例 :1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"fmt"
"<a style=color:f60; text-decoration:underline; href="https://www.php.cn/zt/15841.html" target="_blank">git</a>hub.com/gorilla/mux"
"html"
"log"
"net/http"
"os"
)
func main() {
router := mux.NewRouter()
router.HandleFunc("/", handlePostFile)
log.Fatal(http.ListenAndServe(":8080", router))
}
func handlePostFile(w http.ResponseWriter, r http.Request) {
// Parse multipart form and store files to disk
if err := r.ParseMultipartForm(32 << 20); err != nil {
http.Error(w, "Cannot parse multipart form", http.StatusBadRequest)
return
}
file, handler, err := r.FormFile(r.PostFormValue("filename"))
if err != nil {
http.Error(w, "Cannot retrieve file", http.StatusBadRequest)
return
}
f, err := os.OpenFile("./uploads/"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
http.Error(w, "Cannot save file", http.StatusInternalServerError)
return
}
defer f.Close()
if _, err = io.Copy(f, file); err != nil {
http.Error(w, "Cannot copy file to disk", http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "File %q uploaded successfully", handler.Filename)
}
2. Cloud Storage
特点:谷歌云平台提供的托管文件存储服务,可扩展且可靠实战案例 :1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main
import (
"context"
"fmt"
"github.com/GoogleCloudPlatform/go-cloud-storage/cloudstorage"
"io"
)
func main() {
ctx := context.Background()
// Replace "your-bucket-name" with the name of your bucket
bucketName := "your-bucket-name"
// Initialize client
client, err := cloudstorage.NewClient(ctx)
if err != nil {
fmt.Println(err)
return
}
// Upload file
f := client.Bucket(bucketName).Object("my-file.txt")
wr := f.NewWriter(ctx)
if _, err := wr.Write([]byte("Hello, world!")); err != nil {
fmt.Println(err)
return
}
if err := wr.Close(); err != nil {
fmt.Println(err)
return
}
fmt.Printf("File uploaded to %s", f.GCSObject.MediaLink)
}
3. S3 Manager
特点:对 Amazon S3 提供了全面的支持,可执行各种操作实战案例 :1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"context"
"fmt"
"io"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
func main() {
ctx := context.Background()
// Replace "your-bucket-name" with the name of your bucket
bucket := "your-bucket-name"
sess := session.Must(session.NewSession(&aws.Config{
Region: aws.String("us-east-1"), // Replace with your desired region
}))
uploader := s3manager.NewUploader(sess)
f, err := os.Open("my-file.txt")
if err != nil {
fmt.Printf("Unable to open file %q, %v", "my-file.txt", err)
return
}
// Upload file
result, err := uploader.Upload(&s3manager.UploadInput{
Bucket: &bucket,
Key: "my-file.txt",
Body: f,
})
if err != nil {
fmt.Printf("Unable to upload file %q, %v", "my-file.txt", err)
return
}
fmt.Printf("File uploaded to %s", result.Location)
}
以上就是golang 框架中文件上传的开源实现的详细内容,更多请关注其它相关文章!