-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
39 lines (31 loc) · 953 Bytes
/
s3.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
package example
import (
"bytes"
"net/http"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
// S3PutObject Upload file to S3
func S3PutObject(path string, file []byte) (output *s3.PutObjectOutput, err error) {
awsEndpoint := os.Getenv("AWS_ENDPOINT")
awsRegion := os.Getenv("AWS_REGION")
s3Bucket := os.Getenv("S3_BUCKET")
s := session.Must(session.NewSession(&aws.Config{
S3ForcePathStyle: aws.Bool(true),
Endpoint: aws.String(awsEndpoint),
Region: aws.String(awsRegion),
}))
c := s3.New(s, &aws.Config{})
fileContentType := http.DetectContentType(file)
reader := bytes.NewReader(file)
input := &s3.PutObjectInput{
Bucket: aws.String(s3Bucket),
Key: aws.String(path),
Body: reader,
ContentType: aws.String(fileContentType),
ContentLength: aws.Int64(reader.Size()),
}
return c.PutObject(input)
}