summaryrefslogtreecommitdiff
path: root/imagestore/s3store.go
diff options
context:
space:
mode:
Diffstat (limited to 'imagestore/s3store.go')
-rw-r--r--imagestore/s3store.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/imagestore/s3store.go b/imagestore/s3store.go
new file mode 100644
index 0000000..e023fcc
--- /dev/null
+++ b/imagestore/s3store.go
@@ -0,0 +1,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)
+}