forked from keighl/barkup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3.go
61 lines (51 loc) · 1.25 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
package barkup
import (
"bufio"
"launchpad.net/goamz/aws"
"launchpad.net/goamz/s3"
"os"
)
// S3 is a `Storer` interface that puts an ExportResult to the specified S3 bucket. Don't use your main AWS keys for this!! Create read-only keys using IAM
type S3 struct {
// Available regions:
// * us-east-1
// * us-west-1
// * us-west-2
// * eu-west-1
// * ap-southeast-1
// * ap-southeast-2
// * ap-northeast-1
// * sa-east-1
Region string
// Name of the bucjet
Bucket string
// AWS S3 access key
AccessKey string
// AWS S3 secret
ClientSecret string
}
// Store puts an `ExportResult` struct to an S3 bucket within the specified directory
func (x *S3) Store(result *ExportResult, directory string) *Error {
if result.Error != nil {
return result.Error
}
file, err := os.Open(result.Path)
if err != nil {
return makeErr(err, "")
}
defer file.Close()
buffy := bufio.NewReader(file)
stat, err := file.Stat()
if err != nil {
return makeErr(err, "")
}
size := stat.Size()
auth := aws.Auth{
AccessKey: x.AccessKey,
SecretKey: x.ClientSecret,
}
s := s3.New(auth, aws.Regions[x.Region])
bucket := s.Bucket(x.Bucket)
err = bucket.PutReader(directory+result.Filename(), buffy, size, result.MIME, s3.BucketOwnerFull)
return makeErr(err, "")
}