Skip to content

Commit 3f06e2e

Browse files
author
lwvjhm
committed
feat(cos): Add new data source tencentcloud_cos_object_signed_url
1 parent 5f3a2c2 commit 3f06e2e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+4154
-376
lines changed

.changelog/2967.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-data-source
2+
tencentcloud_cos_object_signed_url
3+
```

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ require (
100100
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf v1.0.1037
101101
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wedata v1.0.792
102102
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wss v1.0.199
103-
github.com/tencentyun/cos-go-sdk-v5 v0.7.42-0.20230629101357-7edd77448a0f
103+
github.com/tencentyun/cos-go-sdk-v5 v0.7.58
104104
github.com/yangwenmai/ratelimit v0.0.0-20180104140304-44221c2292e1
105105
gopkg.in/yaml.v2 v2.4.0
106106
)

go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14j
277277
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
278278
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
279279
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
280+
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
280281
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
281282
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
282283
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
@@ -1064,6 +1065,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wss v1.0.199 h1:hMBLtiJ
10641065
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wss v1.0.199/go.mod h1:nnY91/H3j/Gu7V/oCA6Zeg8T5D3q36EUdBh4EjmHwqY=
10651066
github.com/tencentyun/cos-go-sdk-v5 v0.7.42-0.20230629101357-7edd77448a0f h1:yTMDoBvFuXjZat10d98DIKbPnN9FQG+drt1SbNfMW5U=
10661067
github.com/tencentyun/cos-go-sdk-v5 v0.7.42-0.20230629101357-7edd77448a0f/go.mod h1:LUFnaqRmGk6pEHOaRmdn2dCZR2j0cSsM5xowWFPTPao=
1068+
github.com/tencentyun/cos-go-sdk-v5 v0.7.58 h1:YoZTqaRUcv75P4GSvyam93aEtdgOZWGUJWzXzyCV5EM=
1069+
github.com/tencentyun/cos-go-sdk-v5 v0.7.58/go.mod h1:8+hG+mQMuRP/OIS9d83syAvXvrMj9HhkND6Q1fLghw0=
10671070
github.com/tenntenn/modver v1.0.1 h1:2klLppGhDgzJrScMpkj9Ujy3rXPUspSjAcev9tSEBgA=
10681071
github.com/tenntenn/modver v1.0.1/go.mod h1:bePIyQPb7UeioSRkw3Q0XeMhYZSMx9B8ePqg6SAMGH0=
10691072
github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3 h1:f+jULpRQGxTSkNYKJ51yaw6ChIqO+Je8UqsTKN/cDag=

tencentcloud/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ func Provider() *schema.Provider {
511511
"tencentcloud_cos_batchs": cos.DataSourceTencentCloudCosBatchs(),
512512
"tencentcloud_cos_bucket_inventorys": cos.DataSourceTencentCloudCosBucketInventorys(),
513513
"tencentcloud_cos_bucket_multipart_uploads": cos.DataSourceTencentCloudCosBucketMultipartUploads(),
514+
"tencentcloud_cos_object_signed_url": cos.DataSourceTencentCloudCosObjectSignedUrl(),
514515
"tencentcloud_cfs_file_systems": cfs.DataSourceTencentCloudCfsFileSystems(),
515516
"tencentcloud_cfs_access_groups": cfs.DataSourceTencentCloudCfsAccessGroups(),
516517
"tencentcloud_cfs_access_rules": cfs.DataSourceTencentCloudCfsAccessRules(),

tencentcloud/provider.md

+1
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ Cloud Object Storage(COS)
412412
tencentcloud_cos_batchs
413413
tencentcloud_cos_bucket_inventorys
414414
tencentcloud_cos_bucket_multipart_uploads
415+
tencentcloud_cos_object_signed_url
415416

416417
Resource
417418
tencentcloud_cos_bucket
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package cos
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
11+
"github.com/tencentyun/cos-go-sdk-v5"
12+
13+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
14+
)
15+
16+
func DataSourceTencentCloudCosObjectSignedUrl() *schema.Resource {
17+
return &schema.Resource{
18+
Read: DataSourceTencentCloudCosObjectSignedUrlRead,
19+
20+
Schema: map[string]*schema.Schema{
21+
"bucket": {
22+
Type: schema.TypeString,
23+
Required: true,
24+
Description: "Name of the bucket.",
25+
},
26+
"path": {
27+
Type: schema.TypeString,
28+
Required: true,
29+
Description: "The full path to the object inside the bucket.",
30+
},
31+
"method": {
32+
Type: schema.TypeString,
33+
Optional: true,
34+
Default: "GET",
35+
ValidateFunc: validation.StringInSlice([]string{"GET", "PUT"}, true),
36+
Description: "Method, GET or PUT. Default value is GET.",
37+
},
38+
"duration": {
39+
Type: schema.TypeString,
40+
Optional: true,
41+
Default: "1m",
42+
Description: "Duration of signed url. Default value is 1m.",
43+
},
44+
"headers": {
45+
Type: schema.TypeMap,
46+
Optional: true,
47+
Elem: &schema.Schema{Type: schema.TypeString},
48+
Description: "Request headers.",
49+
},
50+
"queries": {
51+
Type: schema.TypeMap,
52+
Optional: true,
53+
Elem: &schema.Schema{Type: schema.TypeString},
54+
Description: "Request query parameters.",
55+
},
56+
"signed_url": {
57+
Type: schema.TypeString,
58+
Computed: true,
59+
Sensitive: true,
60+
Description: "Signed URL.",
61+
},
62+
"result_output_file": {
63+
Type: schema.TypeString,
64+
Optional: true,
65+
Description: "Used to save results.",
66+
},
67+
},
68+
}
69+
}
70+
71+
func DataSourceTencentCloudCosObjectSignedUrlRead(d *schema.ResourceData, meta interface{}) error {
72+
defer tccommon.LogElapsed("data_source.tencentcloud_cos_object_signed_url.read")()
73+
74+
logId := tccommon.GetLogId(tccommon.ContextNil)
75+
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
76+
77+
bucket := d.Get("bucket").(string)
78+
path := d.Get("path").(string)
79+
method := "GET"
80+
durationString := "1m"
81+
opt := &cos.PresignedURLOptions{}
82+
signHost := true
83+
84+
if v, ok := d.GetOk("method"); ok {
85+
method = v.(string)
86+
}
87+
88+
if v, ok := d.GetOk("duration"); ok {
89+
durationString = v.(string)
90+
}
91+
92+
duration, err := time.ParseDuration(durationString)
93+
if err != nil {
94+
return err
95+
}
96+
97+
if v, ok := d.GetOk("headers"); ok {
98+
for key, value := range v.(map[string]string) {
99+
opt.Header.Set(key, value)
100+
}
101+
}
102+
103+
if v, ok := d.GetOk("queries"); ok {
104+
for key, value := range v.(map[string]string) {
105+
opt.Query.Set(key, value)
106+
}
107+
}
108+
109+
result, err := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseTencentCosClient(bucket).Object.GetPresignedURL2(ctx, method, path, duration, opt, signHost)
110+
if err != nil {
111+
return err
112+
}
113+
114+
signedUrl := result.String()
115+
116+
d.SetId(helper.DataResourceIdHash(path))
117+
_ = d.Set("signed_url", signedUrl)
118+
output, ok := d.GetOk("result_output_file")
119+
if ok && output.(string) != "" {
120+
if err := tccommon.WriteToFile(output.(string), signedUrl); err != nil {
121+
return err
122+
}
123+
}
124+
125+
return nil
126+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Use this data source to query the signed url of the COS object.
2+
3+
Example Usage
4+
5+
```hcl
6+
data "tencentcloud_cos_object_signed_url" "cos_object_signed_url" {
7+
bucket = "xxxxxx"
8+
path = "path/to/file"
9+
}
10+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package cos_test
2+
3+
import (
4+
"testing"
5+
6+
tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
func TestAccTencentCloudCosObjectSignedUrlDataSource_basic(t *testing.T) {
12+
t.Parallel()
13+
resource.Test(t, resource.TestCase{
14+
PreCheck: func() {
15+
tcacctest.AccPreCheck(t)
16+
},
17+
Providers: tcacctest.AccProviders,
18+
Steps: []resource.TestStep{
19+
{
20+
Config: testAccCosObjectSignedUrlDataSource,
21+
Check: resource.ComposeTestCheckFunc(
22+
tcacctest.AccCheckTencentCloudDataSourceID("data.tencentcloud_cos_object_signed_url.cos_object_signed_url"),
23+
resource.TestCheckResourceAttrSet("data.tencentcloud_cos_object_signed_url.cos_object_signed_url", "signed_url"),
24+
),
25+
},
26+
},
27+
})
28+
}
29+
30+
const testAccCosObjectSignedUrlDataSource = `
31+
data "tencentcloud_cos_object_signed_url" "cos_object_signed_url" {
32+
bucket = "keep-test-1308919341"
33+
path = "path/to/file"
34+
headers = {
35+
Content-Type = "text/plain"
36+
}
37+
queries = {
38+
prefix = "xxx"
39+
}
40+
}
41+
`

tencentcloud/services/cos/resource_tc_cos_bucket.go

+3-14
Original file line numberDiff line numberDiff line change
@@ -1582,11 +1582,6 @@ func resourceTencentCloudCosBucketOriginPullUpdate(ctx context.Context, service
15821582
CopyOriginData: true,
15831583
HttpHeader: &cos.BucketOriginHttpHeader{},
15841584
},
1585-
OriginInfo: &cos.BucketOriginInfo{
1586-
FileInfo: &cos.BucketOriginFileInfo{
1587-
PrefixDirective: false,
1588-
},
1589-
},
15901585
}
15911586
)
15921587

