/
uploader.go
47 lines (39 loc) · 945 Bytes
/
uploader.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
package uploader
import (
"io"
"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"
)
// Auth is S3 credentials
type Auth struct {
AccessKeyID string
SecretAccessKey string
Region string
}
// Uploader uploads a file to an s3 location
type Uploader struct {
client *s3.S3
}
// New creates an s3 client as uploader
func New(auth Auth) (*Uploader, error) {
creds := credentials.NewStaticCredentials(
auth.AccessKeyID,
auth.SecretAccessKey,
"",
)
cfg := aws.NewConfig().WithRegion(auth.Region).WithCredentials(creds)
return &Uploader{
client: s3.New(session.New(), cfg),
}, nil
}
// Upload will PUT the s3 body
func (u *Uploader) Upload(bucket, key string, body io.ReadSeeker) error {
_, err := u.client.PutObject(&s3.PutObjectInput{
Bucket: &bucket,
Key: &key,
Body: body,
})
return err
}