-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
51 lines (42 loc) · 1.22 KB
/
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
47
48
49
50
51
package hkpg
import (
"log"
"os"
"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"
)
// Upload accepts a *os.File, uploads it to the specified S3 buckets and
// returns the ETag of the uploaded file. Upload can be called multiple times
// with the same file name.
func Upload(file *os.File) (string, error) {
var bucketName = os.Getenv("S3_BUCKET_NAME")
if bucketName == "" {
log.Fatalf("S3_BUCKET_NAME must be set")
}
creds := credentials.NewEnvCredentials()
_, err := creds.Get()
if err != nil {
log.Fatalf("bad credentials: %s", err)
}
var awsRegion = os.Getenv("AWS_REGION")
if awsRegion == "" {
awsRegion = "us-west-1"
}
cfg := aws.NewConfig().WithRegion(awsRegion).WithCredentials(creds)
sesh := session.Must(session.NewSession(cfg))
uploader := s3manager.NewUploader(sesh, func(u *s3manager.Uploader) {
u.PartSize = 64 * 1024 * 1024 // 64MB per part
})
uploadInput := &s3manager.UploadInput{
Bucket: &bucketName,
Key: aws.String(file.Name()),
Body: file,
}
result, err := uploader.Upload(uploadInput)
if err != nil {
log.Fatalf("%v", err)
}
return result.Location, nil
}