@@ -1606,7 +1601,7 @@ func resourceTencentCloudCosBucketOriginPullUpdate(ctx context.Context, service
16061601
item.OriginParameter.Protocol = v.(string)
16071602
}
16081603
if v, ok := dMap["host"]; ok {
1609-
item.OriginInfo.HostInfo = v.(string)
1604+
item.OriginInfo.HostInfo.HostName = v.(string)
16101605
}
16111606
if v, ok := dMap["follow_query_string"]; ok {
16121607
item.OriginParameter.FollowQueryString = v.(bool)
@@ -1619,17 +1614,11 @@ func resourceTencentCloudCosBucketOriginPullUpdate(ctx context.Context, service
16191614
//}
16201615
if v, ok := dMap["redirect_prefix"]; ok {
16211616
value := v.(string)
1622-
if value != "" {
1623-
item.OriginInfo.FileInfo.PrefixDirective = true
1624-
}
1625-
item.OriginInfo.FileInfo.Prefix = value
1617+
item.OriginInfo.FileInfo.PrefixConfiguration.Prefix = value
16261618
}
16271619
if v, ok := dMap["redirect_suffix"]; ok {
16281620
value := v.(string)
1629-
if value != "" {
1630-
item.OriginInfo.FileInfo.PrefixDirective = true
1631-
}
1632-
item.OriginInfo.FileInfo.Suffix = value
1621+
item.OriginInfo.FileInfo.SuffixConfiguration.Suffix = value
16331622
}
16341623
if v, ok := dMap["custom_http_headers"]; ok {
16351624
var customHeaders []cos.OriginHttpHeader

tencentcloud/services/cos/service_tencentcloud_cos.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ func (me *CosService) GetBucketPullOrigin(ctx context.Context, bucket string) (r
12721272
for _, rule := range originConfig.Rule {
12731273
item := make(map[string]interface{})
12741274
item["priority"] = helper.Int(rule.RulePriority)
1275-
item["host"] = helper.String(rule.OriginInfo.HostInfo)
1275+
item["host"] = helper.String(rule.OriginInfo.HostInfo.HostName)
12761276

12771277
if rule.OriginCondition != nil {
12781278
item["prefix"] = helper.String(rule.OriginCondition.Prefix)
@@ -1311,7 +1311,7 @@ func (me *CosService) GetBucketPullOrigin(ctx context.Context, bucket string) (r
13111311
}
13121312

13131313
if rule.OriginInfo.FileInfo != nil {
1314-
item["host"] = helper.String(rule.OriginInfo.HostInfo)
1314+
item["host"] = helper.String(rule.OriginInfo.HostInfo.HostName)
13151315
//item["redirect_prefix"] = helper.String(rule.OriginInfo.FileInfo.Prefix)
13161316
//item["redirect_suffix"] = helper.String(rule.OriginInfo.FileInfo.Suffix)
13171317
}

vendor/github.com/tencentyun/cos-go-sdk-v5/.gitignore

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)