-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
73 lines (66 loc) · 1.78 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
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"text/template"
"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"
"github.com/mmcdole/gofeed"
)
type S3Output struct {
Endpoint string
Region string
AccessKeyId string
AccessSecret string
Bucket string
KeyFormat string
}
func (out S3Output) Write(feed *gofeed.Feed, item gofeed.Item, identifier string) error {
// https://help.backblaze.com/hc/en-us/articles/360047629713-Using-the-AWS-Go-SDK-with-B2
// Yes, it is awful
// Also, for actual AWS, you may have things like IAM for auth
bucket := aws.String(out.Bucket)
s3Config := &aws.Config{
Credentials: credentials.NewStaticCredentials(out.AccessKeyId, out.AccessSecret, ""),
Endpoint: aws.String(out.Endpoint),
Region: aws.String(out.Region),
S3ForcePathStyle: aws.Bool(true),
}
newSession, err := session.NewSession(s3Config)
if err != nil {
return err
}
s3Client := s3.New(newSession)
data, err := json.Marshal(item)
if err != nil {
return err
}
// TODO This should probably go in initialization
pathTemplate, err := template.New(out.KeyFormat).Parse(out.KeyFormat)
if err != nil {
return err
}
var buffer bytes.Buffer
err = pathTemplate.Execute(&buffer, item)
if err != nil {
return err
}
keyPrefix := buffer.String()
key := fmt.Sprintf("%s/%s-%s.json", keyPrefix, feed.Title, identifier)
_, err = s3Client.PutObject(&s3.PutObjectInput{
Body: bytes.NewReader(data),
Bucket: bucket,
Key: &key,
})
if err != nil {
log.Printf("Failed to upload object %s/%s, %s\n", *bucket, key, err.Error())
return err
} else {
log.Printf("Successfully uploaded key %s\n", key)
}
return nil
}