/
od-main.go
166 lines (141 loc) · 5 KB
/
od-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
// 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"
"strings"
"time"
json "github.com/minio/colorjson"
humanize "github.com/dustin/go-humanize"
"github.com/minio/cli"
"github.com/minio/mc/pkg/probe"
)
// make a bucket.
var odCmd = cli.Command{
Name: "od",
Usage: "measure single stream upload and download",
Action: mainOD,
Before: setGlobalsFromContext,
OnUsageError: onUsageError,
Flags: globalFlags,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} [OPERANDS]
OPERANDS:
if= source stream to upload
of= target path to upload to
size= size of each part. If not specified, will be calculated from the source stream size.
parts= number of parts to upload. If not specified, will calculated from the source file size.
skip= number of parts to skip.
{{if .VisibleFlags}}
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}{{end}}
EXAMPLES:
1. Upload 200MiB of a file to a bucket in 5 parts of size 40MiB.
{{.HelpName}} if=file.txt of=play/my-bucket/file.txt size=40MiB parts=5
2. Upload a full file to a bucket with 40MiB parts.
{{.HelpName}} if=file.txt of=play/my-bucket/file.txt size=40MiB
3. Upload a full file to a bucket in 5 parts.
{{.HelpName}} if=file.txt of=play/my-bucket/file.txt parts=5
`,
}
type odMessage struct {
Status string `json:"status"`
Type string `json:"type"`
Source string `json:"source"`
Target string `json:"target"`
PartSize uint64 `json:"partSize"`
TotalSize int64 `json:"totalSize"`
Parts int `json:"parts"`
Skip int `json:"skip"`
Elapsed int64 `json:"elapsed"`
}
func (o odMessage) String() string {
cleanSize := humanize.IBytes(uint64(o.TotalSize))
elapsed := time.Duration(o.Elapsed) * time.Millisecond
speed := humanize.IBytes(uint64(float64(o.TotalSize) / elapsed.Seconds()))
if o.Type == "S3toFS" && o.Parts == 0 {
return fmt.Sprintf("Transferred: %s, Full file, Time: %s, Speed: %s/s", cleanSize, elapsed, speed)
}
return fmt.Sprintf("Transferred: %s, Parts: %d, Time: %s, Speed: %s/s", cleanSize, o.Parts, elapsed, speed)
}
func (o odMessage) JSON() string {
odMessageBytes, e := json.MarshalIndent(o, "", " ")
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
return string(odMessageBytes)
}
// getOdUrls returns the URLs for the object download.
func getOdUrls(ctx context.Context, args argKVS) (odURLs URLs, e error) {
inFile := args.Get("if")
outFile := args.Get("of")
// Check if outFile is a folder or a file.
opts := prepareCopyURLsOpts{
sourceURLs: []string{inFile},
targetURL: outFile,
}
copyURLsContent, err := guessCopyURLType(ctx, opts)
fatalIf(err, "Unable to guess copy URL type")
// Get content of inFile, set up URLs.
switch copyURLsContent.copyType {
case copyURLsTypeA:
odURLs = makeCopyContentTypeA(*copyURLsContent)
case copyURLsTypeB:
return URLs{}, fmt.Errorf("invalid source path %s, destination cannot be a directory", outFile)
default:
return URLs{}, fmt.Errorf("invalid source path %s, source cannot be a directory", inFile)
}
return odURLs, nil
}
// odCheckType checks if request is a download or upload and calls the appropriate function
func odCheckType(ctx context.Context, odURLs URLs, args argKVS) (message, error) {
if odURLs.SourceAlias != "" && odURLs.TargetAlias == "" {
return odDownload(ctx, odURLs, args)
}
var odType string
if odURLs.SourceAlias == "" && odURLs.TargetAlias != "" {
odType = "FStoS3"
} else if odURLs.SourceAlias != "" && odURLs.TargetAlias != "" {
odType = "S3toS3"
} else {
odType = "FStoFS"
}
return odCopy(ctx, odURLs, args, odType)
}
// mainOd is the entry point for the od command.
func mainOD(cliCtx *cli.Context) error {
ctx, cancelCopy := context.WithCancel(globalContext)
defer cancelCopy()
if !cliCtx.Args().Present() {
showCommandHelpAndExit(cliCtx, 1) // last argument is exit code
}
var kvsArgs argKVS
for _, arg := range cliCtx.Args() {
kv := strings.SplitN(arg, "=", 2)
kvsArgs.Set(kv[0], kv[1])
}
// Get content from source.
odURLs, e := getOdUrls(ctx, kvsArgs)
fatalIf(probe.NewError(e), "Unable to get source and target URLs")
message, e := odCheckType(ctx, odURLs, kvsArgs)
fatalIf(probe.NewError(e), "Unable to transfer object")
// Print message.
printMsg(message)
return nil
}