Skip to content

Commit

Permalink
Merge pull request #262 from tencentyun/feature_jojoliang_96949a4d
Browse files Browse the repository at this point in the history
检查abort multi的路径
  • Loading branch information
agin719 authored Jun 24, 2024
2 parents 713fe2f + bb60e1e commit a70ac93
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
24 changes: 13 additions & 11 deletions bucket_part.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ type ListMultipartUploadsResult struct {

// ListMultipartUploadsOptions is the option of ListMultipartUploads
type ListMultipartUploadsOptions struct {
Delimiter string `url:"delimiter,omitempty"`
EncodingType string `url:"encoding-type,omitempty"`
Prefix string `url:"prefix,omitempty"`
MaxUploads int `url:"max-uploads,omitempty"`
KeyMarker string `url:"key-marker,omitempty"`
UploadIDMarker string `url:"upload-id-marker,omitempty"`
Delimiter string `url:"delimiter,omitempty"`
EncodingType string `url:"encoding-type,omitempty"`
Prefix string `url:"prefix,omitempty"`
MaxUploads int `url:"max-uploads,omitempty"`
KeyMarker string `url:"key-marker,omitempty"`
UploadIDMarker string `url:"upload-id-marker,omitempty"`
XOptionHeader *http.Header `header:"-,omitempty" url:"-" xml:"-"`
}

// ListMultipartUploads 用来查询正在进行中的分块上传。单次最多列出1000个正在进行中的分块上传。
Expand All @@ -46,11 +47,12 @@ type ListMultipartUploadsOptions struct {
func (s *BucketService) ListMultipartUploads(ctx context.Context, opt *ListMultipartUploadsOptions) (*ListMultipartUploadsResult, *Response, error) {
var res ListMultipartUploadsResult
sendOpt := sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: "/?uploads",
method: http.MethodGet,
result: &res,
optQuery: opt,
baseURL: s.client.BaseURL.BucketURL,
uri: "/?uploads",
method: http.MethodGet,
result: &res,
optQuery: opt,
optHeader: opt,
}
resp, err := s.client.doRetry(ctx, &sendOpt)
return &res, resp, err
Expand Down
22 changes: 18 additions & 4 deletions object_part.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,21 +286,35 @@ func (s *ObjectService) CompleteMultipartUpload(ctx context.Context, name, uploa
return &res, resp, err
}

type AbortMultipartUploadOptions struct {
XOptionHeader *http.Header `header:"-,omitempty" url:"-" xml:"-"`
}

// AbortMultipartUpload 用来实现舍弃一个分块上传并删除已上传的块。当您调用Abort Multipart Upload时,
// 如果有正在使用这个Upload Parts上传块的请求,则Upload Parts会返回失败。当该UploadID不存在时,会返回404 NoSuchUpload。
//
// 建议您及时完成分块上传或者舍弃分块上传,因为已上传但是未终止的块会占用存储空间进而产生存储费用。
//
// https://www.qcloud.com/document/product/436/7740
func (s *ObjectService) AbortMultipartUpload(ctx context.Context, name, uploadID string) (*Response, error) {
func (s *ObjectService) AbortMultipartUpload(ctx context.Context, name, uploadID string, opt ...*AbortMultipartUploadOptions) (*Response, error) {
if len(name) == 0 || name == "/" {
return nil, errors.New("empty object name")
}
// When use "" string might call the delete bucket interface
if s.client.Conf.ObjectKeySimplifyCheck && !CheckObjectKeySimplify("/"+name) {
return nil, ObjectKeySimplifyCheckErr
}

var optHeader *AbortMultipartUploadOptions
if len(opt) > 0 {
optHeader = opt[0]
}
u := fmt.Sprintf("/%s?uploadId=%s", encodeURIComponent(name), uploadID)
sendOpt := sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: u,
method: http.MethodDelete,
baseURL: s.client.BaseURL.BucketURL,
uri: u,
method: http.MethodDelete,
optHeader: optHeader,
}
resp, err := s.client.doRetry(ctx, &sendOpt)
return resp, err
Expand Down
19 changes: 17 additions & 2 deletions object_part_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,23 @@ func TestObjectService_AbortMultipartUpload(t *testing.T) {
w.WriteHeader(http.StatusNoContent)
})

_, err := client.Object.AbortMultipartUpload(context.Background(),
name, uploadID)
_, err := client.Object.AbortMultipartUpload(context.Background(), name, uploadID)
if err != nil {
t.Fatalf("Object.AbortMultipartUpload returned error: %v", err)
}
_, err = client.Object.AbortMultipartUpload(context.Background(), "/", uploadID)
if err == nil || err.Error() != "empty object name" {
t.Fatalf("Object.AbortMultipartUpload expect error: %v", err)
}
_, err = client.Object.AbortMultipartUpload(context.Background(), "//", uploadID)
if err != ObjectKeySimplifyCheckErr {
t.Fatalf("Object.AbortMultipartUpload expect error: %v", err)
}
opt := &AbortMultipartUploadOptions{
XOptionHeader: &http.Header{},
}
opt.XOptionHeader.Add("x-cos-meta-test", "value")
_, err = client.Object.AbortMultipartUpload(context.Background(), name, uploadID, opt)
if err != nil {
t.Fatalf("Object.AbortMultipartUpload returned error: %v", err)
}
Expand Down

0 comments on commit a70ac93

Please sign in to comment.