-
Notifications
You must be signed in to change notification settings - Fork 0
/
minio.go
76 lines (58 loc) · 1.73 KB
/
minio.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
package minio
import (
"context"
"errors"
"io"
"time"
"github.com/minio/minio-go/v7"
)
func CreateBucket(bucketName string) error {
if len(bucketName) <= 0 {
return errors.New("bucketName invalid")
}
ctx := context.Background()
if err := minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{}); err != nil {
exists, errEx := minioClient.BucketExists(ctx, bucketName)
if exists && errEx != nil {
// nothing
} else {
return errEx
}
}
return nil
}
func UploadFileByPath(bucketName, objectName, path, contentType string) (int64, error) {
if len(bucketName) <= 0 || len(objectName) <= 0 || len(path) <= 0 {
return -1, errors.New("invalid argument")
}
uploadInfo, err := minioClient.FPutObject(context.Background(), bucketName, objectName, path, minio.PutObjectOptions{
ContentType: contentType,
})
if err != nil {
return -1, err
}
return uploadInfo.Size, nil
}
func UploadFileByIO(bucketName, objectName string, reader io.Reader, size int64, contentType string) (int64, error) {
if len(bucketName) <= 0 || len(objectName) <= 0 {
return -1, errors.New("invalid argument")
}
uploadInfo, err := minioClient.PutObject(context.Background(), bucketName, objectName, reader, size, minio.PutObjectOptions{
ContentType: contentType,
})
if err != nil {
return -1, err
}
return uploadInfo.Size, nil
}
func GetFileTemporaryURL(bucketName, objectName string) (string, error) {
if len(bucketName) <= 0 || len(objectName) <= 0 {
return "", errors.New("invalid argument")
}
expiry := time.Second * time.Duration(ExpireTime)
presignedURL, err := minioClient.PresignedGetObject(context.Background(), bucketName, objectName, expiry, nil)
if err != nil {
return "", err
}
return presignedURL.String(), nil
}