forked from nyaruka/courier
-
Notifications
You must be signed in to change notification settings - Fork 2
/
s3.go
43 lines (36 loc) · 1.1 KB
/
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
40
41
42
43
package utils
import (
"bytes"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
)
var s3BucketURL = "https://%s.s3.amazonaws.com%s"
// TestS3 tests whether the passed in s3 client is properly configured and the passed in bucket is accessible
func TestS3(s3Client s3iface.S3API, bucket string) error {
params := &s3.HeadBucketInput{
Bucket: aws.String(bucket),
}
_, err := s3Client.HeadBucket(params)
if err != nil {
return err
}
return nil
}
// PutS3File writes the passed in file to the bucket with the passed in content type
func PutS3File(s3Client s3iface.S3API, bucket string, path string, contentType string, contents []byte) (string, error) {
params := &s3.PutObjectInput{
Bucket: aws.String(bucket),
Body: bytes.NewReader(contents),
Key: aws.String(path),
ContentType: aws.String(contentType),
ACL: aws.String(s3.BucketCannedACLPublicRead),
}
_, err := s3Client.PutObject(params)
if err != nil {
return "", err
}
url := fmt.Sprintf(s3BucketURL, bucket, path)
return url, nil
}