-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy paths3.go
50 lines (40 loc) · 1.02 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
package main
import (
"bytes"
"fmt"
"io"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
func getS3Object(bucket, key string) (io.ReadCloser, error) {
sess, err := session.NewSession()
if err != nil {
return nil, fmt.Errorf("error creating AWS session (check access keys): %s", err)
}
in := &s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
}
obj, err := s3.New(sess).GetObject(in)
if err != nil {
return nil, fmt.Errorf("error getting S3 object: %s", err)
}
return obj.Body, nil
}
func putS3Object(bucket, key string, buf []byte) error {
sess, err := session.NewSession()
if err != nil {
return fmt.Errorf("error creating AWS session (check access keys): %s", err)
}
in := &s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
Body: bytes.NewReader(buf),
}
_, err = s3.New(sess).PutObject(in)
if err != nil {
return fmt.Errorf("error putting S3 object: %s", err)
}
return nil
}