-
Notifications
You must be signed in to change notification settings - Fork 18
/
sos_download.go
183 lines (154 loc) · 4.28 KB
/
sos_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
package cmd
import (
"fmt"
"io"
"log"
"os"
"path"
"time"
minio "github.com/minio/minio-go/v6"
"github.com/spf13/cobra"
"github.com/vbauerster/mpb/v4"
"github.com/vbauerster/mpb/v4/decor"
)
var downloadCmd = &cobra.Command{
Use: "download BUCKET OBJECT DESTINATION",
Short: "Download an object from a bucket",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 3 {
return cmd.Usage()
}
bucket := args[0]
objectName := args[1]
localFilePath := args[2]
certsFile, err := cmd.Parent().Flags().GetString("certs-file")
if err != nil {
return err
}
sosClient, err := newSOSClient(certsFile)
if err != nil {
return err
}
location, err := sosClient.GetBucketLocation(bucket)
if err != nil {
return err
}
if err := sosClient.setZone(location); err != nil {
return err
}
// Verify if destination already exists.
force, err := cmd.Flags().GetBool("force")
if err != nil {
return err
}
if !force {
exists, err := destinationExists(localFilePath, objectName)
if err != nil {
return err
}
if exists {
return fmt.Errorf("file %q: already exists", localFilePath)
}
}
// Gather md5sum.
objectStat, err := sosClient.StatObjectWithContext(gContext, bucket, objectName, minio.StatObjectOptions{})
if err != nil {
return err
}
// Write to a temporary file "fileName.part.minio" before saving.
filePartPath := localFilePath + objectStat.ETag + ".part.minio"
// If exists, open in append mode. If not create it as a part file.
filePart, err := os.OpenFile(filePartPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600)
if err != nil {
return err
}
// Issue Stat to get the current offset.
st, err := filePart.Stat()
if err != nil {
return err
}
object, err := sosClient.GetObjectWithContext(gContext, bucket, objectName, minio.GetObjectOptions{})
if err != nil {
return err
}
defer object.Close() //nolint: errcheck
// XXXXX I have to do that because this feature of minio-go doesn't working:
// THAT:
_, err = object.Seek(st.Size(), 0)
if err != nil {
return err
}
// INSTEAD OF:
/*
opts := minio.GetObjectOptions{}
// Initialize get object request headers to set the
// appropriate range offsets to read from.
if st.Size() > 0 {
opts.SetRange(st.Size(), 0)
}
object, err := minioClient.GetObjectWithContext(gContext, bucketName, objectName, opts)
*/
// XXXXXX
progress := mpb.NewWithContext(gContext,
// override default (80) width
mpb.WithWidth(64),
// override default 120ms refresh rate
mpb.WithRefreshRate(180*time.Millisecond),
)
bar := progress.AddBar(objectStat.Size,
mpb.PrependDecorators(
// simple name decorator
decor.Name(objectName, decor.WC{W: len(objectName) + 1, C: decor.DidentRight}),
// decor.DSyncWidth bit enables column width synchronization
decor.Percentage(decor.WCSyncSpace),
),
mpb.AppendDecorators(
decor.AverageETA(decor.ET_STYLE_GO),
),
)
bar.IncrBy(int(st.Size()))
reader := bar.ProxyReader(object)
// Write to the part file.
if _, err = io.CopyN(filePart, reader, objectStat.Size-st.Size()); err != nil {
return err
}
// Close the file before rename, this is specifically needed for Windows users.
if err = filePart.Close(); err != nil {
return err
}
// Safely completed. Now commit by renaming to actual filename.
if err = os.Rename(filePartPath, localFilePath); err != nil {
return err
}
progress.Wait()
log.Printf("Successfully downloaded %q into %q\n", objectName, localFilePath)
return nil
},
}
// destinationExists verifies that the given destination does not exist
func destinationExists(localFilePath string, objectName string) (bool, error) {
st, err := os.Stat(localFilePath)
if err == nil {
// If the destination exists and is a directory.
if st.IsDir() {
localFilePath = path.Join(localFilePath, objectName)
_, err = os.Stat(localFilePath)
if err == nil {
return true, nil
}
} else {
return true, nil
}
}
// Proceed if file does not exist. return for all other errors.
if err != nil {
if !os.IsNotExist(err) {
return false, err
}
}
return false, nil
}
func init() {
sosCmd.AddCommand(downloadCmd)
downloadCmd.Flags().BoolP("force", "f", false, "Overwrite the destination if it already exists")
}