add object tagging && bucket origin && add stsv3 demo
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
@@ -19,7 +19,7 @@ func log_status(err error) {
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
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)
|
||||
@@ -50,12 +50,12 @@ func main() {
|
||||
},
|
||||
})
|
||||
|
||||
opt := &cos.BucketGetObjectVersionsOptions {
|
||||
Delimiter: "/",
|
||||
MaxKeys: 1,
|
||||
opt := &cos.BucketGetObjectVersionsOptions{
|
||||
Delimiter: "/",
|
||||
MaxKeys: 1,
|
||||
}
|
||||
v, _, err := c.Bucket.GetObjectVersions(context.Background(), opt)
|
||||
log_status(err)
|
||||
log_status(err)
|
||||
|
||||
for _, c := range v.Version {
|
||||
fmt.Printf("%v, %v, %v\n", c.Key, c.Size, c.IsLatest)
|
||||
|
||||
92
example/bucket/origin.go
Normal file
92
example/bucket/origin.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"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("Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("Code: %v\n", e.Code)
|
||||
fmt.Printf("Message: %v\n", e.Message)
|
||||
fmt.Printf("Resource: %v\n", e.Resource)
|
||||
fmt.Printf("RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Println(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.BucketPutOriginOptions{
|
||||
Rule: []cos.BucketOriginRule{
|
||||
{
|
||||
OriginType: "Proxy",
|
||||
OriginCondition: &cos.BucketOriginCondition{
|
||||
HTTPStatusCode: "404",
|
||||
Prefix: "",
|
||||
},
|
||||
OriginParameter: &cos.BucketOriginParameter{
|
||||
Protocol: "FOLLOW",
|
||||
FollowQueryString: true,
|
||||
HttpHeader: &cos.BucketOriginHttpHeader{
|
||||
NewHttpHeaders: []cos.OriginHttpHeader{
|
||||
{
|
||||
Key: "x-cos-ContentType",
|
||||
Value: "csv",
|
||||
},
|
||||
},
|
||||
FollowHttpHeaders: []cos.OriginHttpHeader{
|
||||
{
|
||||
Key: "Content-Type",
|
||||
},
|
||||
},
|
||||
},
|
||||
FollowRedirection: true,
|
||||
},
|
||||
OriginInfo: &cos.BucketOriginInfo{
|
||||
HostInfo: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
_, err := c.Bucket.PutOrigin(context.Background(), opt)
|
||||
log_status(err)
|
||||
res, _, err := c.Bucket.GetOrigin(context.Background())
|
||||
log_status(err)
|
||||
fmt.Printf("%+v\n", res)
|
||||
fmt.Printf("%+v\n", res.Rule)
|
||||
_, err = c.Bucket.DeleteOrigin(context.Background())
|
||||
log_status(err)
|
||||
}
|
||||
75
example/object/tagging.go
Normal file
75
example/object/tagging.go
Normal file
@@ -0,0 +1,75 @@
|
||||
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 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,
|
||||
},
|
||||
},
|
||||
})
|
||||
name := "test"
|
||||
|
||||
opt := &cos.ObjectPutTaggingOptions{
|
||||
TagSet: []cos.ObjectTaggingTag{
|
||||
{
|
||||
Key: "test_k2",
|
||||
Value: "test_v2",
|
||||
},
|
||||
{
|
||||
Key: "test_k3",
|
||||
Value: "test_v3",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
_, err := c.Object.PutTagging(context.Background(), name, opt)
|
||||
log_status(err)
|
||||
|
||||
res, _, err := c.Object.GetTagging(context.Background(), name)
|
||||
log_status(err)
|
||||
fmt.Printf("%v\n", res.TagSet)
|
||||
|
||||
_, err = c.Object.DeleteTagging(context.Background(), name)
|
||||
log_status(err)
|
||||
}
|
||||
96
example/sts/sts_v3.go
Normal file
96
example/sts/sts_v3.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
"github.com/tencentyun/qcloud-cos-sts-sdk/go"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
appid := "1259654469"
|
||||
bucket := "test-1259654469"
|
||||
c := sts.NewClient(
|
||||
os.Getenv("COS_SECRETID"),
|
||||
os.Getenv("COS_SECRETKEY"),
|
||||
nil,
|
||||
)
|
||||
opt := &sts.CredentialOptions{
|
||||
DurationSeconds: int64(time.Hour.Seconds()),
|
||||
Region: "ap-guangzhou",
|
||||
Policy: &sts.CredentialPolicy{
|
||||
Statement: []sts.CredentialPolicyStatement{
|
||||
{
|
||||
Action: []string{
|
||||
"name/cos:PostObject",
|
||||
"name/cos:PutObject",
|
||||
"name/cos:GetObject",
|
||||
},
|
||||
Effect: "allow",
|
||||
Resource: []string{
|
||||
//这里改成允许的路径前缀,可以根据自己网站的用户登录态判断允许上传的具体路径,例子: a.jpg 或者 a/* 或者 * (使用通配符*存在重大安全风险, 请谨慎评估使用)
|
||||
"qcs::cos:ap-guangzhou:uid/" + appid + ":" + bucket + "/exampleobject",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
res, err := c.GetCredential(opt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("%+v\n", res.Credentials)
|
||||
|
||||
//获取临时ak、sk、token
|
||||
tAk := res.Credentials.TmpSecretID
|
||||
tSk := res.Credentials.TmpSecretKey
|
||||
token := res.Credentials.SessionToken
|
||||
|
||||
u, _ := url.Parse("https://" + bucket + ".cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
client := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
// 使用临时密钥
|
||||
SecretID: tAk,
|
||||
SecretKey: tSk,
|
||||
SessionToken: token,
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
name := "exampleobject"
|
||||
f := strings.NewReader("test")
|
||||
|
||||
_, err = client.Object.Put(context.Background(), name, f, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
name = "exampleobject"
|
||||
f = strings.NewReader("test xxx")
|
||||
optc := &cos.ObjectPutOptions{
|
||||
ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
|
||||
ContentType: "text/html",
|
||||
},
|
||||
ACLHeaderOptions: &cos.ACLHeaderOptions{
|
||||
//XCosACL: "public-read",
|
||||
XCosACL: "private",
|
||||
},
|
||||
}
|
||||
_, err = client.Object.Put(context.Background(), name, f, optc)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user