forked from minio/mc
/
mv-main.go
303 lines (253 loc) · 10.1 KB
/
mv-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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright (c) 2015-2022 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"context"
"fmt"
"os"
"sync"
"github.com/fatih/color"
"github.com/minio/cli"
"github.com/trinet2005/oss-mc/pkg/probe"
"github.com/trinet2005/oss-pkg/console"
)
// mv command flags.
var (
mvFlags = []cli.Flag{
cli.BoolFlag{
Name: "recursive, r",
Usage: "move recursively",
},
cli.StringFlag{
Name: "older-than",
Usage: "move objects older than value in duration string (e.g. 7d10h31s)",
},
cli.StringFlag{
Name: "newer-than",
Usage: "move objects newer than value in duration string (e.g. 7d10h31s)",
},
cli.StringFlag{
Name: "storage-class, sc",
Usage: "set storage class for new object(s) on target",
},
cli.StringFlag{
Name: "encrypt",
Usage: "encrypt/decrypt objects (using server-side encryption with server managed keys)",
},
cli.StringFlag{
Name: "attr",
Usage: "add custom metadata for the object",
},
cli.BoolFlag{
Name: "continue, c",
Usage: "create or resume move session",
},
cli.BoolFlag{
Name: "preserve, a",
Usage: "preserve filesystem attributes (mode, ownership, timestamps)",
},
cli.BoolFlag{
Name: "disable-multipart",
Usage: "disable multipart upload feature",
},
}
)
// Move command.
var mvCmd = cli.Command{
Name: "mv",
Usage: "move objects",
Action: mainMove,
OnUsageError: onUsageError,
Before: setGlobalsFromContext,
Flags: append(append(mvFlags, ioFlags...), globalFlags...),
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} [FLAGS] SOURCE [SOURCE...] TARGET
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
ENVIRONMENT VARIABLES:
MC_ENCRYPT: list of comma delimited prefixes
MC_ENCRYPT_KEY: list of comma delimited prefix=secret values
EXAMPLES:
01. Move a list of objects from local file system to Amazon S3 cloud storage.
{{.Prompt}} {{.HelpName}} Music/*.ogg s3/jukebox/
02. Move a folder recursively from MinIO cloud storage to Amazon S3 cloud storage.
{{.Prompt}} {{.HelpName}} --recursive play/mybucket/ s3/mybucket/
03. Move multiple local folders recursively to MinIO cloud storage.
{{.Prompt}} {{.HelpName}} --recursive backup/2014/ backup/2015/ play/archive/
04. Move a bucket recursively from aliased Amazon S3 cloud storage to local filesystem on Windows.
{{.Prompt}} {{.HelpName}} --recursive s3\documents\2014\ C:\Backups\2014
05. Move files older than 7 days and 10 hours from MinIO cloud storage to Amazon S3 cloud storage.
{{.Prompt}} {{.HelpName}} --older-than 7d10h play/mybucket/myfolder/ s3/mybucket/
06. Move files newer than 7 days and 10 hours from MinIO cloud storage to a local path.
{{.Prompt}} {{.HelpName}} --newer-than 7d10h play/mybucket/myfolder/ ~/latest/
07. Move an object with name containing unicode characters to Amazon S3 cloud storage.
{{.Prompt}} {{.HelpName}} 本語 s3/andoria/
08. Move a local folder with space separated characters to Amazon S3 cloud storage.
{{.Prompt}} {{.HelpName}} --recursive 'workdir/documents/May 2014/' s3/miniocloud
09. Move a folder with encrypted objects recursively from Amazon S3 to MinIO cloud storage.
{{.Prompt}} {{.HelpName}} --recursive --encrypt-key "s3/documents/=32byteslongsecretkeymustbegiven1,myminio/documents/=32byteslongsecretkeymustbegiven2" s3/documents/ myminio/documents/
10. Move a folder with encrypted objects recursively from Amazon S3 to MinIO cloud storage. In case the encryption key contains non-printable character like tab, pass the
base64 encoded string as key.
{{.Prompt}} {{.HelpName}} --recursive --encrypt-key "s3/documents/=MzJieXRlc2xvbmdzZWNyZWFiY2RlZmcJZ2l2ZW5uMjE=,myminio/documents/=MzJieXRlc2xvbmdzZWNyZWFiY2RlZmcJZ2l2ZW5uMjE=" s3/documents/ myminio/documents/
11. Move a list of objects from local file system to MinIO cloud storage with specified metadata, separated by ";"
{{.Prompt}} {{.HelpName}} --attr "key1=value1;key2=value2" Music/*.mp4 play/mybucket/
12. Move a folder recursively from MinIO cloud storage to Amazon S3 cloud storage with Cache-Control and custom metadata, separated by ";".
{{.Prompt}} {{.HelpName}} --attr "Cache-Control=max-age=90000,min-fresh=9000;key1=value1;key2=value2" --recursive play/mybucket/myfolder/ s3/mybucket/
13. Move a text file to an object storage and assign REDUCED_REDUNDANCY storage-class to the uploaded object.
{{.Prompt}} {{.HelpName}} --storage-class REDUCED_REDUNDANCY myobject.txt play/mybucket
14. Move a text file to an object storage and create or resume copy session.
{{.Prompt}} {{.HelpName}} --recursive --continue dir/ play/mybucket
15. Move a text file to an object storage and preserve the file system attribute as metadata.
{{.Prompt}} {{.HelpName}} -a myobject.txt play/mybucket
16. Move a text file to an object storage and disable multipart upload feature.
{{.Prompt}} {{.HelpName}} --disable-multipart myobject.txt play/mybucket
`,
}
type removeClientInfo struct {
client Client
contentCh chan *ClientContent
resultCh <-chan RemoveResult
}
type removeManager struct {
removeMap map[string]*removeClientInfo
removeMapMutex sync.RWMutex
wg sync.WaitGroup
}
func (rm *removeManager) readErrors(resultCh <-chan RemoveResult, targetURL string) {
rm.wg.Add(1)
go func() {
defer rm.wg.Done()
for result := range resultCh {
if result.Err != nil {
errorIf(result.Err.Trace(targetURL), "Failed to remove in`"+targetURL+"`.")
}
}
}()
}
// This function should be parallel-safe because it is executed by ParallelManager
// If targetAlias is empty, it means we will target local FS contents
func (rm *removeManager) add(ctx context.Context, targetAlias, targetURL string) {
rm.removeMapMutex.Lock()
clientInfo := rm.removeMap[targetAlias]
if clientInfo == nil {
client, pErr := newClientFromAlias(targetAlias, targetURL)
if pErr != nil {
errorIf(pErr.Trace(targetURL), "Invalid argument `"+targetURL+"`.")
return
}
contentCh := make(chan *ClientContent, 10000)
resultCh := client.Remove(ctx, false, false, false, false, contentCh)
rm.readErrors(resultCh, targetURL)
clientInfo = &removeClientInfo{
client: client,
contentCh: contentCh,
resultCh: resultCh,
}
rm.removeMap[targetAlias] = clientInfo
}
rm.removeMapMutex.Unlock()
clientInfo.contentCh <- &ClientContent{URL: *newClientURL(targetURL)}
}
func (rm *removeManager) close() {
for _, clientInfo := range rm.removeMap {
close(clientInfo.contentCh)
}
// Wait until all on-going client.Remove() operations to finish
rm.wg.Wait()
}
var rmManager = &removeManager{
removeMap: make(map[string]*removeClientInfo),
}
// mainMove is the entry point for mv command.
func mainMove(cliCtx *cli.Context) error {
ctx, cancelMove := context.WithCancel(globalContext)
defer cancelMove()
// Parse encryption keys per command.
encKeyDB, err := getEncKeys(cliCtx)
fatalIf(err, "Unable to parse encryption keys.")
// Parse metadata.
userMetaMap := make(map[string]string)
if cliCtx.String("attr") != "" {
userMetaMap, err = getMetaDataEntry(cliCtx.String("attr"))
fatalIf(err, "Unable to parse attribute %v", cliCtx.String("attr"))
}
// check 'copy' cli arguments.
checkCopySyntax(ctx, cliCtx, encKeyDB, true)
if cliCtx.NArg() == 2 {
args := cliCtx.Args()
srcURL := args.Get(0)
dstURL := args.Get(1)
if srcURL == dstURL {
fatalIf(errDummy().Trace(), fmt.Sprintf("Source and destination urls cannot be the same: %v.", srcURL))
}
}
// Additional command speific theme customization.
console.SetColor("Copy", color.New(color.FgGreen, color.Bold))
recursive := cliCtx.Bool("recursive")
olderThan := cliCtx.String("older-than")
newerThan := cliCtx.String("newer-than")
storageClass := cliCtx.String("storage-class")
sseKeys := os.Getenv("MC_ENCRYPT_KEY")
if key := cliCtx.String("encrypt-key"); key != "" {
sseKeys = key
}
if sseKeys != "" {
sseKeys, err = getDecodedKey(sseKeys)
fatalIf(err, "Unable to parse encryption keys.")
}
sse := cliCtx.String("encrypt")
var session *sessionV8
if cliCtx.Bool("continue") {
sessionID := getHash("mv", cliCtx.Args())
if isSessionExists(sessionID) {
session, err = loadSessionV8(sessionID)
fatalIf(err.Trace(sessionID), "Unable to load session.")
} else {
session = newSessionV8(sessionID)
session.Header.CommandType = "mv"
session.Header.CommandBoolFlags["recursive"] = recursive
session.Header.CommandStringFlags["older-than"] = olderThan
session.Header.CommandStringFlags["newer-than"] = newerThan
session.Header.CommandStringFlags["storage-class"] = storageClass
session.Header.CommandStringFlags["encrypt-key"] = sseKeys
session.Header.CommandStringFlags["encrypt"] = sse
session.Header.CommandBoolFlags["session"] = cliCtx.Bool("continue")
if cliCtx.Bool("preserve") {
session.Header.CommandBoolFlags["preserve"] = cliCtx.Bool("preserve")
}
session.Header.UserMetaData = userMetaMap
session.Header.CommandBoolFlags["disable-multipart"] = cliCtx.Bool("disable-multipart")
var e error
if session.Header.RootPath, e = os.Getwd(); e != nil {
session.Delete()
fatalIf(probe.NewError(e), "Unable to get current working folder.")
}
// extract URLs.
session.Header.CommandArgs = cliCtx.Args()
}
}
e := doCopySession(ctx, cancelMove, cliCtx, session, encKeyDB, true)
if session != nil {
session.Delete()
}
console.Colorize("Copy", "Waiting for move operations to complete")
rmManager.close()
return e
}