Skip to content

Commit af3675d

Browse files
committed
chore(deps): reduce tencentyun/cos dependencies
1 parent 62b7c5a commit af3675d

9 files changed

Lines changed: 789 additions & 34 deletions

File tree

go.mod

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ require (
88
github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible
99
github.com/aws/aws-sdk-go-v2 v1.41.6
1010
github.com/jdcloud-api/jdcloud-sdk-go v1.64.0
11-
github.com/tencentyun/cos-go-sdk-v5 v0.7.73
1211
github.com/volcengine/volcengine-go-sdk v1.0.168
1312
golang.org/x/oauth2 v0.12.0
1413
gopkg.in/yaml.v3 v3.0.1
@@ -18,18 +17,14 @@ require (
1817
cloud.google.com/go/compute v1.20.1 // indirect
1918
cloud.google.com/go/compute/metadata v0.2.3 // indirect
2019
github.com/aws/smithy-go v1.25.0 // indirect
21-
github.com/clbanning/mxj v1.8.4 // indirect
2220
github.com/gofrs/uuid v4.4.0+incompatible // indirect
2321
github.com/golang/protobuf v1.5.3 // indirect
24-
github.com/google/go-querystring v1.0.0 // indirect
2522
github.com/google/uuid v1.3.0 // indirect
2623
github.com/jmespath/go-jmespath v0.4.0 // indirect
2724
github.com/mattn/go-colorable v0.1.12 // indirect
2825
github.com/mattn/go-isatty v0.0.14 // indirect
2926
github.com/mattn/go-runewidth v0.0.9 // indirect
3027
github.com/mattn/go-tty v0.0.5 // indirect
31-
github.com/mitchellh/mapstructure v1.5.0 // indirect
32-
github.com/mozillazg/go-httpheader v0.2.1 // indirect
3328
github.com/olekukonko/tablewriter v0.0.5 // indirect
3429
github.com/pkg/term v1.2.0-beta.2 // indirect
3530
github.com/volcengine/volc-sdk-golang v1.0.23 // indirect

go.sum

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ github.com/aws/aws-sdk-go-v2 v1.41.6/go.mod h1:dy0UzBIfwSeot4grGvY1AqFWN5zgziMmW
1616
github.com/aws/smithy-go v1.25.0 h1:Sz/XJ64rwuiKtB6j98nDIPyYrV1nVNJ4YU74gttcl5U=
1717
github.com/aws/smithy-go v1.25.0/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc=
1818
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
19-
github.com/clbanning/mxj v1.8.4 h1:HuhwZtbyvyOw+3Z1AowPkU87JkJUSv751ELWaiTpj8I=
20-
github.com/clbanning/mxj v1.8.4/go.mod h1:BVjHeAH+rl9rs6f+QIpeRl0tfu10SXn1pUSa5PVGJng=
2119
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
2220
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2321
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -26,7 +24,6 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
2624
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
2725
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
2826
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
29-
github.com/golang-jwt/jwt/v5 v5.2.3/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
3027
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
3128
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
3229
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@@ -50,9 +47,6 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
5047
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
5148
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
5249
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
53-
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
54-
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
55-
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
5650
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
5751
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
5852
github.com/jdcloud-api/jdcloud-sdk-go v1.64.0 h1:xZc/ZRcrOhDx9Ra9htu6ui2gUUttmLsXIqH61LcvY4U=
@@ -80,11 +74,6 @@ github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/Qd
8074
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
8175
github.com/mattn/go-tty v0.0.5 h1:s09uXI7yDbXzzTTfw3zonKFzwGkyYlgU3OMjqA0ddz4=
8276
github.com/mattn/go-tty v0.0.5/go.mod h1:u5GGXBtZU6RQoKV8gY5W6UhMudbR5vXnUe7j3pxse28=
83-
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
84-
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
85-
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
86-
github.com/mozillazg/go-httpheader v0.2.1 h1:geV7TrjbL8KXSyvghnFm+NyTux/hxwueTSrwhe88TQQ=
87-
github.com/mozillazg/go-httpheader v0.2.1/go.mod h1:jJ8xECTlalr6ValeXYdOF8fFUISeBAdw6E61aqQma60=
8877
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
8978
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
9079
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
@@ -93,15 +82,8 @@ github.com/pkg/term v1.2.0-beta.2/go.mod h1:E25nymQcrSllhX42Ok8MRm1+hyBdHY0dCeiK
9382
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
9483
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
9584
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
96-
github.com/rs/dnscache v0.0.0-20230804202142-fc85eb664529/go.mod h1:qe5TWALJ8/a1Lqznoc5BDHpYX/8HU60Hm2AwRmqzxqA=
9785
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
98-
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
9986
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
100-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.563/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
101-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.563/go.mod h1:uom4Nvi9W+Qkom0exYiJ9VWJjXwyxtPYTkKkaLMlfE0=
102-
github.com/tencentyun/cos-go-sdk-v5 v0.7.73 h1:uFfgp1A7cQaAGR6QP9DsIkoEQ67b8ewj5r1RV6XB540=
103-
github.com/tencentyun/cos-go-sdk-v5 v0.7.73/go.mod h1:STbTNaNKq03u+gscPEGOahKzLcGSYOj6Dzc5zNay7Pg=
104-
github.com/tencentyun/qcloud-cos-sts-sdk v0.0.0-20250515025012-e0eec8a5d123/go.mod h1:b18KQa4IxHbxeseW1GcZox53d7J0z39VNONTxvvlkXw=
10587
github.com/volcengine/volc-sdk-golang v1.0.23 h1:anOslb2Qp6ywnsbyq9jqR0ljuO63kg9PY+4OehIk5R8=
10688
github.com/volcengine/volc-sdk-golang v1.0.23/go.mod h1:AfG/PZRUkHJ9inETvbjNifTDgut25Wbkm2QoYBTbvyU=
10789
github.com/volcengine/volcengine-go-sdk v1.0.168 h1:YE42x70pDNeYPQYCuYFcTeh9pEii7NGigXU5fuPEz14=

pkg/providers/tencent/cos/bucket.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@ package cos
22

33
import (
44
"context"
5-
"net/http"
65

76
"github.com/404tk/cloudtoolkit/pkg/providers/tencent/auth"
87
"github.com/404tk/cloudtoolkit/pkg/schema"
98
"github.com/404tk/cloudtoolkit/utils/logger"
10-
"github.com/tencentyun/cos-go-sdk-v5"
119
)
1210

1311
type Driver struct {
1412
Credential auth.Credential
13+
Client *Client
14+
}
15+
16+
func (d *Driver) client() *Client {
17+
if d.Client == nil {
18+
d.Client = NewClient(d.Credential)
19+
}
20+
return d.Client
1521
}
1622

1723
func (d *Driver) GetBuckets(ctx context.Context) ([]schema.Storage, error) {
@@ -22,20 +28,13 @@ func (d *Driver) GetBuckets(ctx context.Context) ([]schema.Storage, error) {
2228
default:
2329
logger.Info("List COS buckets ...")
2430
}
25-
client := cos.NewClient(nil, &http.Client{
26-
Transport: &cos.AuthorizationTransport{
27-
SecretID: d.Credential.SecretID,
28-
SecretKey: d.Credential.SecretKey,
29-
SessionToken: d.Credential.Token,
30-
},
31-
})
32-
buckets, _, err := client.Service.Get(ctx)
31+
resp, err := d.client().ListBuckets(ctx)
3332
if err != nil {
3433
logger.Error("List buckets failed.")
3534
return nil, err
3635
}
3736

38-
for _, bucket := range buckets.Buckets {
37+
for _, bucket := range resp.Buckets {
3938
_bucket := schema.Storage{
4039
BucketName: bucket.Name,
4140
Region: bucket.Region,
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package cos
2+
3+
import (
4+
"context"
5+
"io"
6+
"net/http"
7+
"strings"
8+
"testing"
9+
"time"
10+
11+
"github.com/404tk/cloudtoolkit/pkg/providers/tencent/api"
12+
"github.com/404tk/cloudtoolkit/pkg/providers/tencent/auth"
13+
)
14+
15+
func TestDriverGetBucketsMapsServiceResponse(t *testing.T) {
16+
client := NewClient(
17+
auth.New("AKIDEXAMPLE", "SECRETKEYEXAMPLE", "TOKENEXAMPLE"),
18+
WithHTTPClient(&http.Client{
19+
Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
20+
if r.Method != http.MethodGet {
21+
t.Fatalf("unexpected method: %s", r.Method)
22+
}
23+
if r.URL.Host != "service.cos.myqcloud.com" {
24+
t.Fatalf("unexpected host: %s", r.URL.Host)
25+
}
26+
if r.URL.Path != "/" {
27+
t.Fatalf("unexpected path: %s", r.URL.Path)
28+
}
29+
if r.Header.Get("x-cos-security-token") != "TOKENEXAMPLE" {
30+
t.Fatalf("unexpected token header: %q", r.Header.Get("x-cos-security-token"))
31+
}
32+
authHeader := r.Header.Get("Authorization")
33+
if authHeader == "" {
34+
t.Fatal("missing authorization header")
35+
}
36+
if !strings.Contains(authHeader, "q-header-list=host;x-cos-security-token") {
37+
t.Fatalf("unexpected authorization header: %s", authHeader)
38+
}
39+
return &http.Response{
40+
StatusCode: http.StatusOK,
41+
Header: make(http.Header),
42+
Body: io.NopCloser(strings.NewReader(`<ListAllMyBucketsResult>
43+
<Owner>
44+
<ID>xbaccxx</ID>
45+
<DisplayName>100000760461</DisplayName>
46+
</Owner>
47+
<Buckets>
48+
<Bucket>
49+
<Name>huadong-1253846586</Name>
50+
<Location>ap-shanghai</Location>
51+
<CreationDate>2017-06-16T13:08:28Z</CreationDate>
52+
</Bucket>
53+
<Bucket>
54+
<Name>huanan-1253846586</Name>
55+
<Location>ap-guangzhou</Location>
56+
<CreationDate>2017-06-10T09:00:07Z</CreationDate>
57+
</Bucket>
58+
</Buckets>
59+
</ListAllMyBucketsResult>`)),
60+
Request: r,
61+
}, nil
62+
}),
63+
}),
64+
WithRetryPolicy(api.RetryPolicy{MaxAttempts: 1}),
65+
WithClock(func() time.Time { return time.Date(2026, 4, 19, 12, 0, 0, 0, time.UTC) }),
66+
)
67+
68+
driver := &Driver{
69+
Credential: auth.New("AKIDEXAMPLE", "SECRETKEYEXAMPLE", "TOKENEXAMPLE"),
70+
Client: client,
71+
}
72+
73+
got, err := driver.GetBuckets(context.Background())
74+
if err != nil {
75+
t.Fatalf("GetBuckets() error = %v", err)
76+
}
77+
if len(got) != 2 {
78+
t.Fatalf("unexpected bucket count: %d", len(got))
79+
}
80+
if got[0].BucketName != "huadong-1253846586" || got[0].Region != "ap-shanghai" {
81+
t.Fatalf("unexpected first bucket: %+v", got[0])
82+
}
83+
if got[1].BucketName != "huanan-1253846586" || got[1].Region != "ap-guangzhou" {
84+
t.Fatalf("unexpected second bucket: %+v", got[1])
85+
}
86+
}
87+
88+
func TestDriverGetBucketsReturnsServiceError(t *testing.T) {
89+
client := NewClient(
90+
auth.New("AKIDEXAMPLE", "SECRETKEYEXAMPLE", ""),
91+
WithHTTPClient(&http.Client{
92+
Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
93+
return &http.Response{
94+
StatusCode: http.StatusForbidden,
95+
Header: http.Header{
96+
"X-Cos-Request-Id": []string{"req-1"},
97+
},
98+
Body: io.NopCloser(strings.NewReader(`<Error><Code>AccessDenied</Code><Message>denied</Message></Error>`)),
99+
Request: r,
100+
}, nil
101+
}),
102+
}),
103+
WithRetryPolicy(api.RetryPolicy{MaxAttempts: 1}),
104+
WithClock(func() time.Time { return time.Date(2026, 4, 19, 12, 0, 0, 0, time.UTC) }),
105+
)
106+
107+
driver := &Driver{
108+
Credential: auth.New("AKIDEXAMPLE", "SECRETKEYEXAMPLE", ""),
109+
Client: client,
110+
}
111+
112+
_, err := driver.GetBuckets(context.Background())
113+
if err == nil {
114+
t.Fatal("expected service error")
115+
}
116+
if !strings.Contains(err.Error(), "AccessDenied") || !strings.Contains(err.Error(), "request_id=req-1") {
117+
t.Fatalf("unexpected error: %v", err)
118+
}
119+
}
120+
121+
type roundTripFunc func(*http.Request) (*http.Response, error)
122+
123+
func (fn roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) {
124+
return fn(r)
125+
}

0 commit comments

Comments
 (0)