-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3_writer.go
50 lines (46 loc) · 1.5 KB
/
s3_writer.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
package queues
import (
"bytes"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/sirupsen/logrus"
"net/http"
)
// func Write(fileName string, content string) error {
// if err := AddFileToS3(fileName, content); ErrorHandler(err) {
// log.Fatal(err)
// return err
// }
// return nil
// }
// AddFileToS3 will upload a single file to S3, it will require a pre-built aws session
// and will set file info like content type and encryption on the uploaded file.
func (q *Queue) AddFileToS3(fileName string, content string) error {
// Get file size and read the file content into a buffer
var size = len(content)
var b bytes.Buffer
if _, err := fmt.Fprintf(&b, content); err != nil {
return err
}
// fmt.Printf("Writing:\n%v\n", b.String())
// Config settings: this is where you choose the bucket, filename, content-type etc.
// of the file you're uploading.
if sess, err := q.getSession(); err != nil {
logrus.Error("Unable to get session ", err)
panic(err)
} else {
if _, err := s3.New(sess).PutObject(&s3.PutObjectInput{
Bucket: aws.String(q.Config.GdprBucket),
Key: aws.String(fileName),
ACL: aws.String("private"),
Body: bytes.NewReader(b.Bytes()),
ContentLength: aws.Int64(int64(size)),
ContentType: aws.String(http.DetectContentType(b.Bytes())),
ContentDisposition: aws.String("application/octet-stream"),
}); ErrorHandler(err) {
return err
}
return nil
}
}