-
Notifications
You must be signed in to change notification settings - Fork 0
/
ul.go
61 lines (51 loc) · 1.42 KB
/
ul.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
package s3
import (
"bytes"
"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"
)
// Upload starts the uploading of a binary object to the location given by
// path in the bucket specified by the configuration. It returns the object
// location in the bucket and an error value.
func Upload(c BucketConf, path string, data []byte) (string, error) {
ul := NewUploader(c)
return ul.Upload(data, path)
}
// Uploader is the interface that wraps the Upload method.
type Uploader interface {
Upload(data []byte, path string) (string, error)
}
type s3Uploader struct {
bucket string
s3Client *s3.S3
uloader *s3manager.Uploader
}
// NewUploader creates and initializes a new Uploader based on the values
// contained in BucketConf.
func NewUploader(c BucketConf) Uploader {
s3Client := s3.New(session.New(), awsConfig(c.ID, c.Secret, c.Region))
uloader := &s3manager.Uploader{
PartSize: 1024 * 1024 * 20,
MaxUploadParts: 100,
Concurrency: 15,
S3: s3Client,
}
return s3Uploader{
bucket: c.Bucket,
s3Client: s3Client,
uloader: uloader,
}
}
func (u s3Uploader) Upload(data []byte, key string) (string, error) {
params := &s3manager.UploadInput{
Bucket: &u.bucket,
Key: &key,
Body: bytes.NewBuffer(data),
}
res, err := u.uloader.Upload(params)
if err != nil {
return "", err
}
return res.Location, nil
}