-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy paths3kor.go
276 lines (224 loc) · 9.17 KB
/
s3kor.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
package main
import (
"fmt"
"io/ioutil"
"net/url"
"os"
"runtime"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/aws"
"go.uber.org/zap"
"github.com/alecthomas/kingpin"
"github.com/aws/aws-sdk-go/aws/session"
)
///Command line flags
var (
app = kingpin.New("s3kor", "s3 tools using golang concurency")
pCustomEndpointURL = app.Flag("custom-endpoint-url", "AWS S3 Custom Endpoint URL").String()
pProfile = app.Flag("profile", "AWS credentials/config file profile to use").String()
pRegion = app.Flag("region", "AWS region").String()
pDetectRegion = app.Flag("detect-region", "Auto detect region for the buckets").Default("false").Bool()
pVerbose = app.Flag("verbose", "Verbose Logging").Default("false").Bool()
rm = app.Command("rm", "remove")
rmQuiet = rm.Flag("quiet", "Does not display the operations performed from the specified command.").Short('q').Default("false").Bool()
rmRecursive = rm.Flag("recursive", "Recurisvley delete").Short('r').Default("false").Bool()
rmAllVersions = rm.Flag("all-versions", "Delete all versions and delete markers").Default("false").Bool()
rmMultiParts = rm.Flag("multi-part", "Abort all inprogress multipart uploads").Default("false").Bool()
rmPath = rm.Arg("S3Uri", "S3 URL").Required().String()
ls = app.Command("ls", "list")
lsAllVersions = ls.Flag("all-versions", "List all versions").Default("false").Bool()
lsPath = ls.Arg("S3Uri", "S3 URL").Required().String()
cp = app.Command("cp", "copy")
cpSource = cp.Arg("source", "file or s3 location").Required().String()
cpDestination = cp.Arg("destination", "file or s3 location").Required().String()
cpQuiet = cp.Flag("quiet", "Does not display the operations performed from the specified command.").Short('q').Default("false").Bool()
cpRecursive = cp.Flag("recursive", "Recursively copy").Short('r').Default("False").Bool()
cpConcurrent = cp.Flag("concurrent", "Maximum number of concurrent uploads to S3.").Short('c').Default("30").Int()
cpSSE = cp.Flag("sse", "Specifies server-side encryption of the object in S3. Valid values are AES256 and aws:kms.").Default("AES256").Enum("AES256", "aws:kms")
cpSSEKMSKeyID = cp.Flag("sse-kms-key-id", "The AWS KMS key ID that should be used to server-side encrypt the object in S3.").String()
cpACL = cp.Flag("acl", "Object ACL").Default(s3.ObjectCannedACLPrivate).Enum(s3.ObjectCannedACLAuthenticatedRead,
s3.ObjectCannedACLAwsExecRead,
s3.ObjectCannedACLBucketOwnerFullControl,
s3.ObjectCannedACLBucketOwnerRead,
s3.ObjectCannedACLPrivate,
s3.ObjectCannedACLPublicRead,
s3.ObjectCannedACLPublicReadWrite)
cpStorageClass = cp.Flag("storage-class", "Storage Class").Default(s3.StorageClassStandard).Enum(s3.StorageClassStandard,
s3.StorageClassStandardIa,
s3.StorageClassDeepArchive,
s3.StorageClassGlacier,
s3.StorageClassOnezoneIa,
s3.StorageClassReducedRedundancy,
s3.StorageClassIntelligentTiering,
StorageClassOptimizeIA,
StorageClassOptimizeGlacier,
StorageClassOptimizeDeepArchive,
)
cpDestProfile = cp.Flag("dest-profile", "Destination bucket AWS credentials/config file profile to use if different from --profile").String()
cpAccelerate = cp.Flag("accelerate", "Use S3 Acceleration").Default("false").Bool()
syncOp = app.Command("sync", "sync")
syncSource = syncOp.Arg("source", "file or s3 location").Required().String()
syncDestination = syncOp.Arg("destination", "file or s3 location").Required().String()
syncQuiet = syncOp.Flag("quiet", "Does not display the operations performed from the specified command.").Short('q').Default("false").Bool()
syncConcurrent = syncOp.Flag("concurrent", "Maximum number of concurrent uploads to S3.").Short('c').Default("20").Int()
syncSSE = syncOp.Flag("sse", "Specifies server-side encryption of the object in S3. Valid values are AES256 and aws:kms.").Default("AES256").Enum("AES256", "aws:kms")
syncSSEKMSKeyID = syncOp.Flag("sse-kms-key-id", "The AWS KMS key ID that should be used to server-side encrypt the object in S3.").String()
syncACL = syncOp.Flag("acl", "Object ACL").Default(s3.ObjectCannedACLPrivate).Enum(s3.ObjectCannedACLAuthenticatedRead,
s3.ObjectCannedACLAwsExecRead,
s3.ObjectCannedACLBucketOwnerFullControl,
s3.ObjectCannedACLBucketOwnerRead,
s3.ObjectCannedACLPrivate,
s3.ObjectCannedACLPublicRead,
s3.ObjectCannedACLPublicReadWrite)
syncStorageClass = syncOp.Flag("storage-class", "Storage Class").Default(s3.StorageClassStandard).Enum(s3.StorageClassStandard,
s3.StorageClassStandardIa,
s3.StorageClassDeepArchive,
s3.StorageClassGlacier,
s3.StorageClassOnezoneIa,
s3.StorageClassReducedRedundancy,
s3.StorageClassIntelligentTiering)
syncDestProfile = syncOp.Flag("dest-profile", "Destination bucket AWS credentials/config file profile to use if different from --profile").String()
syncAccelerate = syncOp.Flag("accelerate", "Use S3 Acceleration").Default("false").Bool()
)
//version variable which can be overidden at compile time
var (
version = "dev-local-version"
commit = "none"
date = "unknown"
)
///Needed to workaround abug with zap logger and daft windows file paths/names
func newWinFileSink(u *url.URL) (zap.Sink, error) {
// Remove leading slash left by url.Parse()
return os.OpenFile(u.Path[1:], os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) //nolint:gosec
}
func setUpLogger() {
var config zap.Config
if *pVerbose {
config = zap.NewDevelopmentConfig()
} else {
config = zap.NewProductionConfig()
}
logFile, err := ioutil.TempFile(os.TempDir(), "s3kor")
if err == nil {
//defer logFile.Close()
//workaround for windows file paths and names
if runtime.GOOS == "windows" {
err = zap.RegisterSink("winfile", newWinFileSink)
if err == nil {
config.OutputPaths = []string{
"winfile:///" + logFile.Name(),
}
}
} else {
config.OutputPaths = []string{
logFile.Name(),
}
}
}
logger, err := config.Build()
if err == nil {
zap.ReplaceGlobals(logger)
zap.RedirectStdLog(logger)
}
logger.Debug("Logging enabled")
}
func getAwsConfig() aws.Config {
config := aws.Config{
CredentialsChainVerboseErrors: aws.Bool(true),
MaxRetries: aws.Int(30),
}
if pCustomEndpointURL == nil || (pCustomEndpointURL != nil && *pCustomEndpointURL == "") {
if pRegion != nil && *pRegion != "" {
config.Region = aws.String(*pRegion)
}
return config
}
fmt.Printf("Using custom endpoint [%+v] on region [%+v]\n", *pCustomEndpointURL, *pRegion)
config.Endpoint = aws.String(*pCustomEndpointURL)
config.S3ForcePathStyle = aws.Bool(true)
if pRegion != nil && *pRegion != "" {
config.Region = aws.String(*pRegion)
} else {
config.Region = aws.String("customendpoint")
}
return config
}
func getAwsSession() *session.Session {
var sess *session.Session
if *pProfile != "" {
sess = session.Must(session.NewSessionWithOptions(session.Options{
Profile: *pProfile,
SharedConfigState: session.SharedConfigEnable,
Config: getAwsConfig(),
}))
} else {
sess = session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
Config: getAwsConfig(),
}))
} //else
return sess
}
func switchCommand(command string) error {
logger := zap.S()
logger.Debug("func switchCommand(command string) error")
var err error
sess := getAwsSession()
switch command {
case rm.FullCommand():
var deleter *BucketDeleter
deleter, err = NewBucketDeleter(*pDetectRegion, *rmPath, *rmQuiet, 50, *rmAllVersions, *rmRecursive, *rmMultiParts, sess)
if err == nil {
err = deleter.delete()
}
case ls.FullCommand():
var lister *BucketLister
lister, err = NewBucketLister(*pDetectRegion, *lsPath, *lsAllVersions, 50, sess)
if err == nil {
err = lister.List(*lsAllVersions)
}
case cp.FullCommand():
inputTemplate := s3manager.UploadInput{
ACL: cpACL,
StorageClass: cpStorageClass,
ServerSideEncryption: cpSSE,
}
if *cpSSEKMSKeyID != "" {
inputTemplate.ServerSideEncryption = cpSSEKMSKeyID
}
var copier *BucketCopier
copier, err = NewBucketCopier(*pDetectRegion, *cpSource, *cpDestination, *cpConcurrent, *cpQuiet, sess, inputTemplate, *cpDestProfile, *cpRecursive, *cpAccelerate)
if err == nil {
err = copier.copy()
}
case syncOp.FullCommand():
inputTemplate := s3manager.UploadInput{
ACL: syncACL,
StorageClass: syncStorageClass,
ServerSideEncryption: syncSSE,
}
if *syncSSEKMSKeyID != "" {
inputTemplate.ServerSideEncryption = syncSSEKMSKeyID
}
var syncer *BucketSyncer
syncer, err = NewSync(*pDetectRegion, *syncSource, *syncDestination, *syncConcurrent, *syncQuiet, sess, inputTemplate, *syncDestProfile, *cpAccelerate)
if err == nil {
err = syncer.sync()
}
}
return err
}
func main() {
//Parse args and flags passed to us
app.Version(version + " " + commit + " " + date)
kingpin.CommandLine.HelpFlag.Short('h')
command := kingpin.MustParse(app.Parse(os.Args[1:]))
setUpLogger()
logger := zap.S()
err := switchCommand(command)
if err != nil {
fmt.Println(err.Error())
logger.Fatal(err.Error())
}
}