-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3.go
289 lines (245 loc) · 10.2 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
package main
import (
"context"
"net/http"
"github.com/aws/smithy-go"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http"
"net/url"
"time"
"fmt"
"os"
"strconv"
"errors"
)
func getPartSize() int64 {
var partSize int64
strSizeInMb, err := os.LookupEnv("DOWNLOAD_PART_SIZE")
if !err {
elog.Println(time.Now().Format(time.RFC3339) + "DOWNLOAD_PART_SIZE is not present..using DefaultDownloadPartSize ")
partSize = manager.DefaultDownloadPartSize
} else {
sizeInMb, err := strconv.Atoi(strSizeInMb)
if err != nil {
elog.Println(time.Now().Format(time.RFC3339) + " DOWNLOAD_PART_SIZE conversion issue..using DefaultDownloadPartSize ")
partSize = manager.DefaultDownloadPartSize
} else {
partSize = int64(sizeInMb) * 1024 * 1204
}
}
return partSize
}
func getRegion() string {
region, err := os.LookupEnv("AWS_REGION")
if !err {
elog.Println(time.Now().Format(time.RFC3339) + " AWS_REGION is not present..using us-east-1")
region = "us-east-1"
}
return region
}
// check if a bucket exists.
func bucketExists(bucket string) (bool, error) {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(getRegion()),
)
if err != nil {
elog.Println( time.Now().Format(time.RFC3339) + " bucketExists: Filed to load config for bucket "+bucket + " error : " + err.Error())
return false, errors.New("Filed to load config")
}
s3client := s3.NewFromConfig(cfg)
_, err = s3client.HeadBucket(context.TODO(),&s3.HeadBucketInput{Bucket: aws.String(bucket)})
if err != nil {
var apiErr smithy.APIError
if errors.As(err, &apiErr) {
var httpResponseErr *awshttp.ResponseError
if errors.As(err, &httpResponseErr) {
switch httpResponseErr.HTTPStatusCode() {
case http.StatusMovedPermanently:
elog.Println( time.Now().Format(time.RFC3339) + " bucketExists: failed for bucket "+bucket + " error : " + err.Error())
return false, errors.New("Bucket StatusMovedPermanently ")
case http.StatusForbidden:
elog.Println( time.Now().Format(time.RFC3339) + " bucketExists: failed for bucket "+bucket + " error : " + err.Error())
return false, errors.New("Bucket StatusForbidden")
case http.StatusNotFound:
elog.Println( time.Now().Format(time.RFC3339) + " bucketExists: failed for bucket "+bucket + " error : " + err.Error())
return false, nil
default:
elog.Println(time.Now().Format(time.RFC3339) + " bucketExists: ResponseError failed for bucket "+bucket + "with error: "+err.Error())
return false, errors.New("Filed to find bucket")
}
} else {
elog.Println(time.Now().Format(time.RFC3339) + " bucketExists: ApiError failed for bucket "+bucket + "with error: "+err.Error())
return false, errors.New("Filed to find bucket")
}
} else {
elog.Println(time.Now().Format(time.RFC3339) + " bucketExists: failed for bucket "+bucket + "with error: "+err.Error())
return false, errors.New("Filed to find bucket")
}
}
return true,nil
}
func getHeadObject(bucket string, key string) (*s3.HeadObjectOutput, error) {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(getRegion()),
)
if err != nil {
elog.Println( time.Now().Format(time.RFC3339) + " getHeadObject: Filed to load config for bucket "+bucket + " error : " + err.Error())
return nil, errors.New("Filed to load config")
}
s3client := s3.NewFromConfig(cfg)
headObjectResponse, err := s3client.HeadObject(context.TODO(), &s3.HeadObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
var apiErr smithy.APIError
if errors.As(err, &apiErr) {
var httpResponseErr *awshttp.ResponseError
if errors.As(err, &httpResponseErr) {
switch httpResponseErr.HTTPStatusCode() {
case http.StatusMovedPermanently:
elog.Println( time.Now().Format(time.RFC3339) + " getHeadObject: failed for bucket "+bucket +" key "+key+" error : " + err.Error())
return nil, errors.New("Bucket StatusMovedPermanently ")
case http.StatusForbidden:
elog.Println( time.Now().Format(time.RFC3339) + " getHeadObject: failed for bucket "+bucket +" key "+key+" error : " + err.Error())
return nil, errors.New("Bucket StatusForbidden")
case http.StatusNotFound:
elog.Println( time.Now().Format(time.RFC3339) + " getHeadObject: failed for bucket "+bucket +" key "+key+" error : " + err.Error())
return nil, errors.New("Bucket StatusNotFound")
default:
elog.Println(time.Now().Format(time.RFC3339) + " getHeadObject: ResponseError failed for bucket "+bucket +" key "+key+" with error: "+err.Error())
return nil, errors.New("Filed to find object")
}
} else {
elog.Println(time.Now().Format(time.RFC3339) + " getHeadObject: APIError failed for bucket "+bucket +" key "+key+" with error: "+err.Error())
return nil, errors.New("Filed to find object")
}
} else {
elog.Println(time.Now().Format(time.RFC3339) + " getHeadObject: failed for bucket "+bucket +" key "+key+" with error: "+err.Error())
return nil, errors.New("Filed to find object")
}
}
return headObjectResponse, nil
}
// check if a file exists.
func keyExists(bucket string, key string) (bool, error) {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(getRegion()),
)
if err != nil {
elog.Println( time.Now().Format(time.RFC3339) + "keyExists: Filed to load config for bucket "+bucket +" key "+key+" error : " + err.Error())
return false, errors.New("Filed to load config")
}
s3client := s3.NewFromConfig(cfg)
_, err = s3client.HeadObject(context.TODO(), &s3.HeadObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
var apiErr smithy.APIError
if errors.As(err, &apiErr) {
var httpResponseErr *awshttp.ResponseError
if errors.As(err, &httpResponseErr) {
switch httpResponseErr.HTTPStatusCode() {
case http.StatusNotFound:
elog.Println( time.Now().Format(time.RFC3339) + " keyExists: failed for bucket "+bucket +" key "+key+" error : " + err.Error())
return false, nil
default:
elog.Println(time.Now().Format(time.RFC3339) + " keyExists: ResponseError failed for bucket "+bucket +" key "+key+" with error: "+err.Error())
return false, errors.New("Filed to find key")
}
} else {
elog.Println(time.Now().Format(time.RFC3339) + " keyExists: APIErrorfailed for bucket "+bucket +" key "+key+" with error: "+err.Error())
return false, errors.New("Filed to find key")
}
} else {
elog.Println(time.Now().Format(time.RFC3339) + " keyExists: failed for bucket "+bucket +" key "+key+" with error: "+err.Error())
return false, errors.New("Filed to find key")
}
}
return true, nil
}
func readFile(bucket string, item string) ([] byte, error) {
// Load AWS Config
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(getRegion()),
)
if err != nil {
elog.Println( time.Now().Format(time.RFC3339) + " readFile: Filed to load config to read file " +item+ " from bucket "+bucket + " error : " + err.Error())
return nil, errors.New("Filed to load config")
}
// Create an S3 client using the loaded configuration
s3client := s3.NewFromConfig(cfg)
// Create a downloader with the client and custom downloader options
downloader := manager.NewDownloader(s3client, func(d *manager.Downloader) {
d.PartSize = getPartSize()
})
headObject, err := getHeadObject(bucket,item)
if err != nil {
elog.Println( time.Now().Format(time.RFC3339) + " readFile: getHeadObject failed " +item+ " from bucket "+bucket + " error : " + err.Error())
return nil, errors.New("Filed to read file")
}
// pre-allocate in memory buffer, where headObject type is *s3.HeadObjectOutput
buff := make([]byte, int(*headObject.ContentLength))
// wrap with aws.WriteAtBuffer
w := manager.NewWriteAtBuffer(buff)
// download file into the memory
_, err = downloader.Download(context.TODO(), w, &s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(item),
})
if err != nil {
elog.Println( time.Now().Format(time.RFC3339) + " Unable to read file " +item+ " from bucket "+bucket + " error : " + err.Error())
return nil, errors.New("Unable to read file")
}
info.Println(time.Now().Format(time.RFC3339) +" Downloaded file "+item+ " from bucket "+bucket)
return buff, nil
}
func copyFile(bucket string, item string, other string) (error){
// Load AWS Config
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(getRegion()),
)
if err != nil {
elog.Println( time.Now().Format(time.RFC3339) + " copyFile: Filed to load config to read file " +item+ " from bucket "+bucket + " error : " + err.Error())
return errors.New("Filed to load config")
}
// Create an S3 client using the loaded configuration
s3client := s3.NewFromConfig(cfg)
source := bucket + "/" + item
_, err = s3client.CopyObject(context.TODO(), &s3.CopyObjectInput{
Bucket: aws.String(other),
CopySource: aws.String(url.PathEscape(source)),
Key: aws.String(item),
})
if err != nil {
elog.Println( time.Now().Format(time.RFC3339) + " Unable to read file " +item+ " from bucket "+bucket+ " to bucket "+other+" error : " + err.Error())
return errors.New("Unable to copy file")
}
info.Println( time.Now().Format(time.RFC3339) + " File "+ item+ " successfully copied from bucket "+bucket+ " to bucket "+other)
return nil
}
func deleteFile(bucket string, item string) (error) {
// Load AWS Config
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(getRegion()),
)
if err != nil {
elog.Println( time.Now().Format(time.RFC3339) + " deleteFile: Filed to load config to read file " +item+ " from bucket "+bucket + " error : " + err.Error())
return errors.New("Filed to load config")
}
// Create an S3 client using the loaded configuration
s3client := s3.NewFromConfig(cfg)
_, err = s3client.DeleteObject(context.TODO(), &s3.DeleteObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(item),
})
if err != nil {
elog.Println( time.Now().Format(time.RFC3339) + " Error occurred while deleting file " +item+ " from bucket "+bucket+" err: "+ fmt.Sprint(err))
return errors.New("Error occurred while deleting file")
}
return nil
}