-
Notifications
You must be signed in to change notification settings - Fork 7
/
client_part.go
252 lines (231 loc) · 8.34 KB
/
client_part.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
// Copyright 2019 Huawei Technologies Co.,Ltd.
// 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 obs
import (
"errors"
"io"
"os"
"sort"
"strings"
)
// ListMultipartUploads lists the multipart uploads.
//
// You can use this API to list the multipart uploads that are initialized but not combined or aborted in a specified bucket.
func (obsClient ObsClient) ListMultipartUploads(input *ListMultipartUploadsInput, extensions ...extensionOptions) (output *ListMultipartUploadsOutput, err error) {
if input == nil {
return nil, errors.New("ListMultipartUploadsInput is nil")
}
output = &ListMultipartUploadsOutput{}
err = obsClient.doActionWithBucket("ListMultipartUploads", HTTP_GET, input.Bucket, input, output, extensions)
if err != nil {
output = nil
} else if output.EncodingType == "url" {
err = decodeListMultipartUploadsOutput(output)
if err != nil {
doLog(LEVEL_ERROR, "Failed to get ListMultipartUploadsOutput with error: %v.", err)
output = nil
}
}
return
}
// AbortMultipartUpload aborts a multipart upload in a specified bucket by using the multipart upload ID.
func (obsClient ObsClient) AbortMultipartUpload(input *AbortMultipartUploadInput, extensions ...extensionOptions) (output *BaseModel, err error) {
if input == nil {
return nil, errors.New("AbortMultipartUploadInput is nil")
}
if input.UploadId == "" {
return nil, errors.New("UploadId is empty")
}
output = &BaseModel{}
err = obsClient.doActionWithBucketAndKey("AbortMultipartUpload", HTTP_DELETE, input.Bucket, input.Key, input, output, extensions)
if err != nil {
output = nil
}
return
}
// InitiateMultipartUpload initializes a multipart upload.
func (obsClient ObsClient) InitiateMultipartUpload(input *InitiateMultipartUploadInput, extensions ...extensionOptions) (output *InitiateMultipartUploadOutput, err error) {
if input == nil {
return nil, errors.New("InitiateMultipartUploadInput is nil")
}
if input.ContentType == "" && input.Key != "" {
if contentType, ok := mimeTypes[strings.ToLower(input.Key[strings.LastIndex(input.Key, ".")+1:])]; ok {
input.ContentType = contentType
}
}
output = &InitiateMultipartUploadOutput{}
err = obsClient.doActionWithBucketAndKey("InitiateMultipartUpload", HTTP_POST, input.Bucket, input.Key, input, output, extensions)
if err != nil {
output = nil
} else {
ParseInitiateMultipartUploadOutput(output)
if output.EncodingType == "url" {
err = decodeInitiateMultipartUploadOutput(output)
if err != nil {
doLog(LEVEL_ERROR, "Failed to get InitiateMultipartUploadOutput with error: %v.", err)
output = nil
}
}
}
return
}
// UploadPart uploads a part to a specified bucket by using a specified multipart upload ID.
//
// After a multipart upload is initialized, you can use this API to upload a part to a specified bucket
// by using the multipart upload ID. Except for the last uploaded part whose size ranges from 0 to 5 GB,
// sizes of the other parts range from 100 KB to 5 GB. The upload part ID ranges from 1 to 10000.
func (obsClient ObsClient) UploadPart(_input *UploadPartInput, extensions ...extensionOptions) (output *UploadPartOutput, err error) {
if _input == nil {
return nil, errors.New("UploadPartInput is nil")
}
if _input.UploadId == "" {
return nil, errors.New("UploadId is empty")
}
input := &UploadPartInput{}
input.Bucket = _input.Bucket
input.Key = _input.Key
input.PartNumber = _input.PartNumber
input.UploadId = _input.UploadId
input.ContentMD5 = _input.ContentMD5
input.SourceFile = _input.SourceFile
input.Offset = _input.Offset
input.PartSize = _input.PartSize
input.SseHeader = _input.SseHeader
input.Body = _input.Body
output = &UploadPartOutput{}
var repeatable bool
if input.Body != nil {
if _, ok := input.Body.(*strings.Reader); !ok {
repeatable = false
}
if _, ok := input.Body.(*readerWrapper); !ok && input.PartSize > 0 {
input.Body = &readerWrapper{reader: input.Body, totalCount: input.PartSize}
}
} else if sourceFile := strings.TrimSpace(input.SourceFile); sourceFile != "" {
fd, _err := os.Open(sourceFile)
if _err != nil {
err = _err
return nil, err
}
defer func() {
errMsg := fd.Close()
if errMsg != nil {
doLog(LEVEL_WARN, "Failed to close file with reason: %v", errMsg)
}
}()
stat, _err := fd.Stat()
if _err != nil {
err = _err
return nil, err
}
fileSize := stat.Size()
fileReaderWrapper := &fileReaderWrapper{filePath: sourceFile}
fileReaderWrapper.reader = fd
if input.Offset < 0 || input.Offset > fileSize {
input.Offset = 0
}
if input.PartSize <= 0 || input.PartSize > (fileSize-input.Offset) {
input.PartSize = fileSize - input.Offset
}
fileReaderWrapper.totalCount = input.PartSize
if _, err = fd.Seek(input.Offset, io.SeekStart); err != nil {
return nil, err
}
input.Body = fileReaderWrapper
repeatable = true
}
if repeatable {
err = obsClient.doActionWithBucketAndKey("UploadPart", HTTP_PUT, input.Bucket, input.Key, input, output, extensions)
} else {
err = obsClient.doActionWithBucketAndKeyUnRepeatable("UploadPart", HTTP_PUT, input.Bucket, input.Key, input, output, extensions)
}
if err != nil {
output = nil
} else {
ParseUploadPartOutput(output)
output.PartNumber = input.PartNumber
}
return
}
// CompleteMultipartUpload combines the uploaded parts in a specified bucket by using the multipart upload ID.
func (obsClient ObsClient) CompleteMultipartUpload(input *CompleteMultipartUploadInput, extensions ...extensionOptions) (output *CompleteMultipartUploadOutput, err error) {
if input == nil {
return nil, errors.New("CompleteMultipartUploadInput is nil")
}
if input.UploadId == "" {
return nil, errors.New("UploadId is empty")
}
var parts partSlice = input.Parts
sort.Sort(parts)
output = &CompleteMultipartUploadOutput{}
err = obsClient.doActionWithBucketAndKey("CompleteMultipartUpload", HTTP_POST, input.Bucket, input.Key, input, output, extensions)
if err != nil {
output = nil
} else {
ParseCompleteMultipartUploadOutput(output)
if output.EncodingType == "url" {
err = decodeCompleteMultipartUploadOutput(output)
if err != nil {
doLog(LEVEL_ERROR, "Failed to get CompleteMultipartUploadOutput with error: %v.", err)
output = nil
}
}
}
return
}
// ListParts lists the uploaded parts in a bucket by using the multipart upload ID.
func (obsClient ObsClient) ListParts(input *ListPartsInput, extensions ...extensionOptions) (output *ListPartsOutput, err error) {
if input == nil {
return nil, errors.New("ListPartsInput is nil")
}
if input.UploadId == "" {
return nil, errors.New("UploadId is empty")
}
output = &ListPartsOutput{}
err = obsClient.doActionWithBucketAndKey("ListParts", HTTP_GET, input.Bucket, input.Key, input, output, extensions)
if err != nil {
output = nil
} else if output.EncodingType == "url" {
err = decodeListPartsOutput(output)
if err != nil {
doLog(LEVEL_ERROR, "Failed to get ListPartsOutput with error: %v.", err)
output = nil
}
}
return
}
// CopyPart copy a part to a specified bucket by using a specified multipart upload ID.
//
// After a multipart upload is initialized, you can use this API to copy a part to a specified bucket by using the multipart upload ID.
func (obsClient ObsClient) CopyPart(input *CopyPartInput, extensions ...extensionOptions) (output *CopyPartOutput, err error) {
if input == nil {
return nil, errors.New("CopyPartInput is nil")
}
if input.UploadId == "" {
return nil, errors.New("UploadId is empty")
}
if strings.TrimSpace(input.CopySourceBucket) == "" {
return nil, errors.New("Source bucket is empty")
}
if strings.TrimSpace(input.CopySourceKey) == "" {
return nil, errors.New("Source key is empty")
}
output = &CopyPartOutput{}
err = obsClient.doActionWithBucketAndKey("CopyPart", HTTP_PUT, input.Bucket, input.Key, input, output, extensions)
if err != nil {
output = nil
} else {
ParseCopyPartOutput(output)
output.PartNumber = input.PartNumber
}
return
}