This repository has been archived by the owner on Sep 27, 2021. It is now read-only.
forked from ulule/gostorages
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3.go
195 lines (147 loc) · 3.58 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package gostorages
import (
"io"
"io/ioutil"
"mime"
"time"
"github.com/lizdeika/goamz/aws"
"github.com/lizdeika/goamz/s3"
)
var ACLs = map[string]s3.ACL{
"private": s3.Private,
"public-read": s3.PublicRead,
"public-read-write": s3.PublicReadWrite,
"authenticated-read": s3.AuthenticatedRead,
"bucket-owner-read": s3.BucketOwnerRead,
"bucket-owner-full-control": s3.BucketOwnerFull,
}
const LastModifiedFormat = time.RFC1123
func NewS3Storage(accessKeyId string, secretAccessKey string, bucketName string, location string, region aws.Region, acl s3.ACL, baseURL string) Storage {
return &S3Storage{
NewBaseStorage(location, baseURL),
accessKeyId,
secretAccessKey,
bucketName,
region,
acl,
}
}
type S3Storage struct {
*BaseStorage
AccessKeyId string
SecretAccessKey string
BucketName string
Region aws.Region
ACL s3.ACL
}
type S3StorageFile struct {
io.ReadCloser
Key *s3.Key
Storage Storage
}
func (f *S3StorageFile) Size() int64 {
return f.Key.Size
}
func (f *S3StorageFile) ReadAll() ([]byte, error) {
return ioutil.ReadAll(f)
}
// Auth returns a Auth instance
func (s *S3Storage) Auth() (auth aws.Auth, err error) {
return aws.GetAuth(s.AccessKeyId, s.SecretAccessKey)
}
// Client returns a S3 instance
func (s *S3Storage) Client() (*s3.S3, error) {
auth, err := s.Auth()
if err != nil {
return nil, err
}
return s3.New(auth, s.Region), nil
}
// Bucket returns a bucket instance
func (s *S3Storage) Bucket() (*s3.Bucket, error) {
client, err := s.Client()
if err != nil {
return nil, err
}
return client.Bucket(s.BucketName), nil
}
// Open returns the file content in a dedicated bucket
func (s *S3Storage) Open(filepath string) (File, error) {
bucket, err := s.Bucket()
if err != nil {
return nil, err
}
key, err := s.Key(filepath)
if err != nil {
return nil, err
}
body, err := bucket.GetReader(s.Path(filepath))
if err != nil {
return nil, err
}
return &S3StorageFile{
body,
key,
s,
}, nil
}
// Delete the file from the bucket
func (s *S3Storage) Delete(filepath string) error {
bucket, err := s.Bucket()
if err != nil {
return err
}
return bucket.Del(s.Path(filepath))
}
func (s *S3Storage) Key(filepath string) (*s3.Key, error) {
bucket, err := s.Bucket()
if err != nil {
return nil, err
}
key, err := bucket.GetKey(s.Path(filepath))
if err != nil {
return nil, err
}
return key, nil
}
// Exists checks if the given file is in the bucket
func (s *S3Storage) Exists(filepath string) bool {
_, err := s.Key(filepath)
if err != nil {
return false
}
return true
}
// Save saves a file at the given path in the bucket
func (s *S3Storage) SaveWithContentType(filepath string, file File, contentType string) error {
bucket, err := s.Bucket()
if err != nil {
return err
}
content, err := file.ReadAll()
if err != nil {
return err
}
err = bucket.Put(s.Path(filepath), content, contentType, s.ACL)
return err
}
// Save saves a file at the given path in the bucket
func (s *S3Storage) Save(filepath string, file File) error {
return s.SaveWithContentType(filepath, file, mime.TypeByExtension(filepath))
}
// Size returns the size of the given file
func (s *S3Storage) Size(filepath string) int64 {
key, err := s.Key(filepath)
if err != nil {
return 0
}
return key.Size
}
// ModifiedTime returns the last update time
func (s *S3Storage) ModifiedTime(filepath string) (time.Time, error) {
key, err := s.Key(filepath)
if err != nil {
return time.Time{}, err
}
return time.Parse(LastModifiedFormat, key.LastModified)
}