-
Notifications
You must be signed in to change notification settings - Fork 7
/
store_messages.go
74 lines (58 loc) · 1.87 KB
/
store_messages.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
74
package main
import (
"bytes"
"compress/gzip"
"fmt"
log "github.com/cihub/seelog"
"github.com/goamz/goamz/aws"
"github.com/goamz/goamz/s3"
"os"
"time"
)
// Print messages to the screen:
func PrintMessages(fileData []byte) error {
fileName := fmt.Sprintf("%v/%v/%v/%v/%v/%v.%v.gz", *s3Path, time.Now().Year(), time.Now().Month(), time.Now().Day(), time.Now().Hour(), time.Now().Minute(), *s3FileExtention)
log.Infof("Would store in '%v'", fileName)
log.Debugf("Messages: %v", string(fileData))
return nil
}
// Store messages to S3:
func StoreMessages(fileData []byte) error {
// Something to compress the fileData into:
var fileDataBytes bytes.Buffer
gzFileData := gzip.NewWriter(&fileDataBytes)
gzFileData.Write(fileData)
gzFileData.Close()
log.Infof("Storing %d bytes...", len(fileDataBytes.Bytes()))
// Authenticate with AWS:
awsAuth, err := aws.GetAuth("", "", "", time.Now())
if err != nil {
log.Criticalf("Unable to authenticate to AWS! (%s) ...\n", err)
os.Exit(2)
} else {
log.Debugf("Authenticated to AWS")
}
// Make a new S3 connection:
log.Debugf("Connecting to AWS...")
s3Connection := s3.New(awsAuth, aws.Regions[*awsRegion])
// Make a bucket object:
s3Bucket := s3Connection.Bucket(*s3Bucket)
// Prepare arguments for the call to store messages on S3:
contType := "text/plain"
perm := s3.BucketOwnerFull
options := &s3.Options{
SSE: false,
Meta: nil,
}
// Build the filename we'll use for S3:
fileName := fmt.Sprintf("%v/%v/%v/%v/%v/%v.%v.gz", *s3Path, time.Now().Year(), time.Now().Month(), time.Now().Day(), time.Now().Hour(), time.Now().Minute(), *s3FileExtention)
// Upload the data:
err = s3Bucket.Put(fileName, fileDataBytes.Bytes(), contType, perm, *options)
if err != nil {
log.Criticalf("Failed to put file (%v) on S3 (%v)", fileName, err)
os.Exit(2)
} else {
log.Infof("Stored file (%v) on s3", fileName)
}
return nil
}