-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
145 lines (126 loc) · 3.32 KB
/
main.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"log"
"net/http"
"path/filepath"
)
func main() {
minioClient, err := minio.New("localhost:9000", &minio.Options{
Creds: credentials.NewStaticV4("admin", "admin123", ""),
Secure: false,
})
if err != nil {
log.Fatal(err)
}
// ایجاد bucket در صورت نیاز
bucketName := "my-fat-bucket"
err = minioClient.MakeBucket(context.Background(), bucketName, minio.MakeBucketOptions{})
if err != nil {
exists, bucketErr := minioClient.BucketExists(context.Background(), bucketName)
if bucketErr == nil && exists {
log.Println("bucket already exist")
} else {
log.Fatal("bucket check error :", bucketErr)
}
} else {
log.Println("bucket successfully created")
}
r := gin.Default()
r.LoadHTMLFiles("upload.html")
r.GET("/upload", func(c *gin.Context) {
c.HTML(200, "upload.html", nil)
})
r.POST("/upload", func(c *gin.Context) {
// store single file
//info, done := singleFileStore(c, minioClient, bucketName)
//if done {
// return
//}
// multi store file
files, err := c.MultipartForm()
if err != nil {
c.JSON(200, gin.H{
"err": err,
})
return
}
if files == nil || len(files.File["files"]) == 0 {
c.JSON(200, gin.H{
"msg": "no files uploaded",
})
return
}
var uploadedFiles []map[string]interface{}
for _, file := range files.File["files"] {
// باز کردن فایل
fileToUpload, err := file.Open()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Unable to open file"})
return
}
defer fileToUpload.Close()
// استفاده از UUID برای نامگذاری فایل
objectName := uuid.New().String()
// آپلود فایل به MinIO
info, err := minioClient.PutObject(
c.Request.Context(),
bucketName,
"products/"+objectName+filepath.Ext(file.Filename),
fileToUpload,
file.Size,
minio.PutObjectOptions{},
)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error uploading file"})
return
}
uploadedFiles = append(uploadedFiles, map[string]interface{}{
"info_file": info,
})
}
c.JSON(200, gin.H{
"info": uploadedFiles,
"msg": "files uploaded successfully",
})
return
})
fmt.Println("http://localhost:8081")
log.Fatal(r.Run(":8081"))
}
func singleFileStore(c *gin.Context, minioClient *minio.Client, bucketName string) (minio.UploadInfo, bool) {
file, _ := c.FormFile("files")
if file == nil {
c.JSON(200, gin.H{
"msg": "file is nil",
})
return minio.UploadInfo{}, true
}
// باز کردن فایل
fileToUpload, err := file.Open()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Unable to open file"})
return minio.UploadInfo{}, true
}
defer fileToUpload.Close()
objectName := uuid.New()
//http://localhost:9000/my-fat-bucket/products/1c13b62b-5f0d-42e7-8eec-e1955ad3b45c.jpg
info, err := minioClient.PutObject(
c.Request.Context(),
bucketName,
"products/"+objectName.String()+filepath.Ext(file.Filename),
fileToUpload,
file.Size,
minio.PutObjectOptions{},
)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error uploading file"})
return minio.UploadInfo{}, true
}
return info, false
}