-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.go
196 lines (180 loc) · 4.91 KB
/
upload.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"context"
"fmt"
"log"
"net/url"
"os"
"path"
"strings"
"sync"
"github.com/dustin/go-humanize"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/urfave/cli/v2"
)
var upload = &cli.Command{
Name: "upload",
Usage: "upload local file to s3",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "dir",
EnvVars: []string{"dir"},
},
&cli.StringFlag{
Name: "dst_endpoint",
EnvVars: []string{"dst_endpoint"},
Required: true,
},
&cli.StringFlag{
Name: "dst_ak",
EnvVars: []string{"dst_ak"},
Required: true,
},
&cli.StringFlag{
Name: "dst_sk",
EnvVars: []string{"dst_sk"},
Required: true,
},
&cli.StringFlag{
Name: "dst_bucket",
EnvVars: []string{"dst_bucket"},
Required: true,
},
&cli.StringFlag{
Name: "dst_region",
EnvVars: []string{"dst_region"},
Required: false,
Hidden: true,
},
&cli.StringFlag{
Name: "dst_prefix",
EnvVars: []string{"dst_prefix"},
},
&cli.StringFlag{
Name: "filelist",
EnvVars: []string{"filelist"},
Usage: "specify the list to be downloaded, one url per line",
},
&cli.StringFlag{
Name: "PartSize",
EnvVars: []string{"PartSize"},
Value: "16MiB",
},
&cli.UintFlag{
Name: "NumThreads",
EnvVars: []string{"NumThreads"},
Value: 4,
},
&cli.BoolFlag{
Name: "EnableMemCache",
EnvVars: []string{"EnableMemCache"},
Usage: "after turning it on, it will obviously occupy memory. PartSize*NumThreads",
},
&cli.BoolFlag{
Name: "DisableMultipart",
EnvVars: []string{"DisableMultipart"},
Value: true,
},
&cli.BoolFlag{
Name: "DisableContentSha256",
EnvVars: []string{"DisableContentSha256"},
Value: true,
},
&cli.IntFlag{
Name: "concurrent",
EnvVars: []string{"concurrent"},
Value: 10,
},
},
Action: uploadAction,
}
func uploadAction(cctx *cli.Context) error {
dst_bucket := cctx.String("dst_bucket")
dst_region := cctx.String("dst_region")
dst_prefix := cctx.String("dst_prefix")
if cctx.IsSet("dir") && cctx.IsSet("filelist") {
return fmt.Errorf("only be specified dir or filelist")
}
PartSize, err := humanize.ParseBytes(cctx.String("PartSize"))
if err != nil {
return err
}
NumThreads := cctx.Uint("NumThreads")
ConcurrentStreamParts := cctx.Bool("EnableMemCache")
DisableMultipart := cctx.Bool("DisableMultipart")
DisableContentSha256 := cctx.Bool("DisableContentSha256")
disableLookupDomain = cctx.Bool("disable_lookup")
parsedDst, err := url.Parse(cctx.String("dst_endpoint"))
if err != nil {
return err
}
dst_endpoint := parsedDst.Host
dst_ssl := parsedDst.Scheme == "https"
dstOptions := &minio.Options{
Creds: credentials.NewStaticV4(cctx.String("dst_ak"), cctx.String("dst_sk"), ""),
Secure: dst_ssl,
Region: dst_region,
Transport: transport,
}
ctx := context.Background()
// A wait group to manage the number of active goroutines.
var wg sync.WaitGroup
// Create a buffered channel to manage the number of workers.
workerCh := make(chan struct{}, cctx.Int("concurrent"))
var lines []string
if cctx.IsSet("dir") {
lines, err = listFiles(cctx.String("dir"))
if err != nil {
return err
}
} else if cctx.IsSet("filelist") {
content, err := os.ReadFile(cctx.String("filelist"))
if err != nil {
log.Fatal(err)
}
lines = strings.Split(strings.TrimSpace(string(content)), "\n")
}
for _, key := range lines {
// Start a new worker.
wg.Add(1)
workerCh <- struct{}{} // Add to the worker queue.
go func(key string) {
defer wg.Done()
defer func() {
<-workerCh // Remove from the worker queue.
}()
var objectName string
if string(key[0]) == "/" {
objectName = path.Join(dst_prefix, key[1:])
} else {
objectName = path.Join(dst_prefix, key)
}
dst, err := minio.New(dst_endpoint, dstOptions)
if err != nil {
log.Println(err)
return
}
// Check if object already exists in the destination bucket.
log.Printf("start StatObject %s in bucket %s\n", objectName, dst_bucket)
_, err = dst.StatObject(ctx, dst_bucket, objectName, minio.StatObjectOptions{})
if err == nil {
log.Printf("object %s already exists in destination bucket %s\n", key, dst_bucket)
return
} else if !strings.Contains(err.Error(), "The specified key does not exist.") {
log.Println("StatObject error:", err)
return
}
log.Printf("start upload %s to bucket %s\n", key, dst_bucket)
_, err = dst.FPutObject(ctx, dst_bucket, objectName, key, minio.PutObjectOptions{NumThreads: NumThreads, PartSize: PartSize, ConcurrentStreamParts: ConcurrentStreamParts, DisableMultipart: DisableMultipart, DisableContentSha256: DisableContentSha256})
if err != nil {
log.Println("FPutObject error:", err)
return
}
log.Printf("object %s upload to destination bucket %s\n", key, dst_bucket)
}(key)
}
// Wait for all workers to finish.
wg.Wait()
return nil
}