-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaliyun_adapter.go
204 lines (165 loc) · 4.87 KB
/
aliyun_adapter.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package storage
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"regexp"
"strings"
"time"
oss "github.com/aliyun/aliyun-oss-go-sdk/oss"
)
// AliyunAdapter : The adapter empty because they do not have any data to share for now
type AliyunAdapter struct {
Endpoint string
AccessKeyID string
AccessKeySecret string
}
// UploadFile : Upload file to the bucket
func (adapter *AliyunAdapter) UploadFile(file *multipart.FileHeader, bucket, filename string) (string, error) {
var reader io.Reader
name := ""
fileExt := strings.ToLower(strings.Split(file.Header.Get("Content-Type"), "/")[1])
if ext, isExist := extensionMapper[fileExt]; isExist {
name = fmt.Sprintf("%s.%s", filename, ext)
} else {
name = fmt.Sprintf("%s.%s", filename, fileExt)
}
src, err := file.Open()
if err != nil {
return "", err
}
defer src.Close()
reader = src
fileURL, err := adapter.UploadReader(bucket, name, reader, strings.ToLower(fileExt))
if err != nil {
return "", err
}
return fileURL, nil
}
// DeleteFileUsingURL : Delete file from the bucket using url
func (adapter *AliyunAdapter) DeleteFileUsingURL(bucket, fileURL string) error {
storageClient, err := adapter.getClient()
if err != nil {
return err
}
object, err := storageClient.Bucket(bucket)
if err != nil {
return err
}
filepath := adapter.getFilePathFromURL(bucket, fileURL)
return object.DeleteObject(filepath)
}
// TemporaryServingFile : TemporaryServingFile file serving
func (adapter *AliyunAdapter) TemporaryServingFile(bucket, fileURL string, expiredDateTime time.Time, aliClient interface{}) (string, error) {
storageClient, err := adapter.getClient()
if err != nil {
return "", err
}
object, err := storageClient.Bucket(bucket)
if err != nil {
return "", err
}
filepath := adapter.getFilePathFromURL(bucket, fileURL)
url, err := object.SignURL(filepath, http.MethodGet, int64(expiredDateTime.UTC().Sub(time.Now().UTC()).Seconds()))
if err != nil {
return "", err
}
return url, nil
}
// UploadReader :
func (adapter *AliyunAdapter) UploadReader(bucket, filename string, reader io.Reader, contentType string) (string, error) {
storageClient, err := adapter.getClient()
if err != nil {
return "", err
}
object, err := storageClient.Bucket(bucket)
if err != nil {
return "", err
}
options := make([]oss.Option, 0)
if contentType == "" {
options = aliyunContentTypeAny(filename)
} else {
contentFunc, isExist := aliyunContentTypeMapper[contentType]
if !isExist {
return "", fmt.Errorf("Content type %s does not supported", contentType)
}
options = contentFunc(filename)
}
if err := object.PutObject(filename, reader, options...); err != nil {
msg := fmt.Sprintf("Could not write file: %v", err)
return "", errors.New(msg)
}
return getAliyunFileURL(adapter.Endpoint, bucket, filename), nil
}
// ReadFile :
func (adapter *AliyunAdapter) ReadFile(bucket, path string) ([]byte, error) {
storageClient, err := adapter.getClient()
if err != nil {
return nil, err
}
object, err := storageClient.Bucket(bucket)
if err != nil {
return nil, err
}
rc, err := object.GetObject(path)
if err != nil {
return nil, err
}
defer rc.Close()
slurp, err := ioutil.ReadAll(rc)
if err != nil {
return nil, err
}
return slurp, nil
}
// UploadBuffer :
func (adapter *AliyunAdapter) UploadBuffer(bucket, filename string, contentType string) (*Buffer, error) {
buf := new(Buffer)
buf.adapter = ALIYUN
storageClient, err := adapter.getClient()
if err != nil {
return nil, err
}
object, err := storageClient.Bucket(bucket)
if err != nil {
return nil, err
}
contentFunc, isExist := aliyunContentTypeMapper[contentType]
if !isExist {
return nil, fmt.Errorf("Content type %s does not supported", contentType)
}
options := contentFunc(filename)
buffer := new(bytes.Buffer)
// delete the object before append
object.DeleteObject(filename)
position, err := object.AppendObject(filename, buffer, buf.position, options...)
if err != nil {
log.Println("error: ", err)
return nil, err
}
buf.position = position
buf.position = position
buf.object = object
buf.filename = filename
buf.bucket = bucket
buf.endpoint = adapter.Endpoint
return buf, nil
}
func (adapter *AliyunAdapter) getClient() (*oss.Client, error) {
return oss.New(adapter.Endpoint, adapter.AccessKeyID, adapter.AccessKeySecret)
}
func getAliyunFileURL(endpoint, bucket string, filename string) string {
endpoint = regexp.MustCompile(`^(http|https)://`).ReplaceAllString(endpoint, "")
return fmt.Sprintf("https://%s.%s/%s", bucket, endpoint, filename)
}
func (adapter *AliyunAdapter) getFilePathFromURL(bucket, fileURL string) string {
endpoint := regexp.MustCompile(`^(http|https)://`).ReplaceAllString(adapter.Endpoint, "")
f := regexp.MustCompile(`^(http|https)://`).ReplaceAllString(fileURL, "")
return strings.Replace(f, fmt.Sprintf("%s.%s/", bucket, endpoint), "", -1)
}