/
s3_ops.go
73 lines (67 loc) · 1.79 KB
/
s3_ops.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
package job
import (
"fmt"
"github.com/PeopleAdmin/pdfcombiner/testmode"
"io/ioutil"
"launchpad.net/goamz/aws"
"launchpad.net/goamz/s3"
"log"
"os"
)
var (
awsRegion = aws.USEast
uploadedFilePermission = s3.Private
)
// Get retrieves the requested document, either from S3 or by decoding the
// embedded `Data` attribute of the Document.
func (doc *Document) Get() (docContent []byte, err error) {
log.Printf("%s GET s3://%s/%s\n", doc.Id(), doc.parent.BucketName, doc.s3Path())
if testmode.IsEnabled() {
return
}
switch doc.Data {
case "":
return doc.parent.download(doc)
default:
docContent, err = decodeEmbeddedData(doc.Data)
doc.Data = ""
doc.FileSize = len(docContent)
return
}
}
// UploadCombinedFile sends a file to the job's CombinedKey on S3.
func (j *Job) UploadCombinedFile() (err error) {
log.Printf("%s PUT s3://%s/%s\n", j.Id(), j.BucketName, j.CombinedKey)
if testmode.IsEnabled() {
return
}
content, err := ioutil.ReadFile(j.LocalPath())
if err != nil || len(content) == 0 {
j.AddError(fmt.Errorf("reading file '%v' for upload: %v", j.LocalPath(), err))
return
}
err = j.bucket.Put(j.CombinedKey, content, "application/pdf", uploadedFilePermission)
if err != nil {
j.AddError(fmt.Errorf("uploading to S3 key '%s': %v", j.CombinedKey, err))
return
}
j.uploadComplete = true
return
}
// Connect to AWS and add the handle to the Job object.
func (j *Job) connect() (err error) {
auth, err := aws.EnvAuth()
if err != nil {
println("S3 connect failed due to auth issues, exiting!")
os.Exit(1)
}
s := s3.New(auth, awsRegion)
j.bucket = s.Bucket(j.BucketName)
return
}
func (j *Job) download(doc *Document) (content []byte, err error) {
remotePath := doc.s3Path()
content, err = j.bucket.Get(remotePath)
doc.FileSize = len(content)
return
}