-
Notifications
You must be signed in to change notification settings - Fork 36
/
s3.go
77 lines (70 loc) · 1.73 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
package aws
import (
"bytes"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
func S3Put(region, bucketName, keyName, data string) error {
sess, err := session.NewSession(
&aws.Config{
Region: aws.String(region),
},
)
if err != nil {
return err
}
svc := s3.New(sess)
input := &s3.PutObjectInput{
Body: strings.NewReader(data),
Bucket: aws.String(bucketName),
Key: aws.String(keyName),
}
// log.Debugf("S3 uploading object. Bucket '%v', key: '%v', region: '%v'", bucketName, keyName, region)
// Create an uploader with the session and default options
_, err = svc.PutObject(input)
if err != nil {
return err
}
return err
}
func S3Get(region, bucketName, keyName string) (string, error) {
svc := s3.New(session.New(
&aws.Config{
Region: aws.String(region),
},
))
input := &s3.GetObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(keyName),
}
// log.Debugf("S3 downloading object. Bucket '%v', key: '%v', region: '%v'", bucketName, keyName, region)
// Create an uploader with the session and default options
result, err := svc.GetObject(input)
if err != nil {
return "", err
}
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(result.Body)
if err != nil {
return "", err
}
return buf.String(), nil
}
func S3Delete(region, bucketName, keyName string) error {
svc := s3.New(session.New(
&aws.Config{
Region: aws.String(region),
},
))
// log.Debugf("S3 deleting object. Bucket '%v', key: '%v', region: '%v'", bucketName, keyName, region)
_, err := svc.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(keyName),
})
if err != nil {
return err
}
return nil
}