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
|
package imagestore
import (
"github.com/mitchellh/goamz/s3"
"io/ioutil"
)
type S3ImageStore struct {
bucketName string
storeRoot string
client *s3.S3
namePathMapper *NamePathMapper
}
func NewS3ImageStore(bucket string, root string, client *s3.S3, mapper *NamePathMapper) *S3ImageStore {
return &S3ImageStore{
bucketName: bucket,
storeRoot: root,
client: client,
namePathMapper: mapper,
}
}
func (this *S3ImageStore) Exists(obj *StoreObject) (bool, error) {
bucket := this.client.Bucket(this.bucketName)
response, err := bucket.Head(this.toPath(obj))
if err != nil {
return false, err
}
return (response.StatusCode == 200), nil
}
func (this *S3ImageStore) Save(src string, obj *StoreObject) (*StoreObject, error) {
bucket := this.client.Bucket(this.bucketName)
data, err := ioutil.ReadFile(src)
if err != nil {
return nil, err
}
err = bucket.Put(this.toPath(obj), data, obj.MimeType, s3.PublicReadWrite)
if err != nil {
return nil, err
}
obj.Url = "https://s3.amazonaws.com/" + this.bucketName + this.toPath(obj)
return obj, nil
}
func (this *S3ImageStore) toPath(obj *StoreObject) string {
return this.storeRoot + "/" + this.namePathMapper.mapToPath(obj)
}
|