agin719
5 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
56 changed files with 2419 additions and 282 deletions
-
1auth.go
-
177batch.go
-
20batch_test.go
-
58bucket.go
-
51bucket_encryption.go
-
100bucket_encryption_test.go
-
90bucket_origin.go
-
71bucket_policy.go
-
40bucket_referer.go
-
88bucket_referer_test.go
-
105bucket_test.go
-
24ci.go
-
4cos.go
-
56costesting/ci_test.go
-
24error.go
-
9example/batch/create_job.go
-
105example/batch/create_job_restore.go
-
35example/bucket/delPolicy.go
-
68example/bucket/encryption.go
-
38example/bucket/getLogging.go
-
64example/bucket/getObjectVersion.go
-
39example/bucket/getPolicy.go
-
92example/bucket/origin.go
-
63example/bucket/putPolicy.go
-
67example/bucket/referer.go
-
23example/object/MultiUpload.go
-
27example/object/abortMultipartUpload.go
-
70example/object/ci_put.go
-
35example/object/completeMultipartUpload.go
-
36example/object/copy.go
-
31example/object/copyPart.go
-
26example/object/delete.go
-
27example/object/deleteMultiple.go
-
42example/object/get.go
-
25example/object/getACL.go
-
24example/object/getAnonymous.go
-
32example/object/getByPresignedURL.go
-
24example/object/head.go
-
23example/object/initiateMultipartUpload.go
-
31example/object/listParts.go
-
26example/object/options.go
-
44example/object/presigned_url_with_token.go
-
35example/object/put.go
-
28example/object/putACL.go
-
24example/object/restore.go
-
81example/object/sse_c.go
-
75example/object/sse_cos.go
-
75example/object/tagging.go
-
23example/object/upload.go
-
29example/object/uploadFile.go
-
27example/object/uploadPart.go
-
96example/sts/sts_v3.go
-
1go.mod
-
2go.sum
-
88object.go
@ -0,0 +1,51 @@ |
|||
package cos |
|||
|
|||
import ( |
|||
"context" |
|||
"encoding/xml" |
|||
"net/http" |
|||
) |
|||
|
|||
type BucketEncryptionConfiguration struct { |
|||
SSEAlgorithm string `xml:"SSEAlgorithm"` |
|||
} |
|||
|
|||
type BucketPutEncryptionOptions struct { |
|||
XMLName xml.Name `xml:"ServerSideEncryptionConfiguration"` |
|||
Rule *BucketEncryptionConfiguration `xml:"Rule>ApplySideEncryptionConfiguration"` |
|||
} |
|||
|
|||
type BucketGetEncryptionResult BucketPutEncryptionOptions |
|||
|
|||
func (s *BucketService) PutEncryption(ctx context.Context, opt *BucketPutEncryptionOptions) (*Response, error) { |
|||
sendOpt := &sendOptions{ |
|||
baseURL: s.client.BaseURL.BucketURL, |
|||
uri: "/?encryption", |
|||
method: http.MethodPut, |
|||
body: opt, |
|||
} |
|||
resp, err := s.client.send(ctx, sendOpt) |
|||
return resp, err |
|||
} |
|||
|
|||
func (s *BucketService) GetEncryption(ctx context.Context) (*BucketGetEncryptionResult, *Response, error) { |
|||
var res BucketGetEncryptionResult |
|||
sendOpt := &sendOptions{ |
|||
baseURL: s.client.BaseURL.BucketURL, |
|||
uri: "/?encryption", |
|||
method: http.MethodGet, |
|||
result: &res, |
|||
} |
|||
resp, err := s.client.send(ctx, sendOpt) |
|||
return &res, resp, err |
|||
} |
|||
|
|||
func (s *BucketService) DeleteEncryption(ctx context.Context) (*Response, error) { |
|||
sendOpt := &sendOptions{ |
|||
baseURL: s.client.BaseURL.BucketURL, |
|||
uri: "/?encryption", |
|||
method: http.MethodDelete, |
|||
} |
|||
resp, err := s.client.send(ctx, sendOpt) |
|||
return resp, err |
|||
} |
@ -0,0 +1,100 @@ |
|||
package cos |
|||
|
|||
import ( |
|||
"context" |
|||
"encoding/xml" |
|||
"fmt" |
|||
"net/http" |
|||
"reflect" |
|||
"testing" |
|||
) |
|||
|
|||
func TestBucketService_GetEncryption(t *testing.T) { |
|||
setup() |
|||
defer teardown() |
|||
|
|||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
|||
testMethod(t, r, "GET") |
|||
vs := values{ |
|||
"encryption": "", |
|||
} |
|||
testFormValues(t, r, vs) |
|||
fmt.Fprint(w, `<ServerSideEncryptionConfiguration> |
|||
<Rule> |
|||
<ApplySideEncryptionConfiguration> |
|||
<SSEAlgorithm>AES256</SSEAlgorithm> |
|||
</ApplySideEncryptionConfiguration> |
|||
</Rule> |
|||
</ServerSideEncryptionConfiguration>`) |
|||
|
|||
}) |
|||
|
|||
res, _, err := client.Bucket.GetEncryption(context.Background()) |
|||
if err != nil { |
|||
t.Fatalf("Bucket.GetEncryption returned error %v", err) |
|||
} |
|||
|
|||
want := &BucketGetEncryptionResult{ |
|||
XMLName: xml.Name{Local: "ServerSideEncryptionConfiguration"}, |
|||
Rule: &BucketEncryptionConfiguration{ |
|||
SSEAlgorithm: "AES256", |
|||
}, |
|||
} |
|||
|
|||
if !reflect.DeepEqual(res, want) { |
|||
t.Errorf("Bucket.GetEncryption returned %+v, want %+v", res, want) |
|||
} |
|||
} |
|||
|
|||
func TestBucketService_PutEncryption(t *testing.T) { |
|||
setup() |
|||
defer teardown() |
|||
|
|||
opt := &BucketPutEncryptionOptions{ |
|||
Rule: &BucketEncryptionConfiguration{ |
|||
SSEAlgorithm: "AES256", |
|||
}, |
|||
} |
|||
|
|||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
|||
testMethod(t, r, "PUT") |
|||
vs := values{ |
|||
"encryption": "", |
|||
} |
|||
testFormValues(t, r, vs) |
|||
|
|||
body := new(BucketPutEncryptionOptions) |
|||
xml.NewDecoder(r.Body).Decode(body) |
|||
want := opt |
|||
want.XMLName = xml.Name{Local: "ServerSideEncryptionConfiguration"} |
|||
if !reflect.DeepEqual(body, want) { |
|||
t.Errorf("Bucket.PutEncryption request\n body: %+v\n, want %+v\n", body, want) |
|||
} |
|||
}) |
|||
|
|||
_, err := client.Bucket.PutEncryption(context.Background(), opt) |
|||
if err != nil { |
|||
t.Fatalf("Bucket.PutEncryption returned error: %v", err) |
|||
} |
|||
} |
|||
|
|||
func TestBucketService_DeleteEncryption(t *testing.T) { |
|||
setup() |
|||
defer teardown() |
|||
|
|||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
|||
testMethod(t, r, http.MethodDelete) |
|||
vs := values{ |
|||
"encryption": "", |
|||
} |
|||
testFormValues(t, r, vs) |
|||
|
|||
w.WriteHeader(http.StatusNoContent) |
|||
}) |
|||
|
|||
_, err := client.Bucket.DeleteEncryption(context.Background()) |
|||
if err != nil { |
|||
t.Fatalf("Bucket.DeleteEncryption returned error: %v", err) |
|||
} |
|||
|
|||
} |
@ -0,0 +1,90 @@ |
|||
package cos |
|||
|
|||
import ( |
|||
"context" |
|||
"encoding/xml" |
|||
"net/http" |
|||
) |
|||
|
|||
type BucketPutOriginOptions struct { |
|||
XMLName xml.Name `xml:"OriginConfiguration"` |
|||
Rule []BucketOriginRule `xml:"OriginRule"` |
|||
} |
|||
|
|||
type BucketOriginRule struct { |
|||
OriginType string `xml:"OriginType"` |
|||
OriginCondition *BucketOriginCondition `xml:"OriginCondition"` |
|||
OriginParameter *BucketOriginParameter `xml:"OriginParameter"` |
|||
OriginInfo *BucketOriginInfo `xml:"OriginInfo"` |
|||
} |
|||
|
|||
type BucketOriginCondition struct { |
|||
HTTPStatusCode string `xml:"HTTPStatusCode,omitempty"` |
|||
Prefix string `xml:"Prefix,omitempty"` |
|||
} |
|||
|
|||
type BucketOriginParameter struct { |
|||
Protocol string `xml:"Protocol,omitempty"` |
|||
FollowQueryString bool `xml:"FollowQueryString,omitempty"` |
|||
HttpHeader *BucketOriginHttpHeader `xml:"HttpHeader,omitempty"` |
|||
FollowRedirection bool `xml:"FollowRedirection,omitempty"` |
|||
HttpRedirectCode string `xml:"HttpRedirectCode,omitempty"` |
|||
CopyOriginData bool `xml:"CopyOriginData,omitempty"` |
|||
} |
|||
|
|||
type BucketOriginHttpHeader struct { |
|||
// 目前还不支持 FollowAllHeaders
|
|||
// FollowAllHeaders bool `xml:"FollowAllHeaders,omitempty"`
|
|||
NewHttpHeaders []OriginHttpHeader `xml:"NewHttpHeaders>Header,omitempty"` |
|||
FollowHttpHeaders []OriginHttpHeader `xml:"FollowHttpHeaders>Header,omitempty"` |
|||
} |
|||
|
|||
type OriginHttpHeader struct { |
|||
Key string `xml:"Key,omitempty"` |
|||
Value string `xml:"Value,omitempty"` |
|||
} |
|||
|
|||
type BucketOriginInfo struct { |
|||
HostInfo string `xml:"HostInfo>HostName,omitempty"` |
|||
FileInfo *BucketOriginFileInfo `xml:"FileInfo,omitempty"` |
|||
} |
|||
type BucketOriginFileInfo struct { |
|||
PrefixDirective bool `xml:"PrefixDirective,omitempty"` |
|||
Prefix string `xml:"Prefix,omitempty"` |
|||
Suffix string `xml:"Suffix,omitempty"` |
|||
} |
|||
|
|||
type BucketGetOriginResult BucketPutOriginOptions |
|||
|
|||
func (s *BucketService) PutOrigin(ctx context.Context, opt *BucketPutOriginOptions) (*Response, error) { |
|||
sendOpt := &sendOptions{ |
|||
baseURL: s.client.BaseURL.BucketURL, |
|||
uri: "/?origin", |
|||
method: http.MethodPut, |
|||
body: opt, |
|||
} |
|||
resp, err := s.client.send(ctx, sendOpt) |
|||
return resp, err |
|||
} |
|||
|
|||
func (s *BucketService) GetOrigin(ctx context.Context) (*BucketGetOriginResult, *Response, error) { |
|||
var res BucketGetOriginResult |
|||
sendOpt := &sendOptions{ |
|||
baseURL: s.client.BaseURL.BucketURL, |
|||
uri: "/?origin", |
|||
method: http.MethodGet, |
|||
result: &res, |
|||
} |
|||
resp, err := s.client.send(ctx, sendOpt) |
|||
return &res, resp, err |
|||
} |
|||
|
|||
func (s *BucketService) DeleteOrigin(ctx context.Context) (*Response, error) { |
|||
sendOpt := &sendOptions{ |
|||
baseURL: s.client.BaseURL.BucketURL, |
|||
uri: "/?origin", |
|||
method: http.MethodDelete, |
|||
} |
|||
resp, err := s.client.send(ctx, sendOpt) |
|||
return resp, err |
|||
} |
@ -0,0 +1,71 @@ |
|||
package cos |
|||
|
|||
import ( |
|||
"bytes" |
|||
"context" |
|||
"encoding/json" |
|||
"net/http" |
|||
"strings" |
|||
) |
|||
|
|||
type BucketStatement struct { |
|||
Principal map[string][]string `json:"principal,omitempty"` |
|||
Action []string `json:"action,omitempty"` |
|||
Effect string `json:"effect,omitempty"` |
|||
Resource []string `json:"resource,omitempty"` |
|||
Condition map[string]map[string]interface{} `json:"condition,omitempty"` |
|||
} |
|||
|
|||
type BucketPutPolicyOptions struct { |
|||
Statement []BucketStatement `json:"statement,omitempty"` |
|||
Version string `json:"version,omitempty"` |
|||
Principal map[string][]string `json:"principal,omitempty"` |
|||
} |
|||
|
|||
type BucketGetPolicyResult BucketPutPolicyOptions |
|||
|
|||
func (s *BucketService) PutPolicy(ctx context.Context, opt *BucketPutPolicyOptions) (*Response, error) { |
|||
var f *strings.Reader |
|||
if opt != nil { |
|||
bs, err := json.Marshal(opt) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
body := string(bs) |
|||
f = strings.NewReader(body) |
|||
} |
|||
sendOpt := &sendOptions{ |
|||
baseURL: s.client.BaseURL.BucketURL, |
|||
uri: "/?policy", |
|||
method: http.MethodPut, |
|||
body: f, |
|||
} |
|||
resp, err := s.client.send(ctx, sendOpt) |
|||
return resp, err |
|||
} |
|||
|
|||
func (s *BucketService) GetPolicy(ctx context.Context) (*BucketGetPolicyResult, *Response, error) { |
|||
var bs bytes.Buffer |
|||
var res BucketGetPolicyResult |
|||
sendOpt := &sendOptions{ |
|||
baseURL: s.client.BaseURL.BucketURL, |
|||
uri: "/?policy", |
|||
method: http.MethodGet, |
|||
result: &bs, |
|||
} |
|||
resp, err := s.client.send(ctx, sendOpt) |
|||
if err == nil { |
|||
err = json.Unmarshal(bs.Bytes(), &res) |
|||
} |
|||
return &res, resp, err |
|||
} |
|||
|
|||
func (s *BucketService) DeletePolicy(ctx context.Context) (*Response, error) { |
|||
sendOpt := &sendOptions{ |
|||
baseURL: s.client.BaseURL.BucketURL, |
|||
uri: "/?policy", |
|||
method: http.MethodDelete, |
|||
} |
|||
resp, err := s.client.send(ctx, sendOpt) |
|||
return resp, err |
|||
} |
@ -0,0 +1,40 @@ |
|||
package cos |
|||
|
|||
import ( |
|||
"context" |
|||
"encoding/xml" |
|||
"net/http" |
|||
) |
|||
|
|||
type BucketPutRefererOptions struct { |
|||
XMLName xml.Name `xml:"RefererConfiguration"` |
|||
Status string `xml:"Status"` |
|||
RefererType string `xml:"RefererType"` |
|||
DomainList []string `xml:"DomainList>Domain"` |
|||
EmptyReferConfiguration string `xml:"EmptyReferConfiguration,omitempty"` |
|||
} |
|||
|
|||
type BucketGetRefererResult BucketPutRefererOptions |
|||
|
|||
func (s *BucketService) PutReferer(ctx context.Context, opt *BucketPutRefererOptions) (*Response, error) { |
|||
sendOpt := &sendOptions{ |
|||
baseURL: s.client.BaseURL.BucketURL, |
|||
uri: "/?referer", |
|||
method: http.MethodPut, |
|||
body: opt, |
|||
} |
|||
resp, err := s.client.send(ctx, sendOpt) |
|||
return resp, err |
|||
} |
|||
|
|||
func (s *BucketService) GetReferer(ctx context.Context) (*BucketGetRefererResult, *Response, error) { |
|||
var res BucketGetRefererResult |
|||
sendOpt := &sendOptions{ |
|||
baseURL: s.client.BaseURL.BucketURL, |
|||
uri: "/?referer", |
|||
method: http.MethodGet, |
|||
result: &res, |
|||
} |
|||
resp, err := s.client.send(ctx, sendOpt) |
|||
return &res, resp, err |
|||
} |
@ -0,0 +1,88 @@ |
|||
package cos |
|||
|
|||
import ( |
|||
"context" |
|||
"encoding/xml" |
|||
"fmt" |
|||
"net/http" |
|||
"reflect" |
|||
"testing" |
|||
) |
|||
|
|||
func TestBucketService_GetReferer(t *testing.T) { |
|||
setup() |
|||
defer teardown() |
|||
|
|||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
|||
testMethod(t, r, "GET") |
|||
vs := values{ |
|||
"referer": "", |
|||
} |
|||
testFormValues(t, r, vs) |
|||
fmt.Fprint(w, `<RefererConfiguration> |
|||
<Status>Enabled</Status> |
|||
<RefererType>White-List</RefererType> |
|||
<DomainList> |
|||
<Domain>*.qq.com</Domain> |
|||
<Domain>*.qcloud.com</Domain> |
|||
</DomainList> |
|||
<EmptyReferConfiguration>Allow</EmptyReferConfiguration> |
|||
</RefererConfiguration>`) |
|||
}) |
|||
|
|||
res, _, err := client.Bucket.GetReferer(context.Background()) |
|||
if err != nil { |
|||
t.Fatalf("Bucket.GetReferer returned error %v", err) |
|||
} |
|||
|
|||
want := &BucketGetRefererResult{ |
|||
XMLName: xml.Name{Local: "RefererConfiguration"}, |
|||
Status: "Enabled", |
|||
RefererType: "White-List", |
|||
DomainList: []string{ |
|||
"*.qq.com", |
|||
"*.qcloud.com", |
|||
}, |
|||
EmptyReferConfiguration: "Allow", |
|||
} |
|||
|
|||
if !reflect.DeepEqual(res, want) { |
|||
t.Errorf("Bucket.GetReferer returned %+v, want %+v", res, want) |
|||
} |
|||
} |
|||
|
|||
func TestBucketService_PutReferer(t *testing.T) { |
|||
setup() |
|||
defer teardown() |
|||
|
|||
opt := &BucketPutRefererOptions{ |
|||
Status: "Enabled", |
|||
RefererType: "White-List", |
|||
DomainList: []string{ |
|||
"*.qq.com", |
|||
"*.qcloud.com", |
|||
}, |
|||
EmptyReferConfiguration: "Allow", |
|||
} |
|||
|
|||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
|||
testMethod(t, r, "PUT") |
|||
vs := values{ |
|||
"referer": "", |
|||
} |
|||
testFormValues(t, r, vs) |
|||
|
|||
body := new(BucketPutRefererOptions) |
|||
xml.NewDecoder(r.Body).Decode(body) |
|||
want := opt |
|||
want.XMLName = xml.Name{Local: "RefererConfiguration"} |
|||
if !reflect.DeepEqual(body, want) { |
|||
t.Errorf("Bucket.PutReferer request\n body: %+v\nwant %+v\n", body, want) |
|||
} |
|||
}) |
|||
|
|||
_, err := client.Bucket.PutReferer(context.Background(), opt) |
|||
if err != nil { |
|||
t.Fatalf("Bucket.PutReferer returned error: %v", err) |
|||
} |
|||
} |
@ -0,0 +1,24 @@ |
|||
package cos |
|||
|
|||
import ( |
|||
"encoding/json" |
|||
) |
|||
|
|||
type PicOperations struct { |
|||
IsPicInfo int `json:"is_pic_info,omitempty"` |
|||
Rules []PicOperationsRules `json:"rules,omitemtpy"` |
|||
} |
|||
|
|||
type PicOperationsRules struct { |
|||
Bucket string `json:"bucket,omitempty"` |
|||
FileId string `json:"fileid"` |
|||
Rule string `json:"rule"` |
|||
} |
|||
|
|||
func EncodePicOperations(pic *PicOperations) string { |
|||
bs, err := json.Marshal(pic) |
|||
if err != nil { |
|||
return "" |
|||
} |
|||
return string(bs) |
|||
} |
@ -0,0 +1,105 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"context" |
|||
"fmt" |
|||
"net/http" |
|||
"net/url" |
|||
"os" |
|||
"strings" |
|||
|
|||
"github.com/google/uuid" |
|||
"github.com/tencentyun/cos-go-sdk-v5" |
|||
"github.com/tencentyun/cos-go-sdk-v5/debug" |
|||
) |
|||
|
|||
func main() { |
|||
test_batch_bucket := "testcd-1259654469" |
|||
appid := 1259654469 |
|||
uin := "100010805041" |
|||
region := "ap-chengdu" |
|||
|
|||
// bucket url:<Bucketname-Appid>.cos.<region>.mycloud.com
|
|||
bucketurl, _ := url.Parse("https://" + test_batch_bucket + ".cos." + region + ".myqcloud.com") |
|||
// batch url:<uin>.cos-control.<region>.myqcloud.ccom
|
|||
batchurl, _ := url.Parse("https://" + uin + ".cos-control." + region + ".myqcloud.com") |
|||
|
|||
b := &cos.BaseURL{BucketURL: bucketurl, BatchURL: batchurl} |
|||
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, |
|||
}, |
|||
}, |
|||
}) |
|||
|
|||
// 创建需要归档恢复的文件
|
|||
source_name := "test/restore.txt" |
|||
sf := strings.NewReader("batch test content") |
|||
objopt := &cos.ObjectPutOptions{ |
|||
nil, |
|||
&cos.ObjectPutHeaderOptions{ |
|||
XCosStorageClass: "Archive", |
|||
}, |
|||
} |
|||
_, err := c.Object.Put(context.Background(), source_name, sf, objopt) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
|
|||
// 创建清单文件
|
|||
manifest_name := "test/manifest.csv" |
|||
f := strings.NewReader(test_batch_bucket + "," + source_name) |
|||
resp, err := c.Object.Put(context.Background(), manifest_name, f, nil) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
etag := resp.Header.Get("ETag") |
|||
|
|||
uuid_str := uuid.New().String() |
|||
opt := &cos.BatchCreateJobOptions{ |
|||
ClientRequestToken: uuid_str, |
|||
ConfirmationRequired: "true", |
|||
Description: "test batch", |
|||
Manifest: &cos.BatchJobManifest{ |
|||
Location: &cos.BatchJobManifestLocation{ |
|||
ETag: etag, |
|||
ObjectArn: "qcs::cos:" + region + "::" + test_batch_bucket + "/" + manifest_name, |
|||
}, |
|||
Spec: &cos.BatchJobManifestSpec{ |
|||
Fields: []string{"Bucket", "Key"}, |
|||
Format: "COSBatchOperations_CSV_V1", |
|||
}, |
|||
}, |
|||
Operation: &cos.BatchJobOperation{ |
|||
RestoreObject: &cos.BatchInitiateRestoreObject{ |
|||
ExpirationInDays: 3, |
|||
JobTier: "Standard", |
|||
}, |
|||
}, |
|||
Priority: 1, |
|||
Report: &cos.BatchJobReport{ |
|||
Bucket: "qcs::cos:" + region + "::" + test_batch_bucket, |
|||
Enabled: "true", |
|||
Format: "Report_CSV_V1", |
|||
Prefix: "job-result", |
|||
ReportScope: "AllTasks", |
|||
}, |
|||
RoleArn: "qcs::cam::uin/" + uin + ":roleName/COSBatch_QcsRole", |
|||
} |
|||
headers := &cos.BatchRequestHeaders{ |
|||
XCosAppid: appid, |
|||
} |
|||
|
|||
res, _, err := c.Batch.CreateJob(context.Background(), opt, headers) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
fmt.Println(res) |
|||
|
|||
} |
@ -0,0 +1,35 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"context" |
|||
"net/http" |
|||
"net/url" |
|||
"os" |
|||
|
|||
"github.com/tencentyun/cos-go-sdk-v5" |
|||
"github.com/tencentyun/cos-go-sdk-v5/debug" |
|||
) |
|||
|
|||
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, |
|||
}, |
|||
}, |
|||
}) |
|||
|
|||
_, err := c.Bucket.DeletePolicy(context.Background()) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
} |
@ -0,0 +1,68 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"context" |
|||
"encoding/xml" |
|||
"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.BucketPutEncryptionOptions{ |
|||
XMLName: xml.Name{Local: "ServerSideEncryptionConfiguration"}, |
|||
Rule: &cos.BucketEncryptionConfiguration{ |
|||
SSEAlgorithm: "AES256", |
|||
}, |
|||
} |
|||
|
|||
_, err := c.Bucket.PutEncryption(context.Background(), opt) |
|||
log_status(err) |
|||
|
|||
res, _, err := c.Bucket.GetEncryption(context.Background()) |
|||
log_status(err) |
|||
fmt.Printf("%+v\n", res) |
|||
|
|||
_, err = c.Bucket.DeleteEncryption(context.Background()) |
|||
log_status(err) |
|||
} |
@ -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) |
|||
} |
@ -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) |
|||
} |
|||
|
|||
} |
@ -0,0 +1,39 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"context" |
|||
"encoding/json" |
|||
"fmt" |
|||
"net/http" |
|||
"net/url" |
|||
"os" |
|||
|
|||
"github.com/tencentyun/cos-go-sdk-v5" |
|||
"github.com/tencentyun/cos-go-sdk-v5/debug" |
|||
) |
|||
|
|||
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, |
|||
}, |
|||
}, |
|||
}) |
|||
|
|||
res, _, err := c.Bucket.GetPolicy(context.Background()) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
bs, err := json.Marshal(res) |
|||
fmt.Println(string(bs)) |
|||
} |
@ -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) |
|||
} |
@ -0,0 +1,63 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"context" |
|||
"net/http" |
|||
"net/url" |
|||
"os" |
|||
|
|||
"github.com/tencentyun/cos-go-sdk-v5" |
|||
"github.com/tencentyun/cos-go-sdk-v5/debug" |
|||
) |
|||
|
|||
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.BucketPutPolicyOptions{ |
|||
Version: "2.0", |
|||
Statement: []cos.BucketStatement{ |
|||
{ |
|||
Principal: map[string][]string{ |
|||
"qcs": []string{ |
|||
"qcs::cam::uin/100000000001:uin/100000000011", //替换成您想授予权限的账户uin
|
|||
}, |
|||
}, |
|||
Action: []string{ |
|||
"name/cos:GetObject", |
|||
}, |
|||
Effect: "allow", |
|||
Resource: []string{ |
|||
//这里改成允许的路径前缀,可以根据自己网站的用户登录态判断允许上传的具体路径,例子: a.jpg 或者 a/* 或者 * (使用通配符*存在重大安全风险, 请谨慎评估使用)
|
|||
"qcs::cos:ap-guangzhou:uid/1259654469:test-1259654469/exampleobject", |
|||
}, |
|||
Condition: map[string]map[string]interface{}{ |
|||
"ip_not_equal": map[string]interface{}{ |
|||
"qcs:ip": []string{ |
|||
"192.168.1.1", |
|||
}, |
|||
}, |
|||
}, |
|||
}, |
|||
}, |
|||
} |
|||
|
|||
_, err := c.Bucket.PutPolicy(context.Background(), opt) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
} |
@ -0,0 +1,67 @@ |
|||
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.BucketPutRefererOptions{ |
|||
Status: "Enabled", |
|||
RefererType: "White-List", |
|||
DomainList: []string{ |
|||
"*.qq.com", |
|||
"*.qcloud.com", |
|||
}, |
|||
EmptyReferConfiguration: "Allow", |
|||
} |
|||
|
|||
_, err := c.Bucket.PutReferer(context.Background(), opt) |
|||
log_status(err) |
|||
|
|||
res, _, err := c.Bucket.GetReferer(context.Background()) |
|||
log_status(err) |
|||
fmt.Printf("%+v\n", res) |
|||
} |
@ -0,0 +1,70 @@ |
|||
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("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, |
|||
// Notice when put a large file and set need the request body, might happend out of memory error.
|
|||
RequestBody: false, |
|||
ResponseHeader: true, |
|||
ResponseBody: true, |
|||
}, |
|||
}, |
|||
}) |
|||
|
|||
opt := &cos.ObjectPutOptions{ |
|||
nil, |
|||
&cos.ObjectPutHeaderOptions{ |
|||
XOptionHeader: &http.Header{}, |
|||
}, |
|||
} |
|||
pic := &cos.PicOperations{ |
|||
IsPicInfo: 1, |
|||
Rules: []cos.PicOperationsRules{ |
|||
{ |
|||
FileId: "format.jpg", |
|||
Rule: "imageView2/format/png", |
|||
}, |
|||
}, |
|||
} |
|||
opt.XOptionHeader.Add("Pic-Operations", cos.EncodePicOperations(pic)) |
|||
name := "test.jpg" |
|||
local_filename := "./test.jpg" |
|||
_, err := c.Object.PutFromFile(context.Background(), name, local_filename, opt) |
|||
log_status(err) |
|||
} |
@ -0,0 +1,44 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"context" |
|||
"fmt" |
|||
"net/http" |
|||
"net/url" |
|||
"os" |
|||
"time" |
|||
|
|||
"github.com/tencentyun/cos-go-sdk-v5" |
|||
) |
|||
|
|||
type URLToken struct { |
|||
SessionToken string `url:"x-cos-security-token,omitempty" header:"-"` |
|||
} |
|||
|
|||
func main() { |
|||
// 替换成您的临时密钥
|
|||
tak := os.Getenv("COS_SECRETID") |
|||
tsk := os.Getenv("COS_SECRETKEY") |
|||
token := &URLToken{ |
|||
SessionToken: "<token>", |
|||
} |
|||
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com") |
|||
b := &cos.BaseURL{BucketURL: u} |
|||
c := cos.NewClient(b, &http.Client{}) |
|||
|
|||
name := "exampleobject" |
|||
ctx := context.Background() |
|||
|
|||
// Get presigned
|
|||
presignedURL, err := c.Object.GetPresignedURL(ctx, http.MethodGet, name, tak, tsk, time.Hour, token) |
|||
if err != nil { |
|||
fmt.Printf("Error: %v\n", err) |
|||
return |
|||
} |
|||
// Get object by presinged url
|
|||
_, err = http.Get(presignedURL.String()) |
|||
if err != nil { |
|||
fmt.Printf("Error: %v\n", err) |
|||
} |
|||
fmt.Println(presignedURL.String()) |
|||
} |
@ -0,0 +1,81 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"context" |
|||
"errors" |
|||
"fmt" |
|||
"io/ioutil" |
|||
"net/http" |
|||
"net/url" |
|||
"os" |
|||
"strings" |
|||
|
|||
"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://testcd-1259654469.cos.ap-chengdu.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, |
|||
// Notice when put a large file and set need the request body, might happend out of memory error.
|
|||
RequestBody: false, |
|||
ResponseHeader: true, |
|||
ResponseBody: true, |
|||
}, |
|||
}, |
|||
}) |
|||
opt := &cos.ObjectPutOptions{ |
|||
ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{ |
|||
ContentType: "text/html", |
|||
XCosSSECustomerAglo: "AES256", |
|||
XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=", |
|||
XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==", |
|||
}, |
|||
ACLHeaderOptions: &cos.ACLHeaderOptions{}, |
|||
} |
|||
name := "PutFromGoWithSSE-C" |
|||
content := "Put Object From Go With SSE-C" |
|||
f := strings.NewReader(content) |
|||
_, err := c.Object.Put(context.Background(), name, f, opt) |
|||
log_status(err) |
|||
|
|||
getopt := &cos.ObjectGetOptions{ |
|||
XCosSSECustomerAglo: "AES256", |
|||
XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=", |
|||
XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==", |
|||
} |
|||
var resp *cos.Response |
|||
resp, err = c.Object.Get(context.Background(), name, getopt) |
|||
log_status(err) |
|||
|
|||
bodyBytes, _ := ioutil.ReadAll(resp.Body) |
|||
bodyContent := string(bodyBytes) |
|||
if bodyContent != content { |
|||
log_status(errors.New("Content inconsistency")) |
|||
} |
|||
} |
@ -0,0 +1,75 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"context" |
|||
"errors" |
|||
"fmt" |
|||
"io/ioutil" |
|||
"net/http" |
|||
"net/url" |
|||
"os" |
|||
"strings" |
|||
|
|||
"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, |
|||
// Notice when put a large file and set need the request body, might happend out of memory error.
|
|||
RequestBody: false, |
|||
ResponseHeader: true, |
|||
ResponseBody: true, |
|||
}, |
|||
}, |
|||
}) |
|||
opt := &cos.ObjectPutOptions{ |
|||
ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{ |
|||
ContentType: "text/html", |
|||
XCosServerSideEncryption: "AES256", |
|||
}, |
|||
ACLHeaderOptions: &cos.ACLHeaderOptions{}, |
|||
} |
|||
name := "PutFromGoWithSSE-COS" |
|||
content := "Put Object From Go With SSE-COS" |
|||
f := strings.NewReader(content) |
|||
_, err := c.Object.Put(context.Background(), name, f, opt) |
|||
log_status(err) |
|||
|
|||
getopt := &cos.ObjectGetOptions{} |
|||
var resp *cos.Response |
|||
resp, err = c.Object.Get(context.Background(), name, getopt) |
|||
log_status(err) |
|||
|
|||
bodyBytes, _ := ioutil.ReadAll(resp.Body) |
|||
bodyContent := string(bodyBytes) |
|||
if bodyContent != content { |
|||
log_status(errors.New("Content inconsistency")) |
|||
} |
|||
} |
@ -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) |
|||
} |
@ -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) |
|||
} |
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue