Skip to content

Commit

Permalink
Merge pull request #78 from agin719/common-dev
Browse files Browse the repository at this point in the history
add GetBucketObjectVersions & update tips when failed
  • Loading branch information
agin719 authored May 20, 2020
2 parents e4f0086 + bcc1ed2 commit 1cc4970
Show file tree
Hide file tree
Showing 31 changed files with 945 additions and 139 deletions.
58 changes: 58 additions & 0 deletions bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,61 @@ type Bucket struct {
Region string `xml:"Location,omitempty"`
CreationDate string `xml:",omitempty"`
}

type BucketGetObjectVersionsOptions struct {
Prefix string `url:"prefix,omitempty"`
Delimiter string `url:"delimiter,omitempty"`
EncodingType string `url:"encoding-type,omitempty"`
KeyMarker string `url:"key-marker,omitempty"`
VersionIdMarker string `url:"version-id-marker,omitempty"`
MaxKeys int `url:"max-keys,omitempty"`
}

type BucketGetObjectVersionsResult struct {
XMLName xml.Name `xml:"ListVersionsResult"`
Name string `xml:"Name,omitempty"`
EncodingType string `xml:"EncodingType,omitempty"`
Prefix string `xml:"Prefix,omitempty"`
KeyMarker string `xml:"KeyMarker,omitempty"`
VersionIdMarker string `xml:"VersionIdMarker,omitempty"`
MaxKeys int `xml:"MaxKeys,omitempty"`
Delimiter string `xml:"Delimiter,omitempty"`
IsTruncated bool `xml:"IsTruncated,omitempty"`
NextKeyMarker string `xml:"NextKeyMarker,omitempty"`
NextVersionIdMarker string `xml:"NextVersionIdMarker,omitempty"`
CommonPrefixes []string `xml:"CommonPrefixes>Prefix,omitempty"`
Version []ListVersionsResultVersion `xml:"Version,omitempty"`
DeleteMarker []ListVersionsResultDeleteMarker `xml:"DeleteMarker,omitempty"`
}

type ListVersionsResultVersion struct {
Key string `xml:"Key,omitempty"`
VersionId string `xml:"VersionId,omitempty"`
IsLatest bool `xml:"IsLatest,omitempty"`
LastModified string `xml:"LastModified,omitempty"`
ETag string `xml:"ETag,omitempty"`
Size int `xml:"Size,omitempty"`
StorageClass string `xml:"StorageClass,omitempty"`
Owner *Owner `xml:"Owner,omitempty"`
}

type ListVersionsResultDeleteMarker struct {
Key string `xml:"Key,omitempty"`
VersionId string `xml:"VersionId,omitempty"`
IsLatest bool `xml:"IsLatest,omitempty"`
LastModified string `xml:"LastModified,omitempty"`
Owner *Owner `xml:"Owner,omitempty"`
}

func (s *BucketService) GetObjectVersions(ctx context.Context, opt *BucketGetObjectVersionsOptions) (*BucketGetObjectVersionsResult, *Response, error) {
var res BucketGetObjectVersionsResult
sendOpt := sendOptions{
baseURL: s.client.BaseURL.BucketURL,
uri: "/?versions",
method: http.MethodGet,
optQuery: opt,
result: &res,
}
resp, err := s.client.send(ctx, &sendOpt)
return &res, resp, err
}
105 changes: 105 additions & 0 deletions bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,108 @@ func TestBucketService_Head(t *testing.T) {
t.Fatalf("Bucket.Head returned error: %v", err)
}
}

func TestBucketService_GetObjectVersions(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
w.WriteHeader(http.StatusOK)
vs := values{
"versions": "",
"delimiter": "/",
}
testFormValues(t, r, vs)

fmt.Fprint(w, `<?xml version='1.0' encoding='utf-8' ?>
<ListVersionsResult>
<Name>examplebucket-1250000000</Name>
<Prefix/>
<KeyMarker/>
<VersionIdMarker/>
<MaxKeys>1000</MaxKeys>
<IsTruncated>false</IsTruncated>
<Delimiter>/</Delimiter>
<CommonPrefixes>
<Prefix>example-folder-1/</Prefix>
</CommonPrefixes>
<CommonPrefixes>
<Prefix>example-folder-2/</Prefix>
</CommonPrefixes>
<Version>
<Key>example-object-1.jpg</Key>
<VersionId>MTg0NDUxNzgxMjEzNTU3NTk1Mjg</VersionId>
<IsLatest>true</IsLatest>
<LastModified>2019-08-16T10:45:53.000Z</LastModified>
<ETag>&quot;5d1143df07a17b23320d0da161e2819e&quot;</ETag>
<Size>30</Size>
<StorageClass>STANDARD</StorageClass>
<Owner>
<ID>1250000000</ID>
<DisplayName>1250000000</DisplayName>
</Owner>
</Version>
<DeleteMarker>
<Key>example-object-1.jpg</Key>
<VersionId>MTg0NDUxNzgxMjEzNjE1OTcxMzM</VersionId>
<IsLatest>false</IsLatest>
<LastModified>2019-08-16T10:45:47.000Z</LastModified>
<Owner>
<ID>1250000000</ID>
<DisplayName>1250000000</DisplayName>
</Owner>
</DeleteMarker>
</ListVersionsResult>`)
})

want := &BucketGetObjectVersionsResult {
XMLName: xml.Name { Local: "ListVersionsResult" },
Name: "examplebucket-1250000000",
MaxKeys: 1000,
IsTruncated: false,
Delimiter: "/",
CommonPrefixes: []string {
"example-folder-1/",
"example-folder-2/",
},
Version: []ListVersionsResultVersion {
{
Key: "example-object-1.jpg",
VersionId: "MTg0NDUxNzgxMjEzNTU3NTk1Mjg",
IsLatest: true,
LastModified: "2019-08-16T10:45:53.000Z",
ETag: "\"5d1143df07a17b23320d0da161e2819e\"",
Size: 30,
StorageClass: "STANDARD",
Owner: &Owner {
ID: "1250000000",
DisplayName: "1250000000",
},
},
},
DeleteMarker: []ListVersionsResultDeleteMarker {
{
Key: "example-object-1.jpg",
VersionId: "MTg0NDUxNzgxMjEzNjE1OTcxMzM",
IsLatest: false,
LastModified: "2019-08-16T10:45:47.000Z",
Owner: &Owner {
ID: "1250000000",
DisplayName: "1250000000",
},
},
},
}
opt := &BucketGetObjectVersionsOptions {
Delimiter: "/",
}
res, _, err := client.Bucket.GetObjectVersions(context.Background(), opt)
if err != nil {
t.Fatalf("Bucket.GetObjectVersions returned error: %v", err)
}
if !reflect.DeepEqual(res, want) {
t.Errorf("Bucket.GetObjectVersions returned\n%+v\nwant\n%+v", res, want)
}

}
2 changes: 1 addition & 1 deletion cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

const (
// Version current go sdk version
Version = "0.7.5"
Version = "0.7.6"
userAgent = "cos-go-sdk-v5/" + Version
contentTypeXML = "application/xml"
defaultServiceBaseURL = "http://service.cos.myqcloud.com"
Expand Down
9 changes: 9 additions & 0 deletions costesting/ci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,15 @@ func (s *CosTestSuite) TestGetBucket() {
assert.Nil(s.T(), err, "GetBucket Failed")
}

func (s *CosTestSuite) TestGetObjectVersions() {
opt := &cos.BucketGetObjectVersionsOptions {
Prefix: "中文",
MaxKeys: 3,
}
_, _, err := s.Client.Bucket.GetObjectVersions(context.Background(), opt)
assert.Nil(s.T(), err, "GetObjectVersions Failed")
}

func (s *CosTestSuite) TestGetBucketLocation() {
v, _, err := s.Client.Bucket.GetLocation(context.Background())
assert.Nil(s.T(), err, "GetLocation Failed")
Expand Down
38 changes: 38 additions & 0 deletions example/bucket/getLogging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"context"
"fmt"
"net/url"
"os"

"net/http"

"github.com/tencentyun/cos-go-sdk-v5"
"github.com/tencentyun/cos-go-sdk-v5/debug"
)

func main() {
u, _ := url.Parse("https://bj-1259654469.cos.ap-beijing.myqcloud.com")
b := &cos.BaseURL{
BucketURL: u,
}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("COS_SECRETID"),
SecretKey: os.Getenv("COS_SECRETKEY"),
Transport: &debug.DebugRequestTransport{
RequestHeader: true,
RequestBody: true,
ResponseHeader: true,
ResponseBody: true,
},
},
})

