/
driver_s3.go
137 lines (117 loc) · 2.82 KB
/
driver_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
package fsabstract
import (
"errors"
"github.com/mitchellh/goamz/aws"
"github.com/mitchellh/goamz/s3"
"strconv"
"time"
)
func init() {
FileStoreDriverMap["s3"] = func() FileStoreDriver {
return new(FSS3)
}
}
// FSS3 is an AWS S3 driver.
type FSS3 struct {
BucketName string `fsdconfig:"fs.s3.bucket"`
AccessKey string `fsdconfig:"fs.s3.accesskey"`
SecretKey string `fsdconfig:"fs.s3.secretkey"`
RegionObj aws.Region `fsdconfig:"fs.s3.region"`
auth *aws.Auth
conn *s3.S3
bucket *s3.Bucket
}
func (self *FSS3) DriverName() string {
return "s3"
}
func (self *FSS3) Configure(c map[string]string) {
if v, exists := c["fs.s3.bucket"]; exists {
self.BucketName = v
}
if v, exists := c["fs.s3.accesskey"]; exists {
self.AccessKey = v
}
if v, exists := c["fs.s3.secretkey"]; exists {
self.SecretKey = v
}
if v, exists := c["fs.s3.region"]; exists {
if _, exists = aws.Regions[v]; exists {
self.RegionObj = aws.Regions[v]
} else {
panic("Unable to resolve S3 region " + v)
}
}
}
func (self *FSS3) Initialize() error {
auth, err := aws.GetAuth(self.AccessKey, self.SecretKey)
if err != nil {
return err
}
self.auth = &auth
conn := s3.New(*self.auth, self.RegionObj)
self.conn = conn
if self.conn == nil {
return errors.New("Unable to initialize s3 driver")
}
bucket := self.conn.Bucket(self.BucketName)
self.bucket = bucket
return nil
}
func (self *FSS3) Get(d FileStoreDescriptor) ([]byte, FileStoreLocation, error) {
// Find the pertinent FileStoreLocation
l, err := LocationForDriver(d, self.DriverName())
if err != nil {
return nil, FileStoreLocation{}, err
}
// Retrieve actual file data from disk
c, err := self.bucket.Get(l.Location)
if err != nil {
return nil, l, err
}
// Send everything back
return c, l, nil
}
func (self *FSS3) Put(d FileStoreDescriptor, c []byte) (FileStoreDescriptor, error) {
dU := d
// Create new location
k := "fs_" + strconv.FormatInt(dU.Id, 16) + "_" + dU.Name
l := FileStoreLocation{
Id: "", // dummy driver doesn't have a store name/id
Driver: self.DriverName(),
Created: time.Now(),
Location: k,
}
// Push out to filesystem
err := self.bucket.Put(
k,
c,
d.Type,
s3.BucketOwnerFull)
if err != nil {
return dU, err
}
// Append location
if dU.Location == nil {
dU.Location = make([]FileStoreLocation, 0)
}
dU.Location = append(dU.Location, l)
// No errors, send back
return dU, nil
}
func (self *FSS3) Delete(d FileStoreDescriptor, l FileStoreLocation) (FileStoreDescriptor, error) {
dU := d
// Find the pertinent FileStoreLocation
l, err := LocationForDriver(dU, self.DriverName())
if err != nil {
return dU, err
}
// Delete from disk
err = self.bucket.Del(l.Location)
if err != nil {
return dU, err
}
// Remove from mapping
RemoveLocation(dU, l)
// No errors, send back
return dU, nil
}