forked from EngineerBetter/concourse-up
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
215 lines (175 loc) · 5.22 KB
/
s3.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
package aws
import (
"bytes"
"io/ioutil"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
const (
// New versions of github.com/aws/aws-sdk-go/aws have these consts
// but the version currently pinned by bosh-cli v2 does not
// ErrCodeNoSuchBucket for service response error code
// "NoSuchBucket".
//
// The specified bucket does not exist.
awsErrCodeNoSuchBucket = "NoSuchBucket"
// ErrCodeNoSuchKey for service response error code
// "NoSuchKey".
//
// The specified key does not exist.
awsErrCodeNoSuchKey = "NoSuchKey"
// Returned when calling HEAD on non-existant bucket or object
awsErrCodeNotFound = "NotFound"
)
// DeleteVersionedBucket deletes and empties a versioned bucket
func DeleteVersionedBucket(name, region string) error {
sess, err := session.NewSession(aws.NewConfig().WithCredentialsChainVerboseErrors(true))
if err != nil {
return err
}
client := s3.New(sess, &aws.Config{Region: ®ion})
// Delete all objects
objects := []*s3.Object{}
err = client.ListObjectsPages(&s3.ListObjectsInput{Bucket: &name},
func(output *s3.ListObjectsOutput, _ bool) bool {
objects = append(objects, output.Contents...)
return true
})
if err != nil {
return err
}
for _, object := range objects {
_, err = client.DeleteObject(&s3.DeleteObjectInput{
Bucket: &name,
Key: object.Key,
})
if err != nil {
return nil
}
}
time.Sleep(time.Second)
_, err = client.DeleteBucket(&s3.DeleteBucketInput{Bucket: &name})
if err != nil {
return err
}
return nil
}
// EnsureBucketExists checks if the named bucket exists and creates it if it doesn't
func EnsureBucketExists(name, region string) error {
sess, err := session.NewSession(aws.NewConfig().WithCredentialsChainVerboseErrors(true))
if err != nil {
return err
}
client := s3.New(sess, &aws.Config{Region: ®ion})
_, err = client.HeadBucket(&s3.HeadBucketInput{Bucket: &name})
if err == nil {
return nil
}
awsErrCode := err.(awserr.Error).Code()
if awsErrCode != awsErrCodeNotFound && awsErrCode != awsErrCodeNoSuchBucket {
return err
}
_, err = client.CreateBucket(&s3.CreateBucketInput{
Bucket: &name,
CreateBucketConfiguration: &s3.CreateBucketConfiguration{
LocationConstraint: ®ion,
},
})
if err != nil {
return err
}
return err
}
// WriteFile writes the specified S3 object
func WriteFile(bucket, path, region string, contents []byte) error {
sess, err := session.NewSession(aws.NewConfig().WithCredentialsChainVerboseErrors(true))
if err != nil {
return err
}
client := s3.New(sess, &aws.Config{Region: ®ion})
_, err = client.PutObject(&s3.PutObjectInput{
Bucket: &bucket,
Key: &path,
Body: bytes.NewReader(contents),
})
return err
}
// HasFile returns true if the specified S3 object exists
func HasFile(bucket, path, region string) (bool, error) {
sess, err := session.NewSession(aws.NewConfig().WithCredentialsChainVerboseErrors(true))
if err != nil {
return false, err
}
client := s3.New(sess, &aws.Config{Region: ®ion})
_, err = client.HeadObject(&s3.HeadObjectInput{Bucket: &bucket, Key: &path})
if err != nil {
awsErrCode := err.(awserr.Error).Code()
if awsErrCode == awsErrCodeNotFound || awsErrCode == awsErrCodeNoSuchKey {
return false, nil
}
return false, err
}
return true, nil
}
// EnsureFileExists checks for the named file in S3 and creates it if it doesn't
// Second argument is true if new file was created
func EnsureFileExists(bucket, path, region string, defaultContents []byte) ([]byte, bool, error) {
sess, err := session.NewSession(aws.NewConfig().WithCredentialsChainVerboseErrors(true))
if err != nil {
return nil, false, err
}
client := s3.New(sess, &aws.Config{Region: ®ion})
output, err := client.GetObject(&s3.GetObjectInput{Bucket: &bucket, Key: &path})
if err == nil {
var contents []byte
contents, err = ioutil.ReadAll(output.Body)
if err != nil {
return nil, false, err
}
// Successfully loaded file
return contents, true, nil
}
awsErrCode := err.(awserr.Error).Code()
if awsErrCode != awsErrCodeNoSuchKey && awsErrCode != awsErrCodeNotFound {
return nil, false, err
}
_, err = client.PutObject(&s3.PutObjectInput{
Bucket: &bucket,
Key: &path,
Body: bytes.NewReader(defaultContents),
})
if err != nil {
return nil, false, err
}
// Created file from given contents
return defaultContents, true, nil
}
// LoadFile loads a file from S3
func LoadFile(bucket, path, region string) ([]byte, error) {
sess, err := session.NewSession(aws.NewConfig().WithCredentialsChainVerboseErrors(true))
if err != nil {
return nil, err
}
client := s3.New(sess, &aws.Config{Region: ®ion})
output, err := client.GetObject(&s3.GetObjectInput{Bucket: &bucket, Key: &path})
if err != nil {
return nil, err
}
return ioutil.ReadAll(output.Body)
}
// DeleteFile deletes a file from S3
func DeleteFile(bucket, path, region string) error {
sess, err := session.NewSession(aws.NewConfig().WithCredentialsChainVerboseErrors(true))
if err != nil {
return err
}
client := s3.New(sess, &aws.Config{Region: ®ion})
_, err = client.DeleteObject(&s3.DeleteObjectInput{
Bucket: &bucket,
Key: &path,
})
return err
}