/
s3.go
60 lines (50 loc) · 1.31 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
package utils
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
type s3BufferedFile struct {
Bucket string
Path string
}
// GetBucket returns file path
func (f s3BufferedFile) GetBucket() string {
return f.Bucket
}
// GetPath returns file path
func (f s3BufferedFile) GetPath() string {
return f.Path
}
// GetBufferedContent creates a S3 session and reads the content
// of a file writing it into a buffer.
func (f s3BufferedFile) GetBufferedContent() (buf []byte, err error) {
if f.Bucket == "" {
fmt.Println("Aborting due to empty bucket")
return
}
sess := session.Must(session.NewSession())
downloader := s3manager.NewDownloader(sess)
awsBuffer := &aws.WriteAtBuffer{}
_, err = downloader.Download(awsBuffer,
&s3.GetObjectInput{
Bucket: aws.String(f.Bucket),
Key: aws.String(f.Path),
})
if err != nil {
return
}
buf = awsBuffer.Bytes()
return
}
// newBufferedFile returns a concrete type of s3BufferedFile
// properly initialized. Following recommendations found at
// https://github.com/golang/go/wiki/CodeReviewComments#interfaces
func newBufferedFile(bucket string, path string) s3BufferedFile {
return s3BufferedFile{
Bucket: bucket,
Path: path,
}
}