-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaws.go
51 lines (41 loc) · 1 KB
/
aws.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 main
import (
"context"
"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"
"bytes"
"os"
)
func upload(ctxt context.Context, p string) error {
creds := credentials.NewStaticCredentials(screenConfig.Aws.ID, screenConfig.Aws.Secret, screenConfig.Aws.Token)
_, err := creds.Get()
if err != nil {
return err
}
cfg := aws.NewConfig().WithRegion(screenConfig.Aws.Region).WithCredentials(creds)
svc := s3.New(session.New(), cfg)
file, err := os.Open(p)
if err != nil {
return err
}
defer file.Close()
fileInfo, err := file.Stat()
if err != nil {
return err
}
size := fileInfo.Size()
buffer := make([]byte, size)
fileBytes := bytes.NewReader(buffer)
_, err = file.Read(buffer)
if err != nil {
return err
}
_, err = svc.PutObjectWithContext(ctxt, &s3.PutObjectInput{
Bucket: aws.String(screenConfig.Aws.Bucket),
Key: aws.String(p),
Body: fileBytes,
})
return err
}