-
Notifications
You must be signed in to change notification settings - Fork 317
/
client.go
282 lines (247 loc) · 7.14 KB
/
client.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright (c) 2018 Huawei Technologies Co., Ltd. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package multicloud
import (
"encoding/xml"
"fmt"
"net/http"
"net/url"
"path"
"strconv"
"time"
"github.com/astaxie/beego/httplib"
log "github.com/golang/glog"
"io/ioutil"
)
const (
DefaultTenantId = "adminTenantId"
DefaultTimeout = 60 // in Seconds
DefaultUploadTimeout = 30 // in Seconds
ApiVersion = "v1"
)
type AuthOptions struct {
Endpoint string
UserName string
Password string
TenantId string
}
type Client struct {
endpoint string
userName string
password string
tenantId string
version string
baseURL string
timeout time.Duration
uploadTimeout time.Duration
}
func NewClient(opt *AuthOptions, uploadTimeout int64) (*Client, error) {
u, err := url.Parse(opt.Endpoint)
if err != nil {
return nil, err
}
u.Path = path.Join(u.Path, ApiVersion)
baseURL := u.String() + "/"
return &Client{
endpoint: opt.Endpoint,
userName: opt.UserName,
password: opt.Password,
tenantId: opt.TenantId,
version: ApiVersion,
baseURL: baseURL,
timeout: time.Duration(DefaultTimeout) * time.Minute,
uploadTimeout: time.Duration(uploadTimeout) * time.Minute,
}, nil
}
type ReqSettingCB func(req *httplib.BeegoHTTPRequest) error
func (c *Client) doRequest(method, u string, in interface{}, cb ReqSettingCB) ([]byte, http.Header, error) {
req := httplib.NewBeegoRequest(u, method)
req.Header("Content-Type", "application/xml")
req.SetTimeout(c.timeout, c.timeout)
if cb != nil {
if err := cb(req); err != nil {
return nil, nil, err
}
}
if in != nil {
var body interface{}
switch in.(type) {
case string, []byte:
body = in
default:
body, _ = xml.Marshal(in)
}
req.Body(body)
}
resp, err := req.Response()
if err != nil {
log.Errorf("Do http request failed, method: %s\n url: %s\n error: %v", method, u, err)
return nil, nil, err
}
log.V(5).Infof("%s: %s OK\n", method, u)
rbody, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Errorf("Get byte[] from response failed, method: %s\n url: %s\n error: %v", method, u, err)
return nil, nil, err
}
return rbody, resp.Header, nil
}
func (c *Client) request(method, p string, in, out interface{}, cb ReqSettingCB) error {
u, err := url.Parse(p)
if err != nil {
return err
}
base, err := url.Parse(c.baseURL)
if err != nil {
return err
}
fullUrl := base.ResolveReference(u)
b, _, err := c.doRequest(method, fullUrl.String(), in, cb)
if err != nil {
return err
}
if out != nil {
log.V(5).Infof("Response:\n%s\n", string(b))
err := xml.Unmarshal(b, out)
if err != nil {
log.Errorf("unmarshal error, reason:%v", err)
return err
}
}
return nil
}
type Object struct {
ObjectKey string `xml:"ObjectKey"`
BucketName string `xml:"BucketName"`
Size uint64 `xml:"Size"`
}
type ListObjectResponse struct {
ListObjects []Object `xml:"ListObjects"`
}
type InitiateMultipartUploadResult struct {
Xmlns string `xml:"xmlns,attr"`
Bucket string `xml:"Bucket"`
Key string `xml:"Key"`
UploadId string `xml:"UploadId"`
}
type UploadPartResult struct {
Xmlns string `xml:"xmlns,attr"`
PartNumber int64 `xml:"PartNumber"`
ETag string `xml:"ETag"`
}
type Part struct {
PartNumber int64 `xml:"PartNumber"`
ETag string `xml:"ETag"`
}
type CompleteMultipartUpload struct {
Xmlns string `xml:"xmlns,attr"`
Part []Part `xml:"Part"`
}
type CompleteMultipartUploadResult struct {
Xmlns string `xml:"xmlns,attr"`
Location string `xml:"Location"`
Bucket string `xml:"Bucket"`
Key string `xml:"Key"`
ETag string `xml:"ETag"`
}
func (c *Client) UploadObject(bucketName, objectKey string, data []byte) error {
p := path.Join("s3", bucketName, objectKey)
if err := c.request("PUT", p, data, nil, nil); err != nil {
return err
}
return nil
}
func (c *Client) ListObject(bucketName string) (*ListObjectResponse, error) {
p := path.Join("s3", bucketName)
object := &ListObjectResponse{}
if err := c.request("GET", p, nil, object, nil); err != nil {
return nil, err
}
return object, nil
}
func (c *Client) RemoveObject(bucketName, objectKey string) error {
p := path.Join("s3", bucketName, objectKey)
if err := c.request("DELETE", p, nil, nil, nil); err != nil {
return err
}
return nil
}
func (c *Client) InitMultiPartUpload(bucketName, objectKey string) (*InitiateMultipartUploadResult, error) {
p := path.Join("s3", bucketName, objectKey)
p += "?uploads"
out := &InitiateMultipartUploadResult{}
if err := c.request("PUT", p, nil, out, nil); err != nil {
return nil, err
}
return out, nil
}
func (c *Client) UploadPart(bucketName, objectKey string, partNum int64, uploadId string, data []byte, size int64) (*UploadPartResult, error) {
log.Infof("upload part buf size:%d", len(data))
p := path.Join("s3", bucketName, objectKey)
p += fmt.Sprintf("?partNumber=%d&uploadId=%s", partNum, uploadId)
out := &UploadPartResult{}
reqSettingCB := func(req *httplib.BeegoHTTPRequest) error {
req.Header("Content-Length", strconv.FormatInt(size, 10))
req.SetTimeout(c.uploadTimeout, c.uploadTimeout)
return nil
}
if err := c.request("PUT", p, data, out, reqSettingCB); err != nil {
return nil, err
}
return out, nil
}
func (c *Client) CompleteMultipartUpload(
bucketName string,
objectKey string,
uploadId string,
input *CompleteMultipartUpload) (*CompleteMultipartUploadResult, error) {
p := path.Join("s3", bucketName, objectKey)
p += fmt.Sprintf("?uploadId=%s", uploadId)
out := &CompleteMultipartUploadResult{}
if err := c.request("PUT", p, input, nil, nil); err != nil {
return nil, err
}
return out, nil
}
func (c *Client) AbortMultipartUpload(bucketName, objectKey string) error {
// TODO: multi-cloud has not implemented it yet. so just comment it.
//p := path.Join("s3", "AbortMultipartUpload", bucketName, objectKey)
//if err := c.request("DELETE", p, nil, nil); err != nil {
// return err
//}
return nil
}
func (c *Client) DownloadPart(bucketName, objectKey string, offset, size int64) ([]byte, error) {
p := path.Join("s3", bucketName, objectKey)
reqSettingCB := func(req *httplib.BeegoHTTPRequest) error {
rangeStr := fmt.Sprintf("bytes:%d-%d", offset, offset+size-1)
req.Header("Range", rangeStr)
req.SetTimeout(c.uploadTimeout, c.uploadTimeout)
return nil
}
u, err := url.Parse(p)
if err != nil {
return nil, err
}
base, err := url.Parse(c.baseURL)
if err != nil {
return nil, err
}
fullUrl := base.ResolveReference(u)
body, _, err := c.doRequest("GET", fullUrl.String(), nil, reqSettingCB)
if err != nil {
return nil, err
}
return body, nil
}