-
Notifications
You must be signed in to change notification settings - Fork 5
/
s3.go
115 lines (91 loc) · 2.46 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
package s3
import (
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/aws/aws-sdk-go/service/s3/s3manager/s3manageriface"
)
var (
fileCreate = os.Create
fileOpen = os.Open
)
// Provider describes a new S3 storage provider.
type Provider struct {
session *session.Session
uploader s3manageriface.UploaderAPI
downloader s3manageriface.DownloaderAPI
bucket string
}
// Kind returns the provider kind.
func (s3p Provider) Kind() string {
return "s3"
}
// CollectionRef gets the bucket reference.
func (s3p Provider) CollectionRef() string {
return s3p.bucket
}
// UploadFile puts a file in the relevant bucket.
func (s3p Provider) UploadFile(filename, reference string) error {
// Open file for writing to S3.
file, err := fileOpen(filename)
// Error if file cannot be opened.
if err != nil {
return err
}
defer file.Close()
// Use the upload manager to write to S3.
_, err = s3p.uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(s3p.bucket),
Key: aws.String(reference),
Body: file,
})
// Error if file cannot be uploaded.
if err != nil {
return err
}
return nil
}
// DownloadFile gets the file from an S3 bucket.
func (s3p Provider) DownloadFile(reference, filename string) error {
// Create file for writing.
file, err := fileCreate(filename)
// Error if file cannot be created.
if err != nil {
return err
}
defer file.Close()
// Attempt to download file from S3.
_, err = s3p.downloader.Download(file,
&s3.GetObjectInput{
Bucket: aws.String(s3p.bucket),
Key: aws.String(reference),
})
// Error on failed download.
if err != nil {
return err
}
return nil
}
// NewS3Provider is a convenience method to return a new *Provider instance.
func NewS3Provider(region, key, secret, bucket string) *Provider {
sess, _ := getSession(region, key, secret)
uploader := s3manager.NewUploader(sess)
downloader := s3manager.NewDownloader(sess)
return &Provider{
session: sess,
uploader: uploader,
downloader: downloader,
bucket: bucket,
}
}
// getSession establishes a new SQS session.
func getSession(region, key, secret string) (*session.Session, error) {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(region),
Credentials: credentials.NewStaticCredentials(key, secret, ""),
})
return sess, err
}