-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3-upload.go
46 lines (38 loc) · 1.25 KB
/
s3-upload.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
package main
import (
"bytes"
"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"
)
func S3Upload(bucket *string, item *string, region *string, bodyBuf *bytes.Buffer) error {
Info.Printf("Uploading: s3://%s/%s (%s, %d)", *bucket, *item, *region, bodyBuf.Len())
// The session the S3 Uploader will use
session, err := session.NewSession(&aws.Config{
Region: aws.String(*region),
})
if err != nil {
return fmt.Errorf("S3 Upload Session Error: %v", err)
}
// Create an uploader with the session and custom options
uploader := s3manager.NewUploader(session, func(u *s3manager.Uploader) {
u.PartSize = 5 * 1024 * 1024 // Must be at least 5MB
})
// Upload input parameters
upParams := &s3manager.UploadInput{
ACL: aws.String("bucket-owner-full-control"),
ServerSideEncryption: aws.String("AES256"),
Bucket: bucket,
Key: item,
Body: bodyBuf,
}
// Perform an upload.
result, err := uploader.Upload(upParams)
if err != nil {
return fmt.Errorf("Unable to upload item to s3://%s/%s: %v", *bucket, *item, err)
}
Debug.Printf("Upload Result=%v", result)
return nil
}