-
Notifications
You must be signed in to change notification settings - Fork 18
/
storage_download.go
262 lines (216 loc) · 6.19 KB
/
storage_download.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
package cmd
import (
"context"
"errors"
"fmt"
"os"
"path"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
s3manager "github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
s3types "github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/spf13/cobra"
"github.com/vbauerster/mpb/v4"
"github.com/vbauerster/mpb/v4/decor"
)
type storageDownloadConfig struct {
bucket string
prefix string
source string
destination string
objects []*s3types.Object
recursive bool
overwrite bool
dryRun bool
}
var storageDownloadCmd = &cobra.Command{
Use: "download sos://BUCKET/[OBJECT|PREFIX/] [DESTINATION]",
Aliases: []string{"get"},
Short: "Download files from a bucket",
Long: `This command downloads files from a bucket.
If no destination argument is provided, files will be stored into the current
directory.
Examples:
# Download a single file
exo storage download sos://my-bucket/file-a
# Download a single file and rename it locally
exo storage download sos://my-bucket/file-a file-z
# Download a prefix recursively
exo storage download -r sos://my-bucket/public/ /tmp/public/
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 || len(args) > 2 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
args[0] = strings.TrimPrefix(args[0], storageBucketPrefix)
// Append implicit root prefix ("/") if only a bucket name is specified in the source
if !strings.Contains(args[0], "/") {
args[0] = args[0] + "/"
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
bucket string
prefix string
src = args[0]
dst = "./"
)
certsFile, err := cmd.Flags().GetString("certs-file")
if err != nil {
return err
}
dryRun, err := cmd.Flags().GetBool("dry-run")
if err != nil {
return err
}
force, err := cmd.Flags().GetBool("force")
if err != nil {
return err
}
recursive, err := cmd.Flags().GetBool("recursive")
if err != nil {
return err
}
parts := strings.SplitN(src, "/", 2)
bucket = parts[0]
if len(parts) > 1 {
prefix = parts[1]
if prefix == "" {
prefix = "/"
}
}
if len(args) == 2 {
dst = args[1]
}
if strings.HasSuffix(src, "/") && !recursive {
return fmt.Errorf("%q is a directory, use flag `-r` to download recursively", src)
}
storage, err := newStorageClient(
storageClientOptWithCertsFile(certsFile),
storageClientOptZoneFromBucket(bucket),
)
if err != nil {
return fmt.Errorf("unable to initialize storage client: %v", err)
}
objects := make([]*s3types.Object, 0)
if err := storage.forEachObject(bucket, prefix, recursive, func(o *s3types.Object) error {
objects = append(objects, o)
return nil
}); err != nil {
return fmt.Errorf("error listing objects: %s", err)
}
return storage.downloadFiles(&storageDownloadConfig{
bucket: bucket,
prefix: prefix,
source: src,
objects: objects,
destination: dst,
recursive: recursive,
overwrite: force,
dryRun: dryRun,
})
},
}
func init() {
storageDownloadCmd.Flags().BoolP("force", "f", false,
"overwrite existing destination files")
storageDownloadCmd.Flags().BoolP("dry-run", "n", false,
"simulate files download, don't actually do it")
storageDownloadCmd.Flags().BoolP("recursive", "r", false,
"download prefix recursively")
storageCmd.AddCommand(storageDownloadCmd)
}
func (c *storageClient) downloadFiles(config *storageDownloadConfig) error {
if len(config.objects) > 1 && !strings.HasSuffix(config.destination, "/") {
return errors.New(`multiple files to download, destination must end with "/"`)
}
// Handle relative filesystem destination (e.g. ".", "../.." etc.)
if dstInfo, err := os.Stat(config.destination); err == nil {
if dstInfo.IsDir() && !strings.HasSuffix(config.destination, "/") {
config.destination += "/"
}
}
if config.dryRun {
fmt.Println("[DRY-RUN]")
}
for _, object := range config.objects {
dst := func() string {
if strings.HasSuffix(config.source, "/") {
return path.Join(config.destination, strings.TrimPrefix(aws.ToString(object.Key), config.prefix))
}
if strings.HasSuffix(config.destination, "/") {
return path.Join(config.destination, path.Base(aws.ToString(object.Key)))
}
return path.Join(config.destination)
}()
if config.dryRun {
fmt.Printf("%s/%s -> %s\n", config.bucket, aws.ToString(object.Key), dst)
continue
}
if _, err := os.Stat(dst); err == nil && !config.overwrite {
return fmt.Errorf("file %q already exists, use flag `-f` to overwrite", dst)
}
if _, err := os.Stat(path.Dir(dst)); errors.Is(err, os.ErrNotExist) {
if err := os.MkdirAll(path.Dir(dst), 0o755); err != nil {
return err
}
}
if err := c.downloadFile(config.bucket, object, dst); err != nil {
return err
}
}
return nil
}
func (c *storageClient) downloadFile(bucket string, object *s3types.Object, dst string) error {
maxFilenameLen := 16
pb := mpb.NewWithContext(gContext,
mpb.ContainerOptOn(mpb.WithOutput(nil), func() bool {
return gQuiet
}),
)
bar := pb.AddBar(
object.Size,
mpb.PrependDecorators(
decor.Name(ellipString(aws.ToString(object.Key), maxFilenameLen),
decor.WC{W: maxFilenameLen, C: decor.DidentRight}),
),
mpb.AppendDecorators(
decor.CountersKibiByte("% .2f / % .2f", decor.WCSyncWidthR),
decor.Name(" | "),
decor.Elapsed(decor.ET_STYLE_GO),
),
)
f, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644)
if err != nil {
return err
}
defer f.Close()
getObjectInput := s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: object.Key,
}
_, err = s3manager.
NewDownloader(c.Client).
Download(
gContext,
// mpb doesn't natively support the io.WriteAt interface expected
// by the s3manager.Download()'s w parameter, so we wrap in a shim
// to be able to track the download progress. Trick inspired from
// https://github.com/vbauerster/mpb/blob/v4/proxyreader.go
&proxyWriterAt{
wt: f,
bar: bar,
iT: time.Now(),
},
&getObjectInput,
)
pb.Wait()
if errors.Is(err, context.Canceled) {
fmt.Fprintf(os.Stderr, "\rDownload interrupted by user\n")
return nil
}
return err
}