-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy paths3.go
More file actions
182 lines (162 loc) · 4.27 KB
/
s3.go
File metadata and controls
182 lines (162 loc) · 4.27 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package client
import (
"context"
"crypto/tls"
"net/http"
"os"
"strings"
"time"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
const (
s3DefaultTimeout = 30 * time.Second
s3TransferTimeout = 24 * time.Hour
)
type s3Client struct {
scType string
bucket string
client *minio.Client
}
func (s *s3Client) ctx(timeout time.Duration) (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), timeout)
}
func NewS3Client(vars map[string]interface{}) (*s3Client, error) {
accessKey := loadParamFromVars("accessKey", vars)
secretKey := loadParamFromVars("secretKey", vars)
endpoint := loadParamFromVars("endpoint", vars)
region := loadParamFromVars("region", vars)
bucket := loadParamFromVars("bucket", vars)
scType := loadParamFromVars("scType", vars)
if len(scType) == 0 {
scType = "Standard"
}
mode := loadParamFromVars("mode", vars)
if len(mode) == 0 {
mode = "virtual hosted"
}
lookupStyle := minio.BucketLookupDNS
if mode == "path" {
lookupStyle = minio.BucketLookupPath
}
ssl := strings.Split(endpoint, ":")[0]
secure := false
tlsConfig := &tls.Config{}
if ssl == "https" {
secure = true
tlsConfig.InsecureSkipVerify = true
}
var transport http.RoundTripper = &http.Transport{
TLSClientConfig: tlsConfig,
}
endpoint = strings.TrimPrefix(endpoint, ssl+"://")
client, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKey, secretKey, ""),
Secure: secure,
Region: region,
BucketLookup: lookupStyle,
Transport: transport,
})
if err != nil {
return nil, err
}
return &s3Client{scType: scType, bucket: bucket, client: client}, nil
}
func (s s3Client) ListBuckets() ([]interface{}, error) {
ctx, cancel := s.ctx(s3DefaultTimeout)
defer cancel()
buckets, err := s.client.ListBuckets(ctx)
if err != nil {
return nil, err
}
var result []interface{}
for _, b := range buckets {
result = append(result, b.Name)
}
return result, nil
}
func (s s3Client) Exist(path string) (bool, error) {
ctx, cancel := s.ctx(s3DefaultTimeout)
defer cancel()
_, err := s.client.StatObject(ctx, s.bucket, path, minio.StatObjectOptions{})
if err != nil {
resp := minio.ToErrorResponse(err)
if resp.StatusCode == 404 {
return false, nil
}
return false, err
}
return true, nil
}
func (s *s3Client) Size(path string) (int64, error) {
ctx, cancel := s.ctx(s3DefaultTimeout)
defer cancel()
info, err := s.client.StatObject(ctx, s.bucket, path, minio.StatObjectOptions{})
if err != nil {
return 0, err
}
return info.Size, nil
}
func (s s3Client) Delete(path string) (bool, error) {
ctx, cancel := s.ctx(s3DefaultTimeout)
defer cancel()
if err := s.client.RemoveObject(ctx, s.bucket, path, minio.RemoveObjectOptions{}); err != nil {
return false, err
}
return true, nil
}
func (s s3Client) Upload(src, target string) (bool, error) {
fileInfo, err := os.Stat(src)
if err != nil {
return false, err
}
file, err := os.Open(src)
if err != nil {
return false, err
}
defer file.Close()
opts := minio.PutObjectOptions{
StorageClass: s.scType,
}
const maxParts = 10000
const defaultPartSize = 64 * 1024 * 1024 // 64 MiB
partSize := uint64(defaultPartSize)
if fileInfo.Size() > int64(maxParts)*int64(defaultPartSize) {
partSize = uint64(fileInfo.Size()) / (maxParts - 1)
}
opts.PartSize = partSize
ctx, cancel := s.ctx(s3TransferTimeout)
defer cancel()
if _, err := s.client.PutObject(ctx, s.bucket, target, file, fileInfo.Size(), opts); err != nil {
return false, err
}
return true, nil
}
func (s s3Client) Download(src, target string) (bool, error) {
if _, err := os.Stat(target); err == nil {
_ = os.Remove(target)
}
ctx, cancel := s.ctx(s3TransferTimeout)
defer cancel()
if err := s.client.FGetObject(ctx, s.bucket, src, target, minio.GetObjectOptions{}); err != nil {
os.Remove(target)
return false, err
}
return true, nil
}
func (s *s3Client) ListObjects(prefix string) ([]string, error) {
opts := minio.ListObjectsOptions{
Recursive: true,
Prefix: prefix,
}
var result []string
ctx, cancel := s.ctx(s3DefaultTimeout)
defer cancel()
for object := range s.client.ListObjects(ctx, s.bucket, opts) {
if object.Err != nil {
return result, object.Err
}
result = append(result, object.Key)
}
return result, nil
}