v, _, err := c.Bucket.GetLogging(context.Background())
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", v.LoggingEnabled)
}
64 changes: 64 additions & 0 deletions example/bucket/getObjectVersion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"context"
"fmt"
"os"

"net/url"

"net/http"

"github.com/tencentyun/cos-go-sdk-v5"
"github.com/tencentyun/cos-go-sdk-v5/debug"
)

func log_status(err error) {
if err == nil {
return
}
if cos.IsNotFoundError(err) {
// WARN
fmt.Println("WARN: Resource is not existed")
} else if e, ok := cos.IsCOSError(err); ok {
fmt.Printf("ERROR: Code: %v\n", e.Code)
fmt.Printf("ERROR: Message: %v\n", e.Message)
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
// ERROR
} else {
fmt.Printf("ERROR: %v\n", err)
// ERROR
}
}

func main() {
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
b := &cos.BaseURL{
BucketURL: u,
}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("COS_SECRETID"),
SecretKey: os.Getenv("COS_SECRETKEY"),
Transport: &debug.DebugRequestTransport{
RequestHeader: true,
RequestBody: true,
ResponseHeader: true,
ResponseBody: true,
},
},
})

opt := &cos.BucketGetObjectVersionsOptions {
Delimiter: "/",
MaxKeys: 1,
}
v, _, err := c.Bucket.GetObjectVersions(context.Background(), opt)
log_status(err)

for _, c := range v.Version {
fmt.Printf("%v, %v, %v\n", c.Key, c.Size, c.IsLatest)
}

}
23 changes: 20 additions & 3 deletions example/object/MutiUpload.go → example/object/MultiUpload.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ import (
"github.com/tencentyun/cos-go-sdk-v5/debug"
)

func log_status(err error) {
if err == nil {
return
}
if cos.IsNotFoundError(err) {
// WARN
fmt.Println("WARN: Resource is not existed")
} else if e, ok := cos.IsCOSError(err); ok {
fmt.Printf("ERROR: Code: %v\n", e.Code)
fmt.Printf("ERROR: Message: %v\n", e.Message)
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
// ERROR
} else {
fmt.Printf("ERROR: %v\n", err)
// ERROR
}
}

func main() {
u, _ := url.Parse("http://alanbj-1251668577.cos.ap-beijing.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
Expand All @@ -37,8 +56,6 @@ func main() {
v, _, err := c.Object.MultiUpload(
context.Background(), "test/gomulput1G", "./test1G", opt,
)
if err != nil {
panic(err)
}
log_status(err)
fmt.Println(v)
}
27 changes: 21 additions & 6 deletions example/object/abortMultipartUpload.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ import (
"github.com/tencentyun/cos-go-sdk-v5/debug"
)

func log_status(err error) {
if err == nil {
return
}
if cos.IsNotFoundError(err) {
// WARN
fmt.Println("WARN: Resource is not existed")
} else if e, ok := cos.IsCOSError(err); ok {
fmt.Printf("ERROR: Code: %v\n", e.Code)
fmt.Printf("ERROR: Message: %v\n", e.Message)
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
// ERROR
} else {
fmt.Printf("ERROR: %v\n", err)
// ERROR
}
}

func main() {
u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
b := &cos.BaseURL{BucketURL: u}
Expand All @@ -30,14 +49,10 @@ func main() {

name := "test_multipart.txt"
v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil)
if err != nil {
panic(err)
}
log_status(err)
fmt.Printf("%s\n", v.UploadID)

resp, err := c.Object.AbortMultipartUpload(context.Background(), name, v.UploadID)
if err != nil {
panic(err)
}
log_status(err)
fmt.Printf("%s\n", resp.Status)
}
Loading

0 comments on commit 1cc4970

Please sign in to comment.