/
awsS3Provider.go
112 lines (100 loc) · 3.02 KB
/
awsS3Provider.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package storage
import (
"bytes"
"io/ioutil"
"log"
"net/http"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awsutil"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/swagchat/chat-api/models"
"github.com/swagchat/chat-api/utils"
)
type AwsS3StorageProvider struct {
accessKeyId string
secretAccessKey string
region string
acl string
uploadBucket string
uploadDirectory string
thumbnailBucket string
thumbnailDirectory string
}
func (provider AwsS3StorageProvider) Init() error {
awsS3Client, err := provider.getSession()
if err != nil {
return err
}
params := &s3.CreateBucketInput{
Bucket: aws.String(provider.uploadBucket),
}
createBucketResp, err := awsS3Client.CreateBucket(params)
if err != nil {
return err
}
log.Printf("Created bucket %#v", awsutil.StringValue(createBucketResp))
params = &s3.CreateBucketInput{
Bucket: aws.String(provider.thumbnailBucket),
}
createBucketResp, err = awsS3Client.CreateBucket(params)
if err != nil {
return err
}
log.Printf("Created bucket %#v", awsutil.StringValue(createBucketResp))
return nil
}
func (provider AwsS3StorageProvider) Post(assetInfo *AssetInfo) (string, *models.ProblemDetail) {
awsS3Client, err := provider.getSession()
if err != nil {
return "", &models.ProblemDetail{
Title: "Create session failed. (Amazon S3)",
Status: http.StatusInternalServerError,
ErrorName: "storage-error",
Detail: err.Error(),
}
}
byteData, err := ioutil.ReadAll(assetInfo.Data)
if err != nil {
return "", &models.ProblemDetail{
Title: "Reading asset data failed. (Amazon S3)",
Status: http.StatusInternalServerError,
ErrorName: "storage-error",
Detail: err.Error(),
}
}
data := bytes.NewReader(byteData)
filePath := utils.AppendStrings(provider.uploadDirectory, "/", assetInfo.FileName)
putObjectResp, err := awsS3Client.PutObject(&s3.PutObjectInput{
Bucket: aws.String(provider.uploadBucket),
Key: aws.String(filePath),
Body: data,
ACL: &provider.acl,
})
if err != nil {
return "", &models.ProblemDetail{
Title: "Create object failed. (Amazon S3)",
Status: http.StatusInternalServerError,
ErrorName: "storage-error",
Detail: err.Error(),
}
}
log.Println("Created object %#v", awsutil.StringValue(putObjectResp))
sourceUrl := utils.AppendStrings("https://s3-ap-northeast-1.amazonaws.com/", provider.uploadBucket, "/", filePath)
log.Println("sourceUrl:", sourceUrl)
return sourceUrl, nil
}
func (provider AwsS3StorageProvider) Get(assetInfo *AssetInfo) ([]byte, *models.ProblemDetail) {
return nil, nil
}
func (provider AwsS3StorageProvider) getSession() (*s3.S3, error) {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(provider.region),
Credentials: credentials.NewStaticCredentials(provider.accessKeyId, provider.secretAccessKey, ""),
})
if err != nil {
return nil, err
}
return s3.New(sess), nil
}