-
Notifications
You must be signed in to change notification settings - Fork 65
/
_adapter_s3.go
66 lines (54 loc) · 1.32 KB
/
_adapter_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
package stm
import (
"bytes"
"compress/gzip"
"io"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
// S3Adapter provides implementation for the Adapter interface.
type S3Adapter struct {
Region string
Bucket string
ACL string
Creds *credentials.Credentials
}
// Bytes gets written content.
func (adp *S3Adapter) Bytes() [][]byte {
// TODO
return nil
}
// Write will create sitemap xml file into the s3.
func (adp *S3Adapter) Write(loc *Location, data []byte) {
var reader io.Reader = bytes.NewReader(data)
if GzipPtn.MatchString(loc.Filename()) {
var writer *io.PipeWriter
reader, writer = io.Pipe()
go func() {
gz := gzip.NewWriter(writer)
io.Copy(gz, bytes.NewReader(data))
gz.Close()
writer.Close()
}()
}
creds := adp.Creds
if creds == nil {
creds = credentials.NewEnvCredentials()
}
creds.Get()
sess := session.New(&aws.Config{
Credentials: creds, Region: &adp.Region})
uploader := s3manager.NewUploader(sess)
_, err := uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(adp.Bucket),
Key: aws.String(loc.PathInPublic()),
ACL: aws.String(adp.ACL),
Body: reader,
})
if err != nil {
log.Fatal("[F] S3 Upload file Error:", err)
}
}