-
Notifications
You must be signed in to change notification settings - Fork 402
/
awscli.go
205 lines (173 loc) · 4.74 KB
/
awscli.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package s3client
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"github.com/zeebo/errs"
)
// AWSCLIError is class for minio errors
var AWSCLIError = errs.Class("aws-cli error")
// AWSCLI implements basic S3 Client with aws-cli
type AWSCLI struct {
conf Config
}
// NewAWSCLI creates new Client
func NewAWSCLI(conf Config) (Client, error) {
if !strings.HasPrefix(conf.S3Gateway, "https://") &&
!strings.HasPrefix(conf.S3Gateway, "http://") {
conf.S3Gateway = "http://" + conf.S3Gateway
}
return &AWSCLI{conf}, nil
}
func (client *AWSCLI) cmd(subargs ...string) *exec.Cmd {
args := []string{
"--endpoint", client.conf.S3Gateway,
}
if client.conf.NoSSL {
args = append(args, "--no-verify-ssl")
}
args = append(args, subargs...)
cmd := exec.Command("aws", args...)
cmd.Env = append(os.Environ(),
"AWS_ACCESS_KEY_ID="+client.conf.AccessKey,
"AWS_SECRET_ACCESS_KEY="+client.conf.SecretKey,
)
return cmd
}
// MakeBucket makes a new bucket
func (client *AWSCLI) MakeBucket(bucket, location string) error {
cmd := client.cmd("s3", "mb", "s3://"+bucket, "--region", location)
_, err := cmd.Output()
if err != nil {
return AWSCLIError.Wrap(fullExitError(err))
}
return nil
}
// RemoveBucket removes a bucket
func (client *AWSCLI) RemoveBucket(bucket string) error {
cmd := client.cmd("s3", "rb", "s3://"+bucket)
_, err := cmd.Output()
if err != nil {
return AWSCLIError.Wrap(fullExitError(err))
}
return nil
}
// ListBuckets lists all buckets
func (client *AWSCLI) ListBuckets() ([]string, error) {
cmd := client.cmd("s3api", "list-buckets", "--output", "json")
jsondata, err := cmd.Output()
if err != nil {
return nil, AWSCLIError.Wrap(fullExitError(err))
}
var response struct {
Buckets []struct {
Name string `json:"Name"`
} `json:"Buckets"`
}
err = json.Unmarshal(jsondata, &response)
if err != nil {
return nil, AWSCLIError.Wrap(fullExitError(err))
}
names := []string{}
for _, bucket := range response.Buckets {
names = append(names, bucket.Name)
}
return names, nil
}
// Upload uploads object data to the specified path
func (client *AWSCLI) Upload(bucket, objectName string, data []byte) error {
// TODO: add upload threshold
cmd := client.cmd("s3", "cp", "-", "s3://"+bucket+"/"+objectName)
cmd.Stdin = bytes.NewReader(data)
_, err := cmd.Output()
if err != nil {
return AWSCLIError.Wrap(fullExitError(err))
}
return nil
}
// UploadMultipart uses multipart uploads, has hardcoded threshold
func (client *AWSCLI) UploadMultipart(bucket, objectName string, data []byte, threshold int) error {
// TODO: add upload threshold
cmd := client.cmd("s3", "cp", "-", "s3://"+bucket+"/"+objectName)
cmd.Stdin = bytes.NewReader(data)
_, err := cmd.Output()
if err != nil {
return AWSCLIError.Wrap(fullExitError(err))
}
return nil
}
// Download downloads object data
func (client *AWSCLI) Download(bucket, objectName string, buffer []byte) ([]byte, error) {
cmd := client.cmd("s3", "cp", "s3://"+bucket+"/"+objectName, "-")
buf := &bufferWriter{buffer[:0]}
cmd.Stdout = buf
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return nil, AWSCLIError.Wrap(fullExitError(err))
}
return buf.data, nil
}
type bufferWriter struct {
data []byte
}
func (b *bufferWriter) Write(data []byte) (n int, err error) {
b.data = append(b.data, data...)
return len(data), nil
}
// Delete deletes object
func (client *AWSCLI) Delete(bucket, objectName string) error {
cmd := client.cmd("s3", "rm", "s3://"+bucket+"/"+objectName)
_, err := cmd.Output()
if err != nil {
return AWSCLIError.Wrap(fullExitError(err))
}
return nil
}
// ListObjects lists objects
func (client *AWSCLI) ListObjects(bucket, prefix string) ([]string, error) {
cmd := client.cmd("s3api", "list-objects",
"--output", "json",
"--bucket", bucket,
"--prefix", prefix,
"--delimiter", "/")
jsondata, err := cmd.Output()
if err != nil {
return nil, AWSCLIError.Wrap(fullExitError(err))
}
var response struct {
Contents []struct {
Key string `json:"Key"`
} `json:"Contents"`
CommonPrefixes []struct {
Key string `json:"Prefix"`
} `json:"CommonPrefixes"`
}
err = json.Unmarshal(jsondata, &response)
if err != nil {
return nil, AWSCLIError.Wrap(fullExitError(err))
}
names := []string{}
for _, object := range response.Contents {
names = append(names, object.Key)
}
for _, object := range response.CommonPrefixes {
names = append(names, object.Key)
}
return names, nil
}
// fullExitError returns error string with the Stderr output
func fullExitError(err error) error {
if err == nil {
return nil
}
if exitErr, ok := err.(*exec.ExitError); ok {
return fmt.Errorf("%v\n%v", exitErr.Error(), string(exitErr.Stderr))
}
return err
}