-
Notifications
You must be signed in to change notification settings - Fork 17
/
s3.go
125 lines (111 loc) · 3.09 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
116
117
118
119
120
121
122
123
124
125
package s3
import (
"bytes"
"context"
"io"
"mime"
"net/http"
"path/filepath"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"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/ulule/gostorages"
)
// Storage is a s3 storage.
type Storage struct {
bucket string
s3 *s3.S3
uploader *s3manager.Uploader
}
// NewStorage returns a new Storage.
func NewStorage(cfg Config) (*Storage, error) {
s, err := session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentialsFromCreds(credentials.Value{
AccessKeyID: cfg.AccessKeyID,
SecretAccessKey: cfg.SecretAccessKey,
}),
Region: aws.String(cfg.Region),
})
if err != nil {
return nil, err
}
return &Storage{
bucket: cfg.Bucket,
s3: s3.New(s),
uploader: s3manager.NewUploader(s),
}, nil
}
// Config is the configuration for Storage.
type Config struct {
AccessKeyID string
SecretAccessKey string
Region string
Bucket string
}
// Save saves content to path.
func (s *Storage) Save(ctx context.Context, content io.Reader, path string) error {
input := &s3manager.UploadInput{
ACL: aws.String(s3.ObjectCannedACLPublicRead),
Body: content,
Bucket: aws.String(s.bucket),
Key: aws.String(path),
}
contenttype := mime.TypeByExtension(filepath.Ext(path)) // first, detect content type from extension
if contenttype == "" {
// second, detect content type from first 512 bytes of content
data := make([]byte, 512)
if _, err := content.Read(data); err != nil {
return err
}
contenttype = http.DetectContentType(data)
input.Body = io.MultiReader(bytes.NewReader(data), content)
}
if contenttype != "" {
input.ContentType = aws.String(contenttype)
}
_, err := s.uploader.UploadWithContext(ctx, input)
return err
}
// Stat returns path metadata.
func (s *Storage) Stat(ctx context.Context, path string) (*gostorages.Stat, error) {
input := &s3.HeadObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(path),
}
out, err := s.s3.HeadObjectWithContext(ctx, input)
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == "NotFound" {
return nil, gostorages.ErrNotExist
} else if err != nil {
return nil, err
}
return &gostorages.Stat{
ModifiedTime: *out.LastModified,
Size: *out.ContentLength,
}, nil
}
// Open opens path for reading.
func (s *Storage) Open(ctx context.Context, path string) (io.ReadCloser, error) {
input := &s3.GetObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(path),
}
out, err := s.s3.GetObjectWithContext(ctx, input)
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == s3.ErrCodeNoSuchKey {
return nil, gostorages.ErrNotExist
} else if err != nil {
return nil, err
}
return out.Body, nil
}
// Delete deletes path.
func (s *Storage) Delete(ctx context.Context, path string) error {
input := &s3.DeleteObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(path),
}
_, err := s.s3.DeleteObjectWithContext(ctx, input)
return err
}