├── .bumpversion.cfg
├── .github
└── workflows
│ └── auto-changelog.yml
├── .gitignore
├── .travis.yml
├── CHANGELOG.md
├── LICENSE
├── Makefile
├── README.md
├── auth.go
├── auth_test.go
├── batch.go
├── batch_test.go
├── bucket.go
├── bucket_accelerate.go
├── bucket_accelerate_test.go
├── bucket_acl.go
├── bucket_acl_test.go
├── bucket_cors.go
├── bucket_cors_test.go
├── bucket_domain.go
├── bucket_domain_test.go
├── bucket_encryption.go
├── bucket_encryption_test.go
├── bucket_intelligenttiering.go
├── bucket_intelligenttiering_test.go
├── bucket_inventory.go
├── bucket_inventory_test.go
├── bucket_lifecycle.go
├── bucket_lifecycle_test.go
├── bucket_location.go
├── bucket_location_test.go
├── bucket_logging.go
├── bucket_logging_test.go
├── bucket_object_lock.go
├── bucket_object_lock_test.go
├── bucket_origin.go
├── bucket_origin_test.go
├── bucket_part.go
├── bucket_part_test.go
├── bucket_policy.go
├── bucket_policy_test.go
├── bucket_referer.go
├── bucket_referer_test.go
├── bucket_replication.go
├── bucket_replication_test.go
├── bucket_tagging.go
├── bucket_tagging_test.go
├── bucket_test.go
├── bucket_version.go
├── bucket_version_test.go
├── bucket_website.go
├── bucket_website_test.go
├── ci.go
├── ci_doc.go
├── ci_doc_test.go
├── ci_fileprocess.go
├── ci_fileprocess_test.go
├── ci_media.go
├── ci_media_test.go
├── ci_metainsight.go
├── ci_metainsight_test.go
├── ci_test.go
├── cos.go
├── cos_test.go
├── costesting
└── ci_test.go
├── crypto
├── aes_ctr.go
├── aes_ctr_cipher.go
├── aes_ctr_cipher_test.go
├── cipher.go
├── crypto_object.go
├── crypto_object_part.go
├── crypto_object_part_test.go
├── crypto_object_test.go
├── crypto_type.go
├── master_kms_cipher.go
└── master_kms_cipher_test.go
├── debug
├── http.go
└── http_test.go
├── doc.go
├── error.go
├── error_test.go
├── example
├── CI
│ ├── ai_recognition
│ │ ├── ai_crop_image.go
│ │ ├── ai_enhance_image.go
│ │ ├── ai_game_rec.go
│ │ ├── ai_image_coloring.go
│ │ ├── ai_license_rec.go
│ │ ├── ai_object_detect.go
│ │ ├── ai_recognize.go
│ │ ├── ai_super_resolution.go
│ │ ├── aipic_matting.go
│ │ ├── aiportrait_matting.go
│ │ ├── assess_quality.go
│ │ ├── auto_translation_block.go
│ │ ├── detect_car.go
│ │ ├── detect_face.go
│ │ ├── detect_person.go
│ │ ├── detect_pet.go
│ │ ├── face_effect.go
│ │ ├── goods_matting.go
│ │ ├── image_inspect.go
│ │ ├── image_quality.go
│ │ ├── image_repair.go
│ │ ├── image_search.go
│ │ ├── image_target_rec.go
│ │ ├── ocr.go
│ │ ├── pic_tag.go
│ │ ├── qrcode.go
│ │ └── recognize_logo.go
│ ├── content_auditing
│ │ ├── ci_audio_auditing_job.go
│ │ ├── ci_image_recognition.go
│ │ └── ci_video_auditing_job.go
│ ├── doc_preview
│ │ ├── doc_compare.go
│ │ ├── doc_preview_html.go
│ │ ├── get_doc_preview.go
│ │ └── job.go
│ ├── image_process
│ │ ├── base_process.go
│ │ ├── compression.go
│ │ ├── hot_link.go
│ │ ├── origin_protect.go
│ │ ├── service.go
│ │ ├── style.go
│ │ └── tdc_refresh.go
│ ├── media_process
│ │ ├── generate_token.go
│ │ ├── hls_decrypt_server.go
│ │ ├── livetranscode.go
│ │ ├── media_process.go
│ │ └── playkey.go
│ ├── metainsight
│ │ └── metainsight.go
│ └── workflow_and_job
│ │ ├── buckets.go
│ │ ├── ci_queue.go
│ │ ├── dna.go
│ │ ├── inventory.go
│ │ ├── jobs.go
│ │ ├── poster.go
│ │ ├── queue.go
│ │ ├── template.go
│ │ ├── vocabulary_table.go
│ │ └── workflow.go
├── README.md
├── batch
│ ├── create_job.go
│ ├── create_job_restore.go
│ ├── delete_job.go
│ ├── describe_job.go
│ ├── list_jobs.go
│ ├── update_priority.go
│ └── update_status.go
├── bucket
│ ├── accelerate.go
│ ├── certificate.go
│ ├── delPolicy.go
│ ├── delWebsite.go
│ ├── delete.go
│ ├── deleteCORS.go
│ ├── deleteLifecycle.go
│ ├── deleteReplication.go
│ ├── deleteTagging.go
│ ├── domain.go
│ ├── encryption.go
│ ├── get.go
│ ├── getACL.go
│ ├── getCORS.go
│ ├── getLifecycle.go
│ ├── getLocation.go
│ ├── getLogging.go
│ ├── getMeta.go
│ ├── getObjectVersion.go
│ ├── getPolicy.go
│ ├── getReplication.go
│ ├── getTagging.go
│ ├── getVersioning.go
│ ├── getWebsite.go
│ ├── head.go
│ ├── intelligenttiering.go
│ ├── intelligenttieringv2.go
│ ├── inventory.go
│ ├── is_exist.go
│ ├── listMultipartUploads.go
│ ├── object_lock.go
│ ├── origin.go
│ ├── put.go
│ ├── putACL.go
│ ├── putCORS.go
│ ├── putLifecycle.go
│ ├── putPolicy.go
│ ├── putReplication.go
│ ├── putTagging.go
│ ├── putVersioning.go
│ ├── putWebsite.go
│ └── referer.go
├── crypto
│ └── crypto_sample.go
├── object
│ ├── MultiUpload.go
│ ├── abortMultipartUpload.go
│ ├── append.go
│ ├── batchGet.go
│ ├── batchUpload.go
│ ├── completeMultipartUpload.go
│ ├── copy.go
│ ├── copyPart.go
│ ├── delete.go
│ ├── deleteMultiple.go
│ ├── directory.go
│ ├── download.go
│ ├── fetch_task.go
│ ├── get.go
│ ├── getACL.go
│ ├── getAnonymous.go
│ ├── getByPresignedURL.go
│ ├── getByPresignedURL2.go
│ ├── get_object_url.go
│ ├── get_signature.go
│ ├── get_with_cos_internal.go
│ ├── get_with_credential.go
│ ├── get_with_cvm_role.go
│ ├── get_with_dnscache.go
│ ├── get_with_retry.go
│ ├── get_with_sts.go
│ ├── head.go
│ ├── initiateMultipartUpload.go
│ ├── is_exist.go
│ ├── listParts.go
│ ├── list_uploads.go
│ ├── moveObject.go
│ ├── multicopy.go
│ ├── object_recover.go
│ ├── object_recover2.go
│ ├── options.go
│ ├── presigned_url_with_token.go
│ ├── put.go
│ ├── putACL.go
│ ├── put_from_url.go
│ ├── put_with_proxy.go
│ ├── put_with_timeout.go
│ ├── put_with_transport.go
│ ├── restore.go
│ ├── select.go
│ ├── select_csv.go
│ ├── sse_c.go
│ ├── sse_cos.go
│ ├── symlink.go
│ ├── tagging.go
│ ├── upload.go
│ ├── uploadFile.go
│ └── uploadPart.go
├── service
│ └── get.go
├── sts
│ ├── sts.go
│ └── sts_v3.go
└── test.sh
├── go.mod
├── go.sum
├── helper.go
├── helper_test.go
├── object.go
├── object_acl.go
├── object_acl_test.go
├── object_part.go
├── object_part_test.go
├── object_select.go
├── object_test.go
├── progress.go
├── retry_test.go
├── service.go
├── service_test.go
└── vendor
└── github.com
├── google
└── go-querystring
│ ├── LICENSE
│ ├── README.md
│ ├── go.mod
│ └── query
│ ├── encode.go
│ └── encode_test.go
├── mozillazg
└── go-httpheader
│ ├── CHANGELOG.md
│ ├── LICENSE
│ ├── Makefile
│ ├── README.md
│ ├── encode.go
│ ├── encode_test.go
│ ├── example_test.go
│ └── go.mod
└── stretchr
└── testify
├── Gopkg.lock
├── Gopkg.toml
├── LICENSE
├── README.md
├── _codegen
└── main.go
├── assert
├── assertion_format.go
├── assertion_format.go.tmpl
├── assertion_forward.go
├── assertion_forward.go.tmpl
├── assertions.go
├── assertions_test.go
├── doc.go
├── errors.go
├── forward_assertions.go
├── forward_assertions_test.go
├── http_assertions.go
└── http_assertions_test.go
├── doc.go
├── go.mod
├── go.sum
├── http
├── doc.go
├── test_response_writer.go
└── test_round_tripper.go
├── mock
├── doc.go
├── mock.go
└── mock_test.go
├── package_test.go
├── require
├── doc.go
├── forward_requirements.go
├── forward_requirements_test.go
├── require.go
├── require.go.tmpl
├── require_forward.go
├── require_forward.go.tmpl
├── requirements.go
└── requirements_test.go
├── suite
├── doc.go
├── interfaces.go
├── suite.go
└── suite_test.go
└── vendor
└── github.com
├── davecgh
└── go-spew
│ ├── LICENSE
│ └── spew
│ ├── bypass.go
│ ├── bypasssafe.go
│ ├── common.go
│ ├── config.go
│ ├── doc.go
│ ├── dump.go
│ ├── format.go
│ └── spew.go
├── pmezard
└── go-difflib
│ ├── LICENSE
│ └── difflib
│ └── difflib.go
└── stretchr
└── objx
├── LICENSE
├── accessors.go
├── constants.go
├── conversions.go
├── doc.go
├── map.go
├── mutations.go
├── security.go
├── tests.go
├── type_specific_codegen.go
└── value.go
/.bumpversion.cfg:
--------------------------------------------------------------------------------
1 | [bumpversion]
2 | commit = True
3 | tag = True
4 | current_version = 0.7.0
5 |
6 | [bumpversion:file:cos.go]
7 |
8 |
--------------------------------------------------------------------------------
/.github/workflows/auto-changelog.yml:
--------------------------------------------------------------------------------
1 | name: ChangeLog
2 |
3 | on:
4 | workflow_dispatch:
5 | release:
6 | types: [published]
7 |
8 | jobs:
9 | build:
10 |
11 | runs-on: ubuntu-latest
12 |
13 | steps:
14 | - uses: actions/setup-node@v2-beta
15 | with:
16 | node-version: '12'
17 | - uses: actions/checkout@v2
18 | with:
19 | fetch-depth: 0
20 |
21 | - name: Checkout Tool
22 | uses: actions/checkout@v2
23 | with:
24 | repository: konakonall/auto-changelog
25 | path: 'auto-changelog'
26 | - name: Build Tool
27 | run: |
28 | cd auto-changelog
29 | npm install
30 | npm link
31 |
32 | - name: Generate ChangeLog
33 | env: # Or as an environment variable
34 | TOKEN: ${{ secrets.GITHUB_TOKEN }}
35 | run: |
36 | auto-changelog --token $TOKEN
37 | - name: Cat ChangeLog
38 | run: cat CHANGELOG.md
39 |
40 | - name: Commit files
41 | env:
42 | CI_USER: "gouki0123"
43 | CI_EMAIL: "gouki0123@gmail.com"
44 | run: |
45 | git config --local user.email "$CI_EMAIL"
46 | git config --local user.name "$CI_USER"
47 | git add CHANGELOG.md && git commit -m 'Updated CHANGELOG.md' && echo "push=1" >> $GITHUB_ENV || echo "No changes to CHANGELOG.md"
48 |
49 | - name: Push changes
50 | if: env.push == 1
51 | env:
52 | CI_USER: "gouki0123"
53 | CI_TOKEN: ${{ secrets.GITHUB_TOKEN }}
54 | run: |
55 | git push "https://$CI_USER:$CI_TOKEN@github.com/$GITHUB_REPOSITORY.git" HEAD:master
56 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled Object files, Static and Dynamic libs (Shared Objects)
2 | *.o
3 | *.a
4 | *.so
5 |
6 | # Folders
7 | _obj
8 | _test
9 | .idea
10 | .DS_Store
11 |
12 | # Architecture specific extensions/prefixes
13 | *.[568vq]
14 | [568vq].out
15 |
16 | *.cgo1.go
17 | *.cgo2.c
18 | _cgo_defun.c
19 | _cgo_gotypes.go
20 | _cgo_export.*
21 |
22 | _testmain.go
23 |
24 | *.exe
25 | *.test
26 | *.prof
27 | dist/
28 | cover.html
29 | cover.out
30 | covprofile
31 | coverage.html
32 | example/CI/media_process/test*
33 | example/CI/workflow_and_job/test*
34 | example/CI/metainsight/test*
35 | example/CI/ai_recognition/test*
36 | example/CI/image_process/test*
37 | example/CI/doc_preview/test*
38 | example/CI/content_auditing/test*
39 | .vscode
40 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 mozillazg
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | help:
2 | @echo "test run test"
3 | @echo "lint run lint"
4 | @echo "example run examples"
5 |
6 | .PHONY: test
7 | test:
8 | go test -v -cover -coverprofile cover.out
9 | go tool cover -html cover.out -o cover.html
10 |
11 | .PHONY: lint
12 | lint:
13 | gofmt -s -w .
14 | goimports -w .
15 | golint .
16 | go vet
17 |
18 | .PHONY: example
19 | example:
20 | cd example && sh test.sh
21 | ci-test:
22 | cd costesting && go test -v
23 |
--------------------------------------------------------------------------------
/bucket_accelerate.go:
--------------------------------------------------------------------------------
1 | package cos
2 |
3 | import (
4 | "context"
5 | "encoding/xml"
6 | "net/http"
7 | )
8 |
9 | type BucketPutAccelerateOptions struct {
10 | XMLName xml.Name `xml:"AccelerateConfiguration"`
11 | Status string `xml:"Status,omitempty"`
12 | Type string `xml:"Type,omitempty"`
13 | }
14 | type BucketGetAccelerateResult BucketPutAccelerateOptions
15 |
16 | func (s *BucketService) PutAccelerate(ctx context.Context, opt *BucketPutAccelerateOptions) (*Response, error) {
17 | sendOpt := &sendOptions{
18 | baseURL: s.client.BaseURL.BucketURL,
19 | uri: "/?accelerate",
20 | method: http.MethodPut,
21 | body: opt,
22 | }
23 | resp, err := s.client.doRetry(ctx, sendOpt)
24 | return resp, err
25 | }
26 |
27 | func (s *BucketService) GetAccelerate(ctx context.Context) (*BucketGetAccelerateResult, *Response, error) {
28 | var res BucketGetAccelerateResult
29 | sendOpt := &sendOptions{
30 | baseURL: s.client.BaseURL.BucketURL,
31 | uri: "/?accelerate",
32 | method: http.MethodGet,
33 | result: &res,
34 | }
35 | resp, err := s.client.doRetry(ctx, sendOpt)
36 | return &res, resp, err
37 | }
38 |
--------------------------------------------------------------------------------
/bucket_accelerate_test.go:
--------------------------------------------------------------------------------
1 | package cos
2 |
3 | import (
4 | "context"
5 | "encoding/xml"
6 | "fmt"
7 | "net/http"
8 | "reflect"
9 | "testing"
10 | )
11 |
12 | func TestBucketService_GetAccelerate(t *testing.T) {
13 | setup()
14 | defer teardown()
15 |
16 | mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
17 | testMethod(t, r, "GET")
18 | vs := values{
19 | "accelerate": "",
20 | }
21 | testFormValues(t, r, vs)
22 | fmt.Fprint(w, `
23 | Enabled
24 | COS
25 | `)
26 | })
27 |
28 | res, _, err := client.Bucket.GetAccelerate(context.Background())
29 | if err != nil {
30 | t.Fatalf("Bucket.GetAccelerate returned error %v", err)
31 | }
32 |
33 | want := &BucketGetAccelerateResult{
34 | XMLName: xml.Name{Local: "AccelerateConfiguration"},
35 | Status: "Enabled",
36 | Type: "COS",
37 | }
38 |
39 | if !reflect.DeepEqual(res, want) {
40 | t.Errorf("Bucket.GetAccelerate returned %+v, want %+v", res, want)
41 | }
42 | }
43 |
44 | func TestBucketService_PutAccelerate(t *testing.T) {
45 | setup()
46 | defer teardown()
47 |
48 | opt := &BucketPutAccelerateOptions{
49 | XMLName: xml.Name{Local: "AccelerateConfiguration"},
50 | Status: "Enabled",
51 | Type: "COS",
52 | }
53 |
54 | rt := 0
55 | mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
56 | testMethod(t, r, "PUT")
57 | vs := values{
58 | "accelerate": "",
59 | }
60 | testFormValues(t, r, vs)
61 |
62 | body := new(BucketPutAccelerateOptions)
63 | xml.NewDecoder(r.Body).Decode(body)
64 | want := opt
65 | want.XMLName = xml.Name{Local: "AccelerateConfiguration"}
66 | if !reflect.DeepEqual(body, want) {
67 | t.Errorf("Bucket.PutAccelerate request\n body: %+v\n, want %+v\n", body, want)
68 | }
69 | rt++
70 | if rt < 3 {
71 | w.WriteHeader(http.StatusBadGateway)
72 | }
73 | })
74 |
75 | _, err := client.Bucket.PutAccelerate(context.Background(), opt)
76 | if err != nil {
77 | t.Fatalf("Bucket.PutAccelerate returned error: %v", err)
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/bucket_acl.go:
--------------------------------------------------------------------------------
1 | package cos
2 |
3 | import (
4 | "context"
5 | "net/http"
6 | )
7 |
8 | // BucketGetACLResult is same to the ACLXml
9 | type BucketGetACLResult = ACLXml
10 |
11 | // GetACL 使用API读取Bucket的ACL表,只有所有者有权操作。
12 | //
13 | // https://www.qcloud.com/document/product/436/7733
14 | func (s *BucketService) GetACL(ctx context.Context) (*BucketGetACLResult, *Response, error) {
15 | var res BucketGetACLResult
16 | sendOpt := sendOptions{
17 | baseURL: s.client.BaseURL.BucketURL,
18 | uri: "/?acl",
19 | method: http.MethodGet,
20 | result: &res,
21 | }
22 | resp, err := s.client.doRetry(ctx, &sendOpt)
23 | if err == nil {
24 | decodeACL(resp, &res)
25 | }
26 | return &res, resp, err
27 | }
28 |
29 | // BucketPutACLOptions is the option of PutBucketACL
30 | type BucketPutACLOptions struct {
31 | Header *ACLHeaderOptions `url:"-" xml:"-"`
32 | Body *ACLXml `url:"-" header:"-"`
33 | }
34 |
35 | // PutACL 使用API写入Bucket的ACL表,您可以通过Header:"x-cos-acl","x-cos-grant-read",
36 | // "x-cos-grant-write","x-cos-grant-full-control"传入ACL信息,也可以通过body以XML格式传入ACL信息,
37 | //
38 | // 但是只能选择Header和Body其中一种,否则返回冲突。
39 | //
40 | // Put Bucket ACL是一个覆盖操作,传入新的ACL将覆盖原有ACL。只有所有者有权操作。
41 | //
42 | // "x-cos-acl":枚举值为public-read,private;public-read意味这个Bucket有公有读私有写的权限,
43 | // private意味这个Bucket有私有读写的权限。
44 | //
45 | // "x-cos-grant-read":意味被赋予权限的用户拥有该Bucket的读权限
46 | // "x-cos-grant-write":意味被赋予权限的用户拥有该Bucket的写权限
47 | // "x-cos-grant-full-control":意味被赋予权限的用户拥有该Bucket的读写权限
48 | //
49 | // https://www.qcloud.com/document/product/436/7737
50 | func (s *BucketService) PutACL(ctx context.Context, opt *BucketPutACLOptions) (*Response, error) {
51 | header := opt.Header
52 | body := opt.Body
53 | sendOpt := sendOptions{
54 | baseURL: s.client.BaseURL.BucketURL,
55 | uri: "/?acl",
56 | method: http.MethodPut,
57 | body: body,
58 | optHeader: header,
59 | }
60 | resp, err := s.client.doRetry(ctx, &sendOpt)
61 | return resp, err
62 | }
63 |
--------------------------------------------------------------------------------
/bucket_encryption.go:
--------------------------------------------------------------------------------
1 | package cos
2 |
3 | import (
4 | "context"
5 | "encoding/xml"
6 | "net/http"
7 | )
8 |
9 | type BucketEncryptionConfiguration struct {
10 | SSEAlgorithm string `xml:"SSEAlgorithm"`
11 | KMSMasterKeyID string `xml:"KMSMasterKeyID,omitempty"`
12 | }
13 |
14 | type BucketPutEncryptionOptions struct {
15 | XMLName xml.Name `xml:"ServerSideEncryptionConfiguration"`
16 | Rule *BucketEncryptionConfiguration `xml:"Rule>ApplyServerSideEncryptionByDefault"`
17 | }
18 |
19 | type BucketGetEncryptionResult BucketPutEncryptionOptions
20 |
21 | func (s *BucketService) PutEncryption(ctx context.Context, opt *BucketPutEncryptionOptions) (*Response, error) {
22 | sendOpt := &sendOptions{
23 | baseURL: s.client.BaseURL.BucketURL,
24 | uri: "/?encryption",
25 | method: http.MethodPut,
26 | body: opt,
27 | }
28 | resp, err := s.client.doRetry(ctx, sendOpt)
29 | return resp, err
30 | }
31 |
32 | func (s *BucketService) GetEncryption(ctx context.Context) (*BucketGetEncryptionResult, *Response, error) {
33 | var res BucketGetEncryptionResult
34 | sendOpt := &sendOptions{
35 | baseURL: s.client.BaseURL.BucketURL,
36 | uri: "/?encryption",
37 | method: http.MethodGet,
38 | result: &res,
39 | }
40 | resp, err := s.client.doRetry(ctx, sendOpt)
41 | return &res, resp, err
42 | }
43 |
44 | func (s *BucketService) DeleteEncryption(ctx context.Context) (*Response, error) {
45 | sendOpt := &sendOptions{
46 | baseURL: s.client.BaseURL.BucketURL,
47 | uri: "/?encryption",
48 | method: http.MethodDelete,
49 | }
50 | resp, err := s.client.doRetry(ctx, sendOpt)
51 | return resp, err
52 | }
53 |
--------------------------------------------------------------------------------
/bucket_location.go:
--------------------------------------------------------------------------------
1 | package cos
2 |
3 | import (
4 | "context"
5 | "encoding/xml"
6 | "net/http"
7 | )
8 |
9 | // BucketGetLocationResult is the result of BucketGetLocation
10 | type BucketGetLocationResult struct {
11 | XMLName xml.Name `xml:"LocationConstraint"`
12 | Location string `xml:",chardata"`
13 | }
14 |
15 | // GetLocation 接口获取Bucket所在地域信息,只有Bucket所有者有权限读取信息。
16 | //
17 | // https://www.qcloud.com/document/product/436/8275
18 | func (s *BucketService) GetLocation(ctx context.Context) (*BucketGetLocationResult, *Response, error) {
19 | var res BucketGetLocationResult
20 | sendOpt := sendOptions{
21 | baseURL: s.client.BaseURL.BucketURL,
22 | uri: "/?location",
23 | method: http.MethodGet,
24 | result: &res,
25 | }
26 | resp, err := s.client.doRetry(ctx, &sendOpt)
27 | return &res, resp, err
28 | }
29 |
--------------------------------------------------------------------------------
/bucket_location_test.go:
--------------------------------------------------------------------------------
1 | package cos
2 |
3 | import (
4 | "context"
5 | "encoding/xml"
6 | "fmt"
7 | "net/http"
8 | "reflect"
9 | "testing"
10 | )
11 |
12 | func TestBucketService_GetLocation(t *testing.T) {
13 | setup()
14 | defer teardown()
15 |
16 | mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
17 | testMethod(t, r, "GET")
18 | vs := values{
19 | "location": "",
20 | }
21 | testFormValues(t, r, vs)
22 | fmt.Fprint(w, `
23 | ap-guangzhou`)
24 | })
25 |
26 | ref, _, err := client.Bucket.GetLocation(context.Background())
27 | if err != nil {
28 | t.Fatalf("Bucket.GetLocation returned error: %v", err)
29 | }
30 |
31 | want := &BucketGetLocationResult{
32 | XMLName: xml.Name{Local: "LocationConstraint"},
33 | Location: "ap-guangzhou",
34 | }
35 |
36 | if !reflect.DeepEqual(ref, want) {
37 | t.Errorf("Bucket.GetLocation returned %+v, want %+v", ref, want)
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/bucket_logging.go:
--------------------------------------------------------------------------------
1 | package cos
2 |
3 | import (
4 | "context"
5 | "encoding/xml"
6 | "net/http"
7 | )
8 |
9 | // Notice bucket logging function is testing, can not use.
10 |
11 | // BucketLoggingEnabled main struct of logging
12 | type BucketLoggingEnabled struct {
13 | TargetBucket string `xml:"TargetBucket"`
14 | TargetPrefix string `xml:"TargetPrefix"`
15 | }
16 |
17 | // BucketPutLoggingOptions is the options of PutBucketLogging
18 | type BucketPutLoggingOptions struct {
19 | XMLName xml.Name `xml:"BucketLoggingStatus"`
20 | LoggingEnabled *BucketLoggingEnabled `xml:"LoggingEnabled,omitempty"`
21 | }
22 |
23 | // BucketGetLoggingResult is the result of GetBucketLogging
24 | type BucketGetLoggingResult BucketPutLoggingOptions
25 |
26 | // PutBucketLogging https://cloud.tencent.com/document/product/436/17054
27 | func (s *BucketService) PutLogging(ctx context.Context, opt *BucketPutLoggingOptions) (*Response, error) {
28 | sendOpt := sendOptions{
29 | baseURL: s.client.BaseURL.BucketURL,
30 | uri: "/?logging",
31 | method: http.MethodPut,
32 | body: opt,
33 | }
34 | resp, err := s.client.doRetry(ctx, &sendOpt)
35 | return resp, err
36 | }
37 |
38 | // GetBucketLogging https://cloud.tencent.com/document/product/436/17053
39 | func (s *BucketService) GetLogging(ctx context.Context) (*BucketGetLoggingResult, *Response, error) {
40 | var res BucketGetLoggingResult
41 | sendOpt := sendOptions{
42 | baseURL: s.client.BaseURL.BucketURL,
43 | uri: "/?logging",
44 | method: http.MethodGet,
45 | result: &res,
46 | }
47 | resp, err := s.client.doRetry(ctx, &sendOpt)
48 | return &res, resp, err
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/bucket_logging_test.go:
--------------------------------------------------------------------------------
1 | package cos
2 |
3 | import (
4 | "context"
5 | "encoding/xml"
6 | "fmt"
7 | "net/http"
8 | "reflect"
9 | "testing"
10 | )
11 |
12 | func TestBucketService_PutLogging(t *testing.T) {
13 | setup()
14 | defer teardown()
15 | opt := &BucketPutLoggingOptions{
16 | LoggingEnabled: &BucketLoggingEnabled{
17 | TargetBucket: "logs",
18 | },
19 | }
20 | mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
21 | testMethod(t, r, http.MethodPut)
22 | vs := values{
23 | "logging": "",
24 | }
25 | testFormValues(t, r, vs)
26 |
27 | body := &BucketPutLoggingOptions{}
28 | xml.NewDecoder(r.Body).Decode(body)
29 | want := opt
30 | want.XMLName = xml.Name{Local: "BucketLoggingStatus"}
31 | if !reflect.DeepEqual(want, body) {
32 | t.Fatalf("Bucket.PutLogging request\n body: %+v\n, want %+v\n", body, want)
33 | }
34 | })
35 |
36 | _, err := client.Bucket.PutLogging(context.Background(), opt)
37 | if err != nil {
38 | t.Fatalf("Bucket.PutLogging failed, error: %v", err)
39 | }
40 | }
41 |
42 | func TestBucketService_GetLogging(t *testing.T) {
43 | setup()
44 | defer teardown()
45 |
46 | mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
47 | testMethod(t, r, http.MethodGet)
48 | vs := values{
49 | "logging": "",
50 | }
51 | testFormValues(t, r, vs)
52 |
53 | fmt.Fprint(w, `
54 |
55 | logs
56 | mylogs
57 |
58 | `)
59 | })
60 | res, _, err := client.Bucket.GetLogging(context.Background())
61 | if err != nil {
62 | t.Fatalf("Bucket.GetLogging failed, error: %v", err)
63 | }
64 | want := &BucketGetLoggingResult{
65 | XMLName: xml.Name{Local: "BucketLoggingStatus"},
66 | LoggingEnabled: &BucketLoggingEnabled{
67 | TargetBucket: "logs",
68 | TargetPrefix: "mylogs",
69 | },
70 | }
71 |
72 | if !reflect.DeepEqual(res, want) {
73 | t.Errorf("Bucket.GetLogging returned\n%+v, want\n%+v", res, want)
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/bucket_part.go:
--------------------------------------------------------------------------------
1 | package cos
2 |
3 | import (
4 | "context"
5 | "encoding/xml"
6 | "net/http"
7 | )
8 |
9 | // ListMultipartUploadsResult is the result of ListMultipartUploads
10 | type ListMultipartUploadsResult struct {
11 | XMLName xml.Name `xml:"ListMultipartUploadsResult"`
12 | Bucket string `xml:"Bucket"`
13 | EncodingType string `xml:"Encoding-Type"`
14 | KeyMarker string
15 | UploadIDMarker string `xml:"UploadIdMarker"`
16 | NextKeyMarker string
17 | NextUploadIDMarker string `xml:"NextUploadIdMarker"`
18 | MaxUploads int
19 | IsTruncated bool
20 | Uploads []struct {
21 | Key string
22 | UploadID string `xml:"UploadId"`
23 | StorageClass string
24 | Initiator *Initiator
25 | Owner *Owner
26 | Initiated string
27 | } `xml:"Upload,omitempty"`
28 | Prefix string
29 | Delimiter string `xml:"delimiter,omitempty"`
30 | CommonPrefixes []string `xml:"CommonPrefixs>Prefix,omitempty"`
31 | }
32 |
33 | // ListMultipartUploadsOptions is the option of ListMultipartUploads
34 | type ListMultipartUploadsOptions struct {
35 | Delimiter string `url:"delimiter,omitempty"`
36 | EncodingType string `url:"encoding-type,omitempty"`
37 | Prefix string `url:"prefix,omitempty"`
38 | MaxUploads int `url:"max-uploads,omitempty"`
39 | KeyMarker string `url:"key-marker,omitempty"`
40 | UploadIDMarker string `url:"upload-id-marker,omitempty"`
41 | XOptionHeader *http.Header `header:"-,omitempty" url:"-" xml:"-"`
42 | }
43 |
44 | // ListMultipartUploads 用来查询正在进行中的分块上传。单次最多列出1000个正在进行中的分块上传。
45 | //
46 | // https://www.qcloud.com/document/product/436/7736
47 | func (s *BucketService) ListMultipartUploads(ctx context.Context, opt *ListMultipartUploadsOptions) (*ListMultipartUploadsResult, *Response, error) {
48 | var res ListMultipartUploadsResult
49 | sendOpt := sendOptions{
50 | baseURL: s.client.BaseURL.BucketURL,
51 | uri: "/?uploads",
52 | method: http.MethodGet,
53 | result: &res,
54 | optQuery: opt,
55 | optHeader: opt,
56 | }
57 | resp, err := s.client.doRetry(ctx, &sendOpt)
58 | return &res, resp, err
59 | }
60 |
--------------------------------------------------------------------------------
/bucket_referer.go:
--------------------------------------------------------------------------------
1 | package cos
2 |
3 | import (
4 | "context"
5 | "encoding/base64"
6 | "encoding/xml"
7 | "net/http"
8 | )
9 |
10 | type BucketPutRefererOptions struct {
11 | XMLName xml.Name `xml:"RefererConfiguration"`
12 | Status string `xml:"Status"`
13 | RefererType string `xml:"RefererType"`
14 | DomainList []string `xml:"DomainList>Domain"`
15 | EmptyReferConfiguration string `xml:"EmptyReferConfiguration,omitempty"`
16 | VerifySignatureURL string `xml:"VerifySignatureURL,omitempty"`
17 | }
18 |
19 | type BucketGetRefererResult BucketPutRefererOptions
20 |
21 | func (s *BucketService) PutReferer(ctx context.Context, opt *BucketPutRefererOptions) (*Response, error) {
22 | sendOpt := &sendOptions{
23 | baseURL: s.client.BaseURL.BucketURL,
24 | uri: "/?referer",
25 | method: http.MethodPut,
26 | body: opt,
27 | }
28 | resp, err := s.client.doRetry(ctx, sendOpt)
29 | return resp, err
30 | }
31 |
32 | func (s *BucketService) GetReferer(ctx context.Context) (*BucketGetRefererResult, *Response, error) {
33 | var res BucketGetRefererResult
34 | sendOpt := &sendOptions{
35 | baseURL: s.client.BaseURL.BucketURL,
36 | uri: "/?referer",
37 | method: http.MethodGet,
38 | result: &res,
39 | }
40 | resp, err := s.client.doRetry(ctx, sendOpt)
41 | return &res, resp, err
42 | }
43 |
44 | // Put空
45 | func (s *BucketService) DeleteReferer(ctx context.Context) (*Response, error) {
46 | sendOpt := &sendOptions{
47 | baseURL: s.client.BaseURL.BucketURL,
48 | uri: "/?referer",
49 | method: http.MethodPut,
50 | optHeader: &struct {
51 | Md5 string `header:"Content-Md5"`
52 | }{
53 | Md5: base64.StdEncoding.EncodeToString(calMD5Digest([]byte(""))),
54 | },
55 | }
56 | resp, err := s.client.doRetry(ctx, sendOpt)
57 | return resp, err
58 | }
59 |
--------------------------------------------------------------------------------
/bucket_tagging.go:
--------------------------------------------------------------------------------
1 | package cos
2 |
3 | import (
4 | "context"
5 | "encoding/xml"
6 | "net/http"
7 | )
8 |
9 | // BucketTaggingTag is the tag of BucketTagging
10 | type BucketTaggingTag struct {
11 | Key string
12 | Value string
13 | }
14 |
15 | // BucketGetTaggingResult is the result of BucketGetTagging
16 | type BucketGetTaggingResult struct {
17 | XMLName xml.Name `xml:"Tagging"`
18 | TagSet []BucketTaggingTag `xml:"TagSet>Tag,omitempty"`
19 | }
20 |
21 | // GetTagging 接口实现获取指定Bucket的标签。
22 | //
23 | // https://www.qcloud.com/document/product/436/8277
24 | func (s *BucketService) GetTagging(ctx context.Context) (*BucketGetTaggingResult, *Response, error) {
25 | var res BucketGetTaggingResult
26 | sendOpt := sendOptions{
27 | baseURL: s.client.BaseURL.BucketURL,
28 | uri: "/?tagging",
29 | method: http.MethodGet,
30 | result: &res,
31 | }
32 | resp, err := s.client.doRetry(ctx, &sendOpt)
33 | return &res, resp, err
34 | }
35 |
36 | // BucketPutTaggingOptions is the option of BucketPutTagging
37 | type BucketPutTaggingOptions struct {
38 | XMLName xml.Name `xml:"Tagging"`
39 | TagSet []BucketTaggingTag `xml:"TagSet>Tag,omitempty"`
40 | }
41 |
42 | // PutTagging 接口实现给用指定Bucket打标签。用来组织和管理相关Bucket。
43 | //
44 | // 当该请求设置相同Key名称,不同Value时,会返回400。请求成功,则返回204。
45 | //
46 | // https://www.qcloud.com/document/product/436/8281
47 | func (s *BucketService) PutTagging(ctx context.Context, opt *BucketPutTaggingOptions) (*Response, error) {
48 | sendOpt := sendOptions{
49 | baseURL: s.client.BaseURL.BucketURL,
50 | uri: "/?tagging",
51 | method: http.MethodPut,
52 | body: opt,
53 | }
54 | resp, err := s.client.doRetry(ctx, &sendOpt)
55 | return resp, err
56 | }
57 |
58 | // DeleteTagging 接口实现删除指定Bucket的标签。
59 | //
60 | // https://www.qcloud.com/document/product/436/8286
61 | func (s *BucketService) DeleteTagging(ctx context.Context) (*Response, error) {
62 | sendOpt := sendOptions{
63 | baseURL: s.client.BaseURL.BucketURL,
64 | uri: "/?tagging",
65 | method: http.MethodDelete,
66 | }
67 | resp, err := s.client.doRetry(ctx, &sendOpt)
68 | return resp, err
69 | }
70 |
--------------------------------------------------------------------------------
/bucket_version.go:
--------------------------------------------------------------------------------
1 | package cos
2 |
3 | import (
4 | "context"
5 | "encoding/xml"
6 | "net/http"
7 | )
8 |
9 | // BucketPutVersionOptions is the options of PutBucketVersioning
10 | type BucketPutVersionOptions struct {
11 | XMLName xml.Name `xml:"VersioningConfiguration"`
12 | Status string `xml:"Status"`
13 | }
14 |
15 | // BucketGetVersionResult is the result of GetBucketVersioning
16 | type BucketGetVersionResult BucketPutVersionOptions
17 |
18 | // PutVersion https://cloud.tencent.com/document/product/436/19889
19 | // Status has Suspended\Enabled
20 | func (s *BucketService) PutVersioning(ctx context.Context, opt *BucketPutVersionOptions) (*Response, error) {
21 | sendOpt := sendOptions{
22 | baseURL: s.client.BaseURL.BucketURL,
23 | uri: "/?versioning",
24 | method: http.MethodPut,
25 | body: opt,
26 | }
27 | resp, err := s.client.doRetry(ctx, &sendOpt)
28 | return resp, err
29 | }
30 |
31 | // GetVersion https://cloud.tencent.com/document/product/436/19888
32 | func (s *BucketService) GetVersioning(ctx context.Context) (*BucketGetVersionResult, *Response, error) {
33 | var res BucketGetVersionResult
34 | sendOpt := sendOptions{
35 | baseURL: s.client.BaseURL.BucketURL,
36 | uri: "/?versioning",
37 | method: http.MethodGet,
38 | result: &res,
39 | }
40 | resp, err := s.client.doRetry(ctx, &sendOpt)
41 | return &res, resp, err
42 | }
43 |
--------------------------------------------------------------------------------
/bucket_version_test.go:
--------------------------------------------------------------------------------
1 | package cos
2 |
3 | import (
4 | "context"
5 | "encoding/xml"
6 | "fmt"
7 | "net/http"
8 | "reflect"
9 | "testing"
10 | )
11 |
12 | func TestBucketService_PutVersioning(t *testing.T) {
13 | setup()
14 | defer teardown()
15 | opt := &BucketPutVersionOptions{
16 | Status: "Suspended",
17 | }
18 | mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
19 | testMethod(t, r, http.MethodPut)
20 | vs := values{
21 | "versioning": "",
22 | }
23 | testFormValues(t, r, vs)
24 |
25 | body := &BucketPutVersionOptions{}
26 | xml.NewDecoder(r.Body).Decode(body)
27 | want := opt
28 | want.XMLName = xml.Name{Local: "VersioningConfiguration"}
29 | if !reflect.DeepEqual(want, body) {
30 | t.Fatalf("Bucket.PutVersioning request\nbody: %+v\nwant %+v\n", body, want)
31 | }
32 | })
33 |
34 | _, err := client.Bucket.PutVersioning(context.Background(), opt)
35 | if err != nil {
36 | t.Fatalf("Bucket.PutVersioning failed, error: %v", err)
37 | }
38 | }
39 |
40 | func TestBucketService_GetVersioning(t *testing.T) {
41 | setup()
42 | defer teardown()
43 |
44 | mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
45 | testMethod(t, r, http.MethodGet)
46 | vs := values{
47 | "versioning": "",
48 | }
49 | testFormValues(t, r, vs)
50 |
51 | fmt.Fprint(w, `
52 | Suspended
53 | `)
54 | })
55 | res, _, err := client.Bucket.GetVersioning(context.Background())
56 | if err != nil {
57 | t.Fatalf("Bucket.GetVersioning failed, error: %v", err)
58 | }
59 | want := &BucketGetVersionResult{
60 | XMLName: xml.Name{Local: "VersioningConfiguration"},
61 | Status: "Suspended",
62 | }
63 |
64 | if !reflect.DeepEqual(res, want) {
65 | t.Errorf("Bucket.GetVersioning returned\n%+v, want\n%+v", res, want)
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/crypto/aes_ctr.go:
--------------------------------------------------------------------------------
1 | package coscrypto
2 |
3 | import (
4 | "crypto/aes"
5 | "crypto/cipher"
6 | "io"
7 | )
8 |
9 | type aesCtr struct {
10 | encrypter cipher.Stream
11 | decrypter cipher.Stream
12 | }
13 |
14 | func newAesCtr(cd CipherData) (Cipher, error) {
15 | block, err := aes.NewCipher(cd.Key)
16 | if err != nil {
17 | return nil, err
18 | }
19 | encrypter := cipher.NewCTR(block, cd.IV)
20 | decrypter := cipher.NewCTR(block, cd.IV)
21 | return &aesCtr{encrypter, decrypter}, nil
22 | }
23 |
24 | func (c *aesCtr) Encrypt(src io.Reader) io.Reader {
25 | reader := &ctrEncryptReader{
26 | encrypter: c.encrypter,
27 | src: src,
28 | }
29 | return reader
30 | }
31 |
32 | type ctrEncryptReader struct {
33 | encrypter cipher.Stream
34 | src io.Reader
35 | }
36 |
37 | func (reader *ctrEncryptReader) Read(data []byte) (int, error) {
38 | plainText := make([]byte, len(data), len(data))
39 | n, err := reader.src.Read(plainText)
40 | if n > 0 {
41 | plainText = plainText[0:n]
42 | reader.encrypter.XORKeyStream(data, plainText)
43 | }
44 | return n, err
45 | }
46 |
47 | func (c *aesCtr) Decrypt(src io.Reader) io.Reader {
48 | return &ctrDecryptReader{
49 | decrypter: c.decrypter,
50 | src: src,
51 | }
52 | }
53 |
54 | type ctrDecryptReader struct {
55 | decrypter cipher.Stream
56 | src io.Reader
57 | }
58 |
59 | func (reader *ctrDecryptReader) Read(data []byte) (int, error) {
60 | cryptoText := make([]byte, len(data), len(data))
61 | n, err := reader.src.Read(cryptoText)
62 | if n > 0 {
63 | cryptoText = cryptoText[0:n]
64 | reader.decrypter.XORKeyStream(data, cryptoText)
65 | }
66 | return n, err
67 | }
68 |
--------------------------------------------------------------------------------
/crypto/cipher.go:
--------------------------------------------------------------------------------
1 | package coscrypto
2 |
3 | import (
4 | "io"
5 | )
6 |
7 | // Cipher is interface for encryption or decryption of an object
8 | type Cipher interface {
9 | Encrypter
10 | Decrypter
11 | }
12 |
13 | // Encrypter is interface with only encrypt method
14 | type Encrypter interface {
15 | Encrypt(io.Reader) io.Reader
16 | }
17 |
18 | // Decrypter is interface with only decrypt method
19 | type Decrypter interface {
20 | Decrypt(io.Reader) io.Reader
21 | }
22 |
23 | // CryptoEncrypter provides close method for Encrypter
24 | type CryptoEncrypter struct {
25 | Body io.Reader
26 | Encrypter io.Reader
27 | isClosed bool
28 | }
29 |
30 | // Close lets the CryptoEncrypter satisfy io.ReadCloser interface
31 | func (rc *CryptoEncrypter) Close() error {
32 | rc.isClosed = true
33 | if closer, ok := rc.Body.(io.ReadCloser); ok {
34 | return closer.Close()
35 | }
36 | return nil
37 | }
38 |
39 | // Read lets the CryptoEncrypter satisfy io.ReadCloser interface
40 | func (rc *CryptoEncrypter) Read(b []byte) (int, error) {
41 | if rc.isClosed {
42 | return 0, io.EOF
43 | }
44 | return rc.Encrypter.Read(b)
45 | }
46 |
47 | // CryptoDecrypter provides close method for Decrypter
48 | type CryptoDecrypter struct {
49 | Body io.Reader
50 | Decrypter io.Reader
51 | isClosed bool
52 | }
53 |
54 | // Close lets the CryptoDecrypter satisfy io.ReadCloser interface
55 | func (rc *CryptoDecrypter) Close() error {
56 | rc.isClosed = true
57 | if closer, ok := rc.Body.(io.ReadCloser); ok {
58 | return closer.Close()
59 | }
60 | return nil
61 | }
62 |
63 | // Read lets the CryptoDecrypter satisfy io.ReadCloser interface
64 | func (rc *CryptoDecrypter) Read(b []byte) (int, error) {
65 | if rc.isClosed {
66 | return 0, io.EOF
67 | }
68 | return rc.Decrypter.Read(b)
69 | }
70 |
--------------------------------------------------------------------------------
/debug/http.go:
--------------------------------------------------------------------------------
1 | package debug
2 |
3 | import (
4 | "fmt"
5 | "github.com/tencentyun/cos-go-sdk-v5"
6 | "io"
7 | "net/http"
8 | "net/http/httputil"
9 | "os"
10 | "regexp"
11 | )
12 |
13 | // DebugRequestTransport 会打印请求和响应信息, 方便调试.
14 | type DebugRequestTransport struct {
15 | RequestHeader bool
16 | RequestBody bool // RequestHeader 为 true 时,这个选项才会生效
17 | ResponseHeader bool
18 | ResponseBody bool // ResponseHeader 为 true 时,这个选项才会生效
19 |
20 | // debug 信息输出到 Writer 中, 默认是 os.Stderr
21 | Writer io.Writer
22 |
23 | Transport http.RoundTripper
24 | }
25 |
26 | var internalHost = regexp.MustCompile(`^.*cos-internal\.[a-z-1]+\.tencentcos\.cn$`)
27 |
28 | // RoundTrip implements the RoundTripper interface.
29 | func (t *DebugRequestTransport) RoundTrip(req *http.Request) (*http.Response, error) {
30 | req = cloneRequest(req) // per RoundTrip contract
31 | w := t.Writer
32 | if w == nil {
33 | w = os.Stderr
34 | }
35 |
36 | if t.RequestHeader {
37 | a, _ := httputil.DumpRequest(req, t.RequestBody)
38 | fmt.Fprintf(w, "%s\n\n", string(a))
39 | }
40 |
41 | resp, err := t.transport(req).RoundTrip(req)
42 | if err != nil {
43 | return resp, err
44 | }
45 |
46 | if t.ResponseHeader {
47 |
48 | b, _ := httputil.DumpResponse(resp, t.ResponseBody)
49 | fmt.Fprintf(w, "%s\n", string(b))
50 | }
51 |
52 | return resp, err
53 | }
54 |
55 | func (t *DebugRequestTransport) transport(req *http.Request) http.RoundTripper {
56 | if t.Transport != nil {
57 | return t.Transport
58 | }
59 | // 内部域名默认使用DNS打散
60 | if rc := internalHost.MatchString(req.URL.Hostname()); rc {
61 | return cos.DNSScatterTransport
62 | }
63 | return http.DefaultTransport
64 | }
65 |
66 | // cloneRequest returns a clone of the provided *http.Request. The clone is a
67 | // shallow copy of the struct and its Header map.
68 | func cloneRequest(r *http.Request) *http.Request {
69 | // shallow copy of the struct
70 | r2 := new(http.Request)
71 | *r2 = *r
72 | // deep copy of the Header
73 | r2.Header = make(http.Header, len(r.Header))
74 | for k, s := range r.Header {
75 | r2.Header[k] = append([]string(nil), s...)
76 | }
77 | return r2
78 | }
79 |
--------------------------------------------------------------------------------
/doc.go:
--------------------------------------------------------------------------------
1 | // Package cos is COS(Cloud Object Storage) Go SDK. The V5 version(XML API).
2 | // There are examples of using each API in the project's 'example' directory.
3 | package cos
4 |
--------------------------------------------------------------------------------
/example/CI/ai_recognition/ai_game_rec.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func log_status(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("WARN: Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("ERROR: Code: %v\n", e.Code)
23 | fmt.Printf("ERROR: Message: %v\n", e.Message)
24 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Printf("ERROR: %v\n", err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com")
35 | b := &cos.BaseURL{BucketURL: u}
36 | c := cos.NewClient(b, &http.Client{
37 | Transport: &cos.AuthorizationTransport{
38 | SecretID: os.Getenv("COS_SECRETID"),
39 | SecretKey: os.Getenv("COS_SECRETKEY"),
40 | Transport: &debug.DebugRequestTransport{
41 | RequestHeader: true,
42 | // Notice when put a large file and set need the request body, might happend out of memory error.
43 | RequestBody: false,
44 | ResponseHeader: true,
45 | ResponseBody: true,
46 | },
47 | },
48 | })
49 | obj := "pic/game2.jpeg"
50 | opt := &cos.AIGameRecOptions{}
51 | res, _, err := c.CI.AIGameRec(context.Background(), obj, opt)
52 | log_status(err)
53 | fmt.Printf("%+v\n", res)
54 | }
55 |
--------------------------------------------------------------------------------
/example/CI/ai_recognition/ai_license_rec.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func log_status(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("WARN: Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("ERROR: Code: %v\n", e.Code)
23 | fmt.Printf("ERROR: Message: %v\n", e.Message)
24 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Printf("ERROR: %v\n", err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com")
35 | b := &cos.BaseURL{BucketURL: u}
36 | c := cos.NewClient(b, &http.Client{
37 | Transport: &cos.AuthorizationTransport{
38 | SecretID: os.Getenv("COS_SECRETID"),
39 | SecretKey: os.Getenv("COS_SECRETKEY"),
40 | Transport: &debug.DebugRequestTransport{
41 | RequestHeader: true,
42 | // Notice when put a large file and set need the request body, might happend out of memory error.
43 | RequestBody: false,
44 | ResponseHeader: true,
45 | ResponseBody: true,
46 | },
47 | },
48 | })
49 | obj := "pic/carCard.jpeg"
50 | opt := &cos.AILicenseRecOptions{
51 | CardType: "DriverLicense",
52 | }
53 | res, _, err := c.CI.AILicenseRec(context.Background(), obj, opt)
54 | log_status(err)
55 | fmt.Printf("%+v\n", res)
56 | }
57 |
--------------------------------------------------------------------------------
/example/CI/ai_recognition/ai_object_detect.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func log_status(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("WARN: Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("ERROR: Code: %v\n", e.Code)
23 | fmt.Printf("ERROR: Message: %v\n", e.Message)
24 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Printf("ERROR: %v\n", err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com")
35 | b := &cos.BaseURL{BucketURL: u}
36 | c := cos.NewClient(b, &http.Client{
37 | Transport: &cos.AuthorizationTransport{
38 | SecretID: os.Getenv("COS_SECRETID"),
39 | SecretKey: os.Getenv("COS_SECRETKEY"),
40 | Transport: &debug.DebugRequestTransport{
41 | RequestHeader: true,
42 | // Notice when put a large file and set need the request body, might happend out of memory error.
43 | RequestBody: false,
44 | ResponseHeader: true,
45 | ResponseBody: true,
46 | },
47 | },
48 | })
49 | obj := "pic/carCard.jpeg"
50 | opt := &cos.AIObjectDetectOptions{}
51 | res, _, err := c.CI.AIObjectDetect(context.Background(), obj, opt)
52 | log_status(err)
53 | fmt.Printf("%+v\n", res)
54 | }
55 |
--------------------------------------------------------------------------------
/example/CI/ai_recognition/ai_recognize.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func log_status(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("WARN: Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("ERROR: Code: %v\n", e.Code)
23 | fmt.Printf("ERROR: Message: %v\n", e.Message)
24 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Printf("ERROR: %v\n", err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com")
35 | b := &cos.BaseURL{BucketURL: u}
36 | c := cos.NewClient(b, &http.Client{
37 | Transport: &cos.AuthorizationTransport{
38 | SecretID: os.Getenv("COS_SECRETID"),
39 | SecretKey: os.Getenv("COS_SECRETKEY"),
40 | Transport: &debug.DebugRequestTransport{
41 | RequestHeader: true,
42 | // Notice when put a large file and set need the request body, might happend out of memory error.
43 | RequestBody: false,
44 | ResponseHeader: true,
45 | ResponseBody: true,
46 | },
47 | },
48 | })
49 | obj := "pic/logo.jpeg"
50 | opt := &cos.AIRecognitionOptions{
51 | DetectType: "DetectVehicle,DetectPet,DetectPedestrian",
52 | }
53 | res, _, err := c.CI.AIRecognition(context.Background(), obj, opt)
54 | log_status(err)
55 | fmt.Printf("%+v\n", res)
56 | }
57 |
--------------------------------------------------------------------------------
/example/CI/ai_recognition/assess_quality.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func log_status(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("WARN: Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("ERROR: Code: %v\n", e.Code)
23 | fmt.Printf("ERROR: Message: %v\n", e.Message)
24 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Printf("ERROR: %v\n", err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com")
35 | b := &cos.BaseURL{BucketURL: u}
36 | c := cos.NewClient(b, &http.Client{
37 | Transport: &cos.AuthorizationTransport{
38 | SecretID: os.Getenv("COS_SECRETID"),
39 | SecretKey: os.Getenv("COS_SECRETKEY"),
40 | Transport: &debug.DebugRequestTransport{
41 | RequestHeader: true,
42 | // Notice when put a large file and set need the request body, might happend out of memory error.
43 | RequestBody: false,
44 | ResponseHeader: true,
45 | ResponseBody: true,
46 | },
47 | },
48 | })
49 | obj := "pic/logo.jpeg"
50 | res, _, err := c.CI.GetAssessQuality(context.Background(), obj)
51 | log_status(err)
52 | fmt.Printf("%+v\n", res)
53 | }
54 |
--------------------------------------------------------------------------------
/example/CI/ai_recognition/auto_translation_block.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func log_status(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("WARN: Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("ERROR: Code: %v\n", e.Code)
23 | fmt.Printf("ERROR: Message: %v\n", e.Message)
24 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Printf("ERROR: %v\n", err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com")
35 | b := &cos.BaseURL{BucketURL: u}
36 | c := cos.NewClient(b, &http.Client{
37 | Transport: &cos.AuthorizationTransport{
38 | SecretID: os.Getenv("COS_SECRETID"),
39 | SecretKey: os.Getenv("COS_SECRETKEY"),
40 | Transport: &debug.DebugRequestTransport{
41 | RequestHeader: true,
42 | // Notice when put a large file and set need the request body, might happend out of memory error.
43 | RequestBody: false,
44 | ResponseHeader: true,
45 | ResponseBody: true,
46 | },
47 | },
48 | })
49 | opt := &cos.AutoTranslationBlockOptions{
50 | InputText: "你好,很高兴认识你!",
51 | SourceLang: "zh",
52 | TargetLang: "en",
53 | }
54 | res, _, err := c.CI.GetAutoTranslationBlock(context.Background(), opt)
55 | log_status(err)
56 | fmt.Printf("%+v\n", res.TranslationResult)
57 | }
58 |
--------------------------------------------------------------------------------
/example/CI/ai_recognition/detect_car.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func log_status(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("WARN: Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("ERROR: Code: %v\n", e.Code)
23 | fmt.Printf("ERROR: Message: %v\n", e.Message)
24 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Printf("ERROR: %v\n", err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com")
35 | b := &cos.BaseURL{BucketURL: u}
36 | c := cos.NewClient(b, &http.Client{
37 | Transport: &cos.AuthorizationTransport{
38 | SecretID: os.Getenv("COS_SECRETID"),
39 | SecretKey: os.Getenv("COS_SECRETKEY"),
40 | Transport: &debug.DebugRequestTransport{
41 | RequestHeader: true,
42 | // Notice when put a large file and set need the request body, might happend out of memory error.
43 | RequestBody: false,
44 | ResponseHeader: true,
45 | ResponseBody: true,
46 | },
47 | },
48 | })
49 | obj := "pic/car.png"
50 | res, _, err := c.CI.DetectCar(context.Background(), obj)
51 | log_status(err)
52 | fmt.Printf("%+v\n", res)
53 | }
54 |
--------------------------------------------------------------------------------
/example/CI/ai_recognition/detect_face.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func log_status(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("WARN: Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("ERROR: Code: %v\n", e.Code)
23 | fmt.Printf("ERROR: Message: %v\n", e.Message)
24 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Printf("ERROR: %v\n", err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com")
35 | b := &cos.BaseURL{BucketURL: u}
36 | c := cos.NewClient(b, &http.Client{
37 | Transport: &cos.AuthorizationTransport{
38 | SecretID: os.Getenv("COS_SECRETID"),
39 | SecretKey: os.Getenv("COS_SECRETKEY"),
40 | Transport: &debug.DebugRequestTransport{
41 | RequestHeader: true,
42 | // Notice when put a large file and set need the request body, might happend out of memory error.
43 | RequestBody: false,
44 | ResponseHeader: true,
45 | ResponseBody: true,
46 | },
47 | },
48 | })
49 | obj := "pic/face.jpeg"
50 | opt := &cos.DetectFaceOptions{
51 | MaxFaceNum: 2,
52 | }
53 | res, _, err := c.CI.DetectFace(context.Background(), obj, opt)
54 | log_status(err)
55 | fmt.Printf("%+v\n", res)
56 | }
57 |
--------------------------------------------------------------------------------
/example/CI/ai_recognition/detect_pet.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func log_status(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("WARN: Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("ERROR: Code: %v\n", e.Code)
23 | fmt.Printf("ERROR: Message: %v\n", e.Message)
24 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Printf("ERROR: %v\n", err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com")
35 | b := &cos.BaseURL{BucketURL: u}
36 | c := cos.NewClient(b, &http.Client{
37 | Transport: &cos.AuthorizationTransport{
38 | SecretID: os.Getenv("COS_SECRETID"),
39 | SecretKey: os.Getenv("COS_SECRETKEY"),
40 | Transport: &debug.DebugRequestTransport{
41 | RequestHeader: true,
42 | // Notice when put a large file and set need the request body, might happend out of memory error.
43 | RequestBody: false,
44 | ResponseHeader: true,
45 | ResponseBody: true,
46 | },
47 | },
48 | })
49 | obj := "pic/pet.jpeg"
50 | opt := &cos.PetDetectOption{}
51 | res, _, err := c.CI.DetectPet(context.Background(), obj, opt)
52 | log_status(err)
53 | fmt.Printf("%+v\n", res)
54 | }
55 |
--------------------------------------------------------------------------------
/example/CI/ai_recognition/image_inspect.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func log_status(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("WARN: Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("ERROR: Code: %v\n", e.Code)
23 | fmt.Printf("ERROR: Message: %v\n", e.Message)
24 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Printf("ERROR: %v\n", err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com")
35 | b := &cos.BaseURL{BucketURL: u}
36 | c := cos.NewClient(b, &http.Client{
37 | Transport: &cos.AuthorizationTransport{
38 | SecretID: os.Getenv("COS_SECRETID"),
39 | SecretKey: os.Getenv("COS_SECRETKEY"),
40 | Transport: &debug.DebugRequestTransport{
41 | RequestHeader: true,
42 | // Notice when put a large file and set need the request body, might happend out of memory error.
43 | RequestBody: true,
44 | ResponseHeader: true,
45 | ResponseBody: true,
46 | },
47 | },
48 | })
49 |
50 | opt := &cos.CosImageInspectOptions{}
51 | name := "pic/pet.jpg"
52 | res, _, err := c.CI.CosImageInspect(context.Background(), name, opt)
53 | log_status(err)
54 | fmt.Printf("%+v\n", res)
55 | }
56 |
--------------------------------------------------------------------------------
/example/CI/ai_recognition/image_quality.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func log_status(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("WARN: Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("ERROR: Code: %v\n", e.Code)
23 | fmt.Printf("ERROR: Message: %v\n", e.Message)
24 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Printf("ERROR: %v\n", err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com")
35 | b := &cos.BaseURL{BucketURL: u}
36 | c := cos.NewClient(b, &http.Client{
37 | Transport: &cos.AuthorizationTransport{
38 | SecretID: os.Getenv("COS_SECRETID"),
39 | SecretKey: os.Getenv("COS_SECRETKEY"),
40 | Transport: &debug.DebugRequestTransport{
41 | RequestHeader: true,
42 | // Notice when put a large file and set need the request body, might happend out of memory error.
43 | RequestBody: false,
44 | ResponseHeader: true,
45 | ResponseBody: false,
46 | },
47 | },
48 | })
49 | obj := "pic/deer.jpg"
50 | res, _, err := c.CI.ImageQuality(context.Background(), obj)
51 | log_status(err)
52 | fmt.Printf("%+v\n", res)
53 | }
54 |
--------------------------------------------------------------------------------
/example/CI/ai_recognition/image_repair.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "io"
7 | "net/http"
8 | "net/url"
9 | "os"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func log_status(err error) {
16 | if err == nil {
17 | return
18 | }
19 | if cos.IsNotFoundError(err) {
20 | // WARN
21 | fmt.Println("WARN: Resource is not existed")
22 | } else if e, ok := cos.IsCOSError(err); ok {
23 | fmt.Printf("ERROR: Code: %v\n", e.Code)
24 | fmt.Printf("ERROR: Message: %v\n", e.Message)
25 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
26 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
27 | // ERROR
28 | } else {
29 | fmt.Printf("ERROR: %v\n", err)
30 | // ERROR
31 | }
32 | }
33 |
34 | func main() {
35 | u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com")
36 | b := &cos.BaseURL{BucketURL: u}
37 | c := cos.NewClient(b, &http.Client{
38 | Transport: &cos.AuthorizationTransport{
39 | SecretID: os.Getenv("COS_SECRETID"),
40 | SecretKey: os.Getenv("COS_SECRETKEY"),
41 | Transport: &debug.DebugRequestTransport{
42 | RequestHeader: true,
43 | // Notice when put a large file and set need the request body, might happend out of memory error.
44 | RequestBody: false,
45 | ResponseHeader: true,
46 | ResponseBody: false,
47 | },
48 | },
49 | })
50 | obj := "pic/watermark.png"
51 | localPath := "test.jpeg"
52 | opt := &cos.ImageRepairOptions{
53 | MaskPic: "xxx",
54 | }
55 | resp, err := c.CI.GetImageRepair(context.Background(), obj, opt)
56 | log_status(err)
57 | if err == nil {
58 | fd, _ := os.OpenFile(localPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
59 | io.Copy(fd, resp.Body)
60 | fd.Close()
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/example/CI/ai_recognition/ocr.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func log_status(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("WARN: Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("ERROR: Code: %v\n", e.Code)
23 | fmt.Printf("ERROR: Message: %v\n", e.Message)
24 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Printf("ERROR: %v\n", err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com")
35 | b := &cos.BaseURL{BucketURL: u}
36 | c := cos.NewClient(b, &http.Client{
37 | Transport: &cos.AuthorizationTransport{
38 | SecretID: os.Getenv("COS_SECRETID"),
39 | SecretKey: os.Getenv("COS_SECRETKEY"),
40 | Transport: &debug.DebugRequestTransport{
41 | RequestHeader: true,
42 | // Notice when put a large file and set need the request body, might happend out of memory error.
43 | RequestBody: false,
44 | ResponseHeader: true,
45 | ResponseBody: false,
46 | },
47 | },
48 | })
49 | obj := "pic/ocr.png"
50 | opt := &cos.OcrRecognitionOptions{
51 | Type: "general",
52 | LanguageType: "zh",
53 | Isword: true,
54 | EnableWordPolygon: true,
55 | }
56 | res, _, err := c.CI.OcrRecognition(context.Background(), obj, opt)
57 | log_status(err)
58 | fmt.Printf("%+v\n", res)
59 | }
60 |
--------------------------------------------------------------------------------
/example/CI/ai_recognition/pic_tag.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func log_status(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("WARN: Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("ERROR: Code: %v\n", e.Code)
23 | fmt.Printf("ERROR: Message: %v\n", e.Message)
24 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Printf("ERROR: %v\n", err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func picTag() {
34 | u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com")
35 | b := &cos.BaseURL{CIURL: u}
36 | c := cos.NewClient(b, &http.Client{
37 | Transport: &cos.AuthorizationTransport{
38 | SecretID: os.Getenv("COS_SECRETID"),
39 | SecretKey: os.Getenv("COS_SECRETKEY"),
40 | Transport: &debug.DebugRequestTransport{
41 | RequestHeader: true,
42 | // Notice when put a large file and set need the request body, might happend out of memory error.
43 | RequestBody: true,
44 | ResponseHeader: true,
45 | ResponseBody: true,
46 | },
47 | },
48 | })
49 |
50 | obj := "pic/deer.jpg"
51 | res, _, err := c.CI.PicTag(context.Background(), obj)
52 | log_status(err)
53 | fmt.Printf("%+v\n", res)
54 | }
55 |
56 | func main() {
57 | picTag()
58 | }
59 |
--------------------------------------------------------------------------------
/example/CI/ai_recognition/recognize_logo.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func log_status(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("WARN: Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("ERROR: Code: %v\n", e.Code)
23 | fmt.Printf("ERROR: Message: %v\n", e.Message)
24 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Printf("ERROR: %v\n", err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com")
35 | b := &cos.BaseURL{BucketURL: u}
36 | c := cos.NewClient(b, &http.Client{
37 | Transport: &cos.AuthorizationTransport{
38 | SecretID: os.Getenv("COS_SECRETID"),
39 | SecretKey: os.Getenv("COS_SECRETKEY"),
40 | Transport: &debug.DebugRequestTransport{
41 | RequestHeader: true,
42 | // Notice when put a large file and set need the request body, might happend out of memory error.
43 | RequestBody: false,
44 | ResponseHeader: true,
45 | ResponseBody: true,
46 | },
47 | },
48 | })
49 | obj := "pic/logo.jpeg"
50 | opt := &cos.RecognizeLogoOptions{}
51 | res, _, err := c.CI.GetRecognizeLogo(context.Background(), obj, opt)
52 | log_status(err)
53 | fmt.Printf("%+v\n", res)
54 | }
55 |
--------------------------------------------------------------------------------
/example/CI/content_auditing/ci_audio_auditing_job.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 | "time"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func log_status(err error) {
16 | if err == nil {
17 | return
18 | }
19 | if cos.IsNotFoundError(err) {
20 | // WARN
21 | fmt.Println("WARN: Resource is not existed")
22 | } else if e, ok := cos.IsCOSError(err); ok {
23 | fmt.Printf("ERROR: Code: %v\n", e.Code)
24 | fmt.Printf("ERROR: Message: %v\n", e.Message)
25 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
26 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
27 | // ERROR
28 | } else {
29 | fmt.Printf("ERROR: %v\n", err)
30 | // ERROR
31 | }
32 | }
33 |
34 | func main() {
35 | bu, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
36 | cu, _ := url.Parse("https://test-1259654469.ci.ap-guangzhou.myqcloud.com")
37 | b := &cos.BaseURL{BucketURL: bu, CIURL: cu}
38 | c := cos.NewClient(b, &http.Client{
39 | Transport: &cos.AuthorizationTransport{
40 | SecretID: os.Getenv("COS_SECRETID"),
41 | SecretKey: os.Getenv("COS_SECRETKEY"),
42 | Transport: &debug.DebugRequestTransport{
43 | RequestHeader: true,
44 | RequestBody: true,
45 | ResponseHeader: true,
46 | ResponseBody: true,
47 | },
48 | },
49 | })
50 | opt := &cos.PutAudioAuditingJobOptions{
51 | InputObject: "test.mp3",
52 | Conf: &cos.AudioAuditingJobConf{},
53 | }
54 |
55 | res, _, err := c.CI.PutAudioAuditingJob(context.Background(), opt)
56 | log_status(err)
57 | fmt.Printf("%+v\n", res)
58 |
59 | time.Sleep(3 * time.Second)
60 | res2, _, err := c.CI.GetAudioAuditingJob(context.Background(), res.JobsDetail.JobId)
61 | log_status(err)
62 | fmt.Printf("%+v\n", res2)
63 | }
64 |
--------------------------------------------------------------------------------
/example/CI/content_auditing/ci_image_recognition.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func log_status(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("WARN: Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("ERROR: Code: %v\n", e.Code)
23 | fmt.Printf("ERROR: Message: %v\n", e.Message)
24 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Printf("ERROR: %v\n", err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
35 | b := &cos.BaseURL{BucketURL: u}
36 | c := cos.NewClient(b, &http.Client{
37 | Transport: &cos.AuthorizationTransport{
38 | SecretID: os.Getenv("COS_SECRETID"),
39 | SecretKey: os.Getenv("COS_SECRETKEY"),
40 | Transport: &debug.DebugRequestTransport{
41 | RequestHeader: true,
42 | RequestBody: true,
43 | ResponseHeader: true,
44 | ResponseBody: true,
45 | },
46 | },
47 | })
48 | name := "test.jpg"
49 | res, _, err := c.CI.ImageRecognition(context.Background(), name, "")
50 | log_status(err)
51 | fmt.Printf("%+v\n", res)
52 | }
53 |
--------------------------------------------------------------------------------
/example/CI/content_auditing/ci_video_auditing_job.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 | "time"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func log_status(err error) {
16 | if err == nil {
17 | return
18 | }
19 | if cos.IsNotFoundError(err) {
20 | // WARN
21 | fmt.Println("WARN: Resource is not existed")
22 | } else if e, ok := cos.IsCOSError(err); ok {
23 | fmt.Printf("ERROR: Code: %v\n", e.Code)
24 | fmt.Printf("ERROR: Message: %v\n", e.Message)
25 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
26 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
27 | // ERROR
28 | } else {
29 | fmt.Printf("ERROR: %v\n", err)
30 | // ERROR
31 | }
32 | }
33 |
34 | func main() {
35 | bu, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
36 | cu, _ := url.Parse("https://test-1259654469.ci.ap-guangzhou.myqcloud.com")
37 | b := &cos.BaseURL{BucketURL: bu, CIURL: cu}
38 | c := cos.NewClient(b, &http.Client{
39 | Transport: &cos.AuthorizationTransport{
40 | SecretID: os.Getenv("COS_SECRETID"),
41 | SecretKey: os.Getenv("COS_SECRETKEY"),
42 | Transport: &debug.DebugRequestTransport{
43 | RequestHeader: true,
44 | RequestBody: true,
45 | ResponseHeader: true,
46 | ResponseBody: true,
47 | },
48 | },
49 | })
50 | opt := &cos.PutVideoAuditingJobOptions{
51 | InputObject: "demo.mp4",
52 | Conf: &cos.VideoAuditingJobConf{
53 | DetectType: "Porn,Terrorism,Politics,Ads",
54 | Snapshot: &cos.PutVideoAuditingJobSnapshot{
55 | Mode: "Interval",
56 | Start: 0.5,
57 | TimeInterval: 50.5,
58 | Count: 100,
59 | },
60 | },
61 | }
62 |
63 | res, _, err := c.CI.PutVideoAuditingJob(context.Background(), opt)
64 | log_status(err)
65 | fmt.Printf("%+v\n", res)
66 |
67 | time.Sleep(3 * time.Second)
68 | res2, _, err := c.CI.GetVideoAuditingJob(context.Background(), res.JobsDetail.JobId)
69 | log_status(err)
70 | fmt.Printf("%+v\n", res2)
71 | }
72 |
--------------------------------------------------------------------------------
/example/CI/doc_preview/doc_compare.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func log_status(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("WARN: Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("ERROR: Code: %v\n", e.Code)
23 | fmt.Printf("ERROR: Message: %v\n", e.Message)
24 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Printf("ERROR: %v\n", err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com")
35 | cu, _ := url.Parse("https://test-1234567890.ci.ap-chongqing.myqcloud.com")
36 | b := &cos.BaseURL{BucketURL: u, CIURL: cu}
37 | c := cos.NewClient(b, &http.Client{
38 | Transport: &cos.AuthorizationTransport{
39 | SecretID: os.Getenv("COS_SECRETID"),
40 | SecretKey: os.Getenv("COS_SECRETKEY"),
41 | Transport: &debug.DebugRequestTransport{
42 | RequestHeader: true,
43 | // Notice when put a large file and set need the request body, might happend out of memory error.
44 | RequestBody: true,
45 | ResponseHeader: true,
46 | ResponseBody: false,
47 | },
48 | },
49 | })
50 |
51 | opt := &cos.CIDocCompareOptions{
52 | Object: "doc/1.docx",
53 | ComparePath: "doc/2.docx",
54 | }
55 | _, res, err := c.CI.CIDocCompare(context.Background(), opt)
56 | fmt.Printf("%+v\n", res)
57 | log_status(err)
58 | }
59 |
--------------------------------------------------------------------------------
/example/CI/doc_preview/get_doc_preview.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "io"
7 | "net/http"
8 | "net/url"
9 | "os"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func log_status(err error) {
16 | if err == nil {
17 | return
18 | }
19 | if cos.IsNotFoundError(err) {
20 | // WARN
21 | fmt.Println("WARN: Resource is not existed")
22 | } else if e, ok := cos.IsCOSError(err); ok {
23 | fmt.Printf("ERROR: Code: %v\n", e.Code)
24 | fmt.Printf("ERROR: Message: %v\n", e.Message)
25 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
26 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
27 | // ERROR
28 | } else {
29 | fmt.Printf("ERROR: %v\n", err)
30 | // ERROR
31 | }
32 | }
33 |
34 | func main() {
35 | u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com")
36 | cu, _ := url.Parse("https://test-1234567890.ci.ap-chongqing.myqcloud.com")
37 | b := &cos.BaseURL{BucketURL: u, CIURL: cu}
38 | c := cos.NewClient(b, &http.Client{
39 | Transport: &cos.AuthorizationTransport{
40 | SecretID: os.Getenv("COS_SECRETID"),
41 | SecretKey: os.Getenv("COS_SECRETKEY"),
42 | Transport: &debug.DebugRequestTransport{
43 | RequestHeader: true,
44 | // Notice when put a large file and set need the request body, might happend out of memory error.
45 | RequestBody: true,
46 | ResponseHeader: true,
47 | ResponseBody: false,
48 | },
49 | },
50 | })
51 |
52 | key := "input/doc_preview.ppt"
53 | opt := &cos.DocPreviewOptions{
54 | DstType: "pdf",
55 | }
56 | resp, err := c.CI.DocPreview(context.Background(), key, opt)
57 | log_status(err)
58 | fd, _ := os.OpenFile("doc_preview.pdf", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
59 | io.Copy(fd, resp.Body)
60 | fd.Close()
61 | }
62 |
--------------------------------------------------------------------------------
/example/CI/image_process/tdc_refresh.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func log_status(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("WARN: Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("ERROR: Code: %v\n", e.Code)
23 | fmt.Printf("ERROR: Message: %v\n", e.Message)
24 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Printf("ERROR: %v\n", err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | u, _ := url.Parse("https://test-1234567890.pic.ap-chongqing.myqcloud.com")
35 | b := &cos.BaseURL{CIURL: u}
36 | c := cos.NewClient(b, &http.Client{
37 | Transport: &cos.AuthorizationTransport{
38 | SecretID: os.Getenv("COS_SECRETID"),
39 | SecretKey: os.Getenv("COS_SECRETKEY"),
40 | Transport: &debug.DebugRequestTransport{
41 | RequestHeader: true,
42 | // Notice when put a large file and set need the request body, might happend out of memory error.
43 | RequestBody: false,
44 | ResponseHeader: true,
45 | ResponseBody: false,
46 | },
47 | },
48 | })
49 | name := "test.pic"
50 | _, err := c.CI.TDCRefresh(context.Background(), name)
51 | log_status(err)
52 | }
53 |
--------------------------------------------------------------------------------
/example/CI/media_process/hls_decrypt_server.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "encoding/base64"
5 | "fmt"
6 | "net"
7 | "net/http"
8 | "net/url"
9 | "os"
10 |
11 | kms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms/v20190118"
12 | )
13 |
14 | func handler(w http.ResponseWriter, r *http.Request) {
15 | m, err := url.ParseQuery(r.URL.RawQuery)
16 | if err != nil {
17 | fmt.Fprint(w, err.Error())
18 | return
19 | }
20 |
21 | ciphertext := m.Get("Ciphertext")
22 | kmsRegion := m.Get("KMSRegion")
23 | if len(ciphertext) == 0 || len(kmsRegion) == 0 {
24 | fmt.Fprint(w, "Ciphertext or KMSRegion is empty")
25 | return
26 | }
27 |
28 | c, err := kms.NewClientWithSecretId(os.Getenv("COS_SECRETID"), os.Getenv("COS_SECRETKEY"), kmsRegion)
29 | if err != nil {
30 | fmt.Fprint(w, err.Error())
31 | return
32 | }
33 |
34 | req := kms.NewDecryptRequest()
35 | req.CiphertextBlob = &ciphertext
36 | rsp, err := c.Decrypt(req)
37 | if err != nil {
38 | fmt.Fprint(w, err.Error())
39 | return
40 | }
41 | s, err := base64.StdEncoding.DecodeString(*rsp.Response.Plaintext)
42 | if err != nil {
43 | fmt.Fprint(w, err.Error())
44 | return
45 | }
46 | fmt.Fprint(w, string(s))
47 | }
48 |
49 | func GetLocalIp() string {
50 | localIP := "127.0.0.1"
51 | addrSlice, err := net.InterfaceAddrs()
52 | if nil == err {
53 | for _, addr := range addrSlice {
54 | if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
55 | if nil != ipnet.IP.To4() {
56 | localIP = ipnet.IP.String()
57 | break
58 | }
59 | }
60 | }
61 | }
62 | return localIP
63 | }
64 |
65 | func main() {
66 | http.HandleFunc("/", handler)
67 | http.ListenAndServe(GetLocalIp()+":8082", nil)
68 | }
69 |
--------------------------------------------------------------------------------
/example/CI/workflow_and_job/dna.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 |
7 | "github.com/tencentyun/cos-go-sdk-v5"
8 | )
9 |
10 | func log_status(err error) {
11 | if err == nil {
12 | return
13 | }
14 | if cos.IsNotFoundError(err) {
15 | // WARN
16 | fmt.Println("WARN: Resource is not existed")
17 | } else if e, ok := cos.IsCOSError(err); ok {
18 | fmt.Printf("ERROR: Code: %v\n", e.Code)
19 | fmt.Printf("ERROR: Message: %v\n", e.Message)
20 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
21 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
22 | // ERROR
23 | } else {
24 | fmt.Printf("ERROR: %v\n", err)
25 | // ERROR
26 | }
27 | }
28 |
29 | // InvokeDNAJob 提交一个DNA任务
30 | // https://cloud.tencent.com/document/product/460/96115
31 | func InvokeDNAJob() {
32 | c := getClient()
33 | createJobOpt := &cos.CreateJobsOptions{
34 | Tag: "DNA",
35 | Input: &cos.JobInput{
36 | Object: "input/test.mp4",
37 | },
38 | Operation: &cos.MediaProcessJobOperation{
39 | DnaConfig: &cos.DnaConfig{
40 | RuleType: "GetFingerPrint",
41 | DnaDbId: "xxx",
42 | },
43 | UserData: "This is my DNA job",
44 | JobLevel: 1,
45 | },
46 | }
47 | createJobRes, _, err := c.CI.CreateJob(context.Background(), createJobOpt)
48 | log_status(err)
49 | fmt.Printf("%+v\n", createJobRes.JobsDetail)
50 | }
51 |
52 | // GetDnaDb 查询 DNA 库列表
53 | // https://cloud.tencent.com/document/product/460/96117
54 | func GetDnaDb() {
55 | c := getClient()
56 | opt := &cos.GetDnaDbOptions{
57 | PageNumber: "2",
58 | PageSize: "10",
59 | }
60 | res, _, err := c.CI.GetDnaDb(context.Background(), opt)
61 | log_status(err)
62 | fmt.Printf("%+v\n", res)
63 | }
64 |
65 | // GetDnaDbFiles 获取 DNA 库中文件列表
66 | // https://cloud.tencent.com/document/product/460/96116
67 | func GetDnaDbFiles() {
68 | c := getClient()
69 | opt := &cos.GetDnaDbFilesOptions{
70 | PageNumber: "2",
71 | PageSize: "10",
72 | }
73 | res, _, err := c.CI.GetDnaDbFiles(context.Background(), opt)
74 | log_status(err)
75 | fmt.Printf("%+v\n", res)
76 | }
77 |
78 | func main() {
79 | }
80 |
--------------------------------------------------------------------------------
/example/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | ```
4 | export COS_SECRETID=xx
5 | export COS_SECRETKEY=xxx
6 |
7 | go run xxx.go
8 | ```
9 |
--------------------------------------------------------------------------------
/example/batch/delete_job.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/http"
6 | "net/url"
7 | "os"
8 |
9 | "github.com/tencentyun/cos-go-sdk-v5"
10 | "github.com/tencentyun/cos-go-sdk-v5/debug"
11 | )
12 |
13 | func main() {
14 | uin := "100010805041"
15 | appid := 1259654469
16 | jobid := "49e0dd01-27a6-41a6-97b2-dda3cca19223"
17 | u, _ := url.Parse("https://" + uin + ".cos-control.ap-guangzhou.myqcloud.com")
18 | b := &cos.BaseURL{BatchURL: u}
19 | c := cos.NewClient(b, &http.Client{
20 | Transport: &cos.AuthorizationTransport{
21 | SecretID: os.Getenv("COS_SECRETID"),
22 | SecretKey: os.Getenv("COS_SECRETKEY"),
23 | Transport: &debug.DebugRequestTransport{
24 | RequestHeader: true,
25 | RequestBody: true,
26 | ResponseHeader: true,
27 | ResponseBody: true,
28 | },
29 | },
30 | })
31 |
32 | headers := &cos.BatchRequestHeaders{
33 | XCosAppid: appid,
34 | }
35 |
36 | _, err := c.Batch.DeleteJob(context.Background(), jobid, headers)
37 | if err != nil {
38 | panic(err)
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/example/batch/describe_job.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/http"
6 | "net/url"
7 | "os"
8 |
9 | "fmt"
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func main() {
15 | uin := "100010805041"
16 | appid := 1259654469
17 | jobid := "795ad997-5557-4869-9a19-b66ec087d460"
18 | u, _ := url.Parse("https://" + uin + ".cos-control.ap-chengdu.myqcloud.com")
19 | b := &cos.BaseURL{BatchURL: u}
20 | c := cos.NewClient(b, &http.Client{
21 | Transport: &cos.AuthorizationTransport{
22 | SecretID: os.Getenv("COS_SECRETID"),
23 | SecretKey: os.Getenv("COS_SECRETKEY"),
24 | Transport: &debug.DebugRequestTransport{
25 | RequestHeader: true,
26 | RequestBody: true,
27 | ResponseHeader: true,
28 | ResponseBody: true,
29 | },
30 | },
31 | })
32 |
33 | headers := &cos.BatchRequestHeaders{
34 | XCosAppid: appid,
35 | }
36 |
37 | res, _, err := c.Batch.DescribeJob(context.Background(), jobid, headers)
38 | if err != nil {
39 | panic(err)
40 | }
41 | if res != nil && res.Job != nil {
42 | fmt.Printf("%+v", res.Job)
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/example/batch/list_jobs.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/http"
6 | "net/url"
7 | "os"
8 |
9 | "fmt"
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func main() {
15 | uin := "100010805041"
16 | appid := 1259654469
17 | u, _ := url.Parse("https://" + uin + ".cos-control.ap-chengdu.myqcloud.com")
18 | b := &cos.BaseURL{BatchURL: u}
19 | c := cos.NewClient(b, &http.Client{
20 | Transport: &cos.AuthorizationTransport{
21 | SecretID: os.Getenv("COS_SECRETID"),
22 | SecretKey: os.Getenv("COS_SECRETKEY"),
23 | Transport: &debug.DebugRequestTransport{
24 | RequestHeader: true,
25 | RequestBody: true,
26 | ResponseHeader: true,
27 | ResponseBody: true,
28 | },
29 | },
30 | })
31 |
32 | headers := &cos.BatchRequestHeaders{
33 | XCosAppid: appid,
34 | }
35 |
36 | res, _, err := c.Batch.ListJobs(context.Background(), nil, headers)
37 | if err != nil {
38 | panic(err)
39 | }
40 | if res != nil && res.Jobs != nil {
41 | fmt.Printf("%+v", res.Jobs)
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/example/batch/update_priority.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/http"
6 | "net/url"
7 | "os"
8 |
9 | "fmt"
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func main() {
15 | uin := "100010805041"
16 | appid := 1259654469
17 | jobid := "795ad997-5557-4869-9a19-b66ec087d460"
18 | u, _ := url.Parse("https://" + uin + ".cos-control.ap-chengdu.myqcloud.com")
19 | b := &cos.BaseURL{BatchURL: u}
20 | c := cos.NewClient(b, &http.Client{
21 | Transport: &cos.AuthorizationTransport{
22 | SecretID: os.Getenv("COS_SECRETID"),
23 | SecretKey: os.Getenv("COS_SECRETKEY"),
24 | Transport: &debug.DebugRequestTransport{
25 | RequestHeader: true,
26 | RequestBody: true,
27 | ResponseHeader: true,
28 | ResponseBody: true,
29 | },
30 | },
31 | })
32 |
33 | opt := &cos.BatchUpdatePriorityOptions{
34 | JobId: jobid,
35 | Priority: 3,
36 | }
37 | headers := &cos.BatchRequestHeaders{
38 | XCosAppid: appid,
39 | }
40 |
41 | res, _, err := c.Batch.UpdateJobPriority(context.Background(), opt, headers)
42 | if err != nil {
43 | panic(err)
44 | }
45 | if res != nil {
46 | fmt.Printf("%+v", res)
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/example/batch/update_status.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/http"
6 | "net/url"
7 | "os"
8 |
9 | "fmt"
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func main() {
15 | uin := "100010805041"
16 | appid := 1259654469
17 | jobid := "289b0ea1-5ac5-453d-8a61-7f452dd4a209"
18 | u, _ := url.Parse("https://" + uin + ".cos-control.ap-chengdu.myqcloud.com")
19 | b := &cos.BaseURL{BatchURL: u}
20 | c := cos.NewClient(b, &http.Client{
21 | Transport: &cos.AuthorizationTransport{
22 | SecretID: os.Getenv("COS_SECRETID"),
23 | SecretKey: os.Getenv("COS_SECRETKEY"),
24 | Transport: &debug.DebugRequestTransport{
25 | RequestHeader: true,
26 | RequestBody: true,
27 | ResponseHeader: true,
28 | ResponseBody: true,
29 | },
30 | },
31 | })
32 |
33 | opt := &cos.BatchUpdateStatusOptions{
34 | JobId: jobid,
35 | RequestedJobStatus: "Ready", // 允许状态转换见 https://cloud.tencent.com/document/product/436/38604
36 | StatusUpdateReason: "to test",
37 | }
38 | headers := &cos.BatchRequestHeaders{
39 | XCosAppid: appid,
40 | }
41 |
42 | res, _, err := c.Batch.UpdateJobStatus(context.Background(), opt, headers)
43 | if err != nil {
44 | panic(err)
45 | }
46 | if res != nil {
47 | fmt.Printf("%+v", res)
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/example/bucket/accelerate.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func logStatus(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("Code: %v\n", e.Code)
23 | fmt.Printf("Message: %v\n", e.Message)
24 | fmt.Printf("Resource: %v\n", e.Resource)
25 | fmt.Printf("RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Println(err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
35 | b := &cos.BaseURL{
36 | BucketURL: u,
37 | }
38 | c := cos.NewClient(b, &http.Client{
39 | Transport: &cos.AuthorizationTransport{
40 | SecretID: os.Getenv("SECRETID"),
41 | SecretKey: os.Getenv("SECRETKEY"),
42 | Transport: &debug.DebugRequestTransport{
43 | RequestHeader: true,
44 | RequestBody: true,
45 | ResponseHeader: true,
46 | ResponseBody: true,
47 | },
48 | },
49 | })
50 | res, _, err := c.Bucket.GetAccelerate(context.Background())
51 | logStatus(err)
52 | fmt.Printf("%+v\n", res)
53 |
54 | opt := &cos.BucketPutAccelerateOptions{
55 | Status: "Enabled",
56 | }
57 | _, err = c.Bucket.PutAccelerate(context.Background(), opt)
58 | logStatus(err)
59 |
60 | res, _, err = c.Bucket.GetAccelerate(context.Background())
61 | logStatus(err)
62 | fmt.Printf("%+v\n", res)
63 |
64 | opt.Status = "Suspended"
65 | _, err = c.Bucket.PutAccelerate(context.Background(), opt)
66 | logStatus(err)
67 |
68 | res, _, err = c.Bucket.GetAccelerate(context.Background())
69 | logStatus(err)
70 | fmt.Printf("%+v\n", res)
71 | }
72 |
--------------------------------------------------------------------------------
/example/bucket/delPolicy.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/http"
6 | "net/url"
7 | "os"
8 |
9 | "github.com/tencentyun/cos-go-sdk-v5"
10 | "github.com/tencentyun/cos-go-sdk-v5/debug"
11 | )
12 |
13 | func main() {
14 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
15 | b := &cos.BaseURL{
16 | BucketURL: u,
17 | }
18 | c := cos.NewClient(b, &http.Client{
19 | Transport: &cos.AuthorizationTransport{
20 | SecretID: os.Getenv("SECRETID"),
21 | SecretKey: os.Getenv("SECRETKEY"),
22 | Transport: &debug.DebugRequestTransport{
23 | RequestHeader: true,
24 | RequestBody: true,
25 | ResponseHeader: true,
26 | ResponseBody: true,
27 | },
28 | },
29 | })
30 |
31 | _, err := c.Bucket.DeletePolicy(context.Background())
32 | if err != nil {
33 | panic(err)
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/example/bucket/delWebsite.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/http"
6 | "net/url"
7 | "os"
8 |
9 | "github.com/tencentyun/cos-go-sdk-v5"
10 | "github.com/tencentyun/cos-go-sdk-v5/debug"
11 | )
12 |
13 | func main() {
14 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
15 | b := &cos.BaseURL{
16 | BucketURL: u,
17 | }
18 | c := cos.NewClient(b, &http.Client{
19 | Transport: &cos.AuthorizationTransport{
20 | SecretID: os.Getenv("SECRETID"),
21 | SecretKey: os.Getenv("SECRETKEY"),
22 | Transport: &debug.DebugRequestTransport{
23 | RequestHeader: true,
24 | RequestBody: true,
25 | ResponseHeader: true,
26 | ResponseBody: true,
27 | },
28 | },
29 | })
30 |
31 | _, err := c.Bucket.DeleteWebsite(context.Background())
32 | if err != nil {
33 | panic(err)
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/example/bucket/delete.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/url"
6 | "os"
7 |
8 | "net/http"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func main() {
15 | u, _ := url.Parse("https://testdelete-1253846586.cos.ap-guangzhou.myqcloud.com")
16 | b := &cos.BaseURL{
17 | BucketURL: u,
18 | }
19 | c := cos.NewClient(b, &http.Client{
20 | Transport: &cos.AuthorizationTransport{
21 | SecretID: os.Getenv("SECRETID"),
22 | SecretKey: os.Getenv("SECRETKEY"),
23 | Transport: &debug.DebugRequestTransport{
24 | RequestHeader: true,
25 | RequestBody: true,
26 | ResponseHeader: true,
27 | ResponseBody: true,
28 | },
29 | },
30 | })
31 |
32 | _, err := c.Bucket.Delete(context.Background())
33 | if err != nil {
34 | panic(err)
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/example/bucket/deleteCORS.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/url"
6 | "os"
7 |
8 | "net/http"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func main() {
15 | u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
16 | b := &cos.BaseURL{
17 | BucketURL: u,
18 | }
19 | c := cos.NewClient(b, &http.Client{
20 | Transport: &cos.AuthorizationTransport{
21 | SecretID: os.Getenv("SECRETID"),
22 | SecretKey: os.Getenv("SECRETKEY"),
23 | Transport: &debug.DebugRequestTransport{
24 | RequestHeader: true,
25 | RequestBody: true,
26 | ResponseHeader: true,
27 | ResponseBody: true,
28 | },
29 | },
30 | })
31 |
32 | _, err := c.Bucket.DeleteCORS(context.Background())
33 | if err != nil {
34 | panic(err)
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/example/bucket/deleteLifecycle.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/url"
6 | "os"
7 |
8 | "net/http"
9 | "github.com/tencentyun/cos-go-sdk-v5"
10 | "github.com/tencentyun/cos-go-sdk-v5/debug"
11 | )
12 |
13 | func main() {
14 | u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
15 | b := &cos.BaseURL{
16 | BucketURL: u,
17 | }
18 | c := cos.NewClient(b, &http.Client{
19 | Transport: &cos.AuthorizationTransport{
20 | SecretID: os.Getenv("SECRETID"),
21 | SecretKey: os.Getenv("SECRETKEY"),
22 | Transport: &debug.DebugRequestTransport{
23 | RequestHeader: true,
24 | RequestBody: true,
25 | ResponseHeader: true,
26 | ResponseBody: true,
27 | },
28 | },
29 | })
30 |
31 | _, err := c.Bucket.DeleteLifecycle(context.Background())
32 | if err != nil {
33 | panic(err)
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/example/bucket/deleteReplication.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/url"
6 | "os"
7 |
8 | "net/http"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func main() {
15 | u, _ := url.Parse("https://alanbj-1251668577.cos.ap-beijing.myqcloud.com")
16 | b := &cos.BaseURL{
17 | BucketURL: u,
18 | }
19 | c := cos.NewClient(b, &http.Client{
20 | Transport: &cos.AuthorizationTransport{
21 | SecretID: os.Getenv("SECRETID"),
22 | SecretKey: os.Getenv("SECRETKEY"),
23 | Transport: &debug.DebugRequestTransport{
24 | RequestHeader: true,
25 | RequestBody: true,
26 | ResponseHeader: true,
27 | ResponseBody: true,
28 | },
29 | },
30 | })
31 |
32 | _, err := c.Bucket.DeleteBucketReplication(context.Background())
33 | if err != nil {
34 | panic(err)
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/example/bucket/deleteTagging.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/url"
6 | "os"
7 |
8 | "net/http"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func main() {
15 | u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
16 | b := &cos.BaseURL{
17 | BucketURL: u,
18 | }
19 | c := cos.NewClient(b, &http.Client{
20 | Transport: &cos.AuthorizationTransport{
21 | SecretID: os.Getenv("SECRETID"),
22 | SecretKey: os.Getenv("SECRETKEY"),
23 | Transport: &debug.DebugRequestTransport{
24 | RequestHeader: true,
25 | RequestBody: true,
26 | ResponseHeader: true,
27 | ResponseBody: true,
28 | },
29 | },
30 | })
31 |
32 | _, err := c.Bucket.DeleteTagging(context.Background())
33 | if err != nil {
34 | panic(err)
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/example/bucket/domain.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func logStatus(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("WARN: Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("ERROR: Code: %v\n", e.Code)
23 | fmt.Printf("ERROR: Message: %v\n", e.Message)
24 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Printf("ERROR: %v\n", err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
35 | b := &cos.BaseURL{
36 | BucketURL: u,
37 | }
38 | c := cos.NewClient(b, &http.Client{
39 | Transport: &cos.AuthorizationTransport{
40 | SecretID: os.Getenv("SECRETID"),
41 | SecretKey: os.Getenv("SECRETKEY"),
42 | Transport: &debug.DebugRequestTransport{
43 | RequestHeader: true,
44 | RequestBody: true,
45 | ResponseHeader: true,
46 | ResponseBody: true,
47 | },
48 | },
49 | })
50 |
51 | opt := &cos.BucketPutDomainOptions{
52 | Rules: []cos.BucketDomainRule{
53 | {
54 | Status: "ENABLED",
55 | Name: "www.qq.com",
56 | Type: "REST",
57 | ForcedReplacement: "CNAME",
58 | },
59 | },
60 | }
61 |
62 | _, err := c.Bucket.PutDomain(context.Background(), opt)
63 | logStatus(err)
64 |
65 | res, _, err := c.Bucket.GetDomain(context.Background())
66 | logStatus(err)
67 | fmt.Printf("%+v\n", res)
68 |
69 | _, err = c.Bucket.DeleteDomain(context.Background())
70 | logStatus(err)
71 | }
72 |
--------------------------------------------------------------------------------
/example/bucket/encryption.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "encoding/xml"
6 | "fmt"
7 | "net/http"
8 | "net/url"
9 | "os"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func logStatus(err error) {
16 | if err == nil {
17 | return
18 | }
19 | if cos.IsNotFoundError(err) {
20 | // WARN
21 | fmt.Println("Resource is not existed")
22 | } else if e, ok := cos.IsCOSError(err); ok {
23 | fmt.Printf("Code: %v\n", e.Code)
24 | fmt.Printf("Message: %v\n", e.Message)
25 | fmt.Printf("Resource: %v\n", e.Resource)
26 | fmt.Printf("RequestId: %v\n", e.RequestID)
27 | // ERROR
28 | } else {
29 | fmt.Println(err)
30 | // ERROR
31 | }
32 | }
33 |
34 | func main() {
35 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
36 | b := &cos.BaseURL{
37 | BucketURL: u,
38 | }
39 | c := cos.NewClient(b, &http.Client{
40 | Transport: &cos.AuthorizationTransport{
41 | SecretID: os.Getenv("SECRETID"),
42 | SecretKey: os.Getenv("SECRETKEY"),
43 | Transport: &debug.DebugRequestTransport{
44 | RequestHeader: true,
45 | RequestBody: true,
46 | ResponseHeader: true,
47 | ResponseBody: true,
48 | },
49 | },
50 | })
51 |
52 | opt := &cos.BucketPutEncryptionOptions{
53 | XMLName: xml.Name{Local: "ServerSideEncryptionConfiguration"},
54 | Rule: &cos.BucketEncryptionConfiguration{
55 | SSEAlgorithm: "AES256",
56 | },
57 | }
58 |
59 | _, err := c.Bucket.PutEncryption(context.Background(), opt)
60 | logStatus(err)
61 |
62 | res, _, err := c.Bucket.GetEncryption(context.Background())
63 | logStatus(err)
64 | fmt.Printf("%+v\n", res)
65 |
66 | _, err = c.Bucket.DeleteEncryption(context.Background())
67 | logStatus(err)
68 | }
69 |
--------------------------------------------------------------------------------
/example/bucket/get.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "os"
7 |
8 | "net/url"
9 |
10 | "net/http"
11 |
12 | "github.com/tencentyun/cos-go-sdk-v5"
13 | "github.com/tencentyun/cos-go-sdk-v5/debug"
14 | )
15 |
16 | func main() {
17 | u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
18 | b := &cos.BaseURL{
19 | BucketURL: u,
20 | }
21 | c := cos.NewClient(b, &http.Client{
22 | Transport: &cos.AuthorizationTransport{
23 | SecretID: os.Getenv("SECRETID"),
24 | SecretKey: os.Getenv("SECRETKEY"),
25 | Transport: &debug.DebugRequestTransport{
26 | RequestHeader: true,
27 | RequestBody: true,
28 | ResponseHeader: true,
29 | ResponseBody: true,
30 | },
31 | },
32 | })
33 |
34 | opt := &cos.BucketGetOptions{
35 | Prefix: "test",
36 | MaxKeys: 3,
37 | }
38 | v, _, err := c.Bucket.Get(context.Background(), opt)
39 | if err != nil {
40 | panic(err)
41 | }
42 |
43 | for _, c := range v.Contents {
44 | fmt.Printf("%s, %d\n", c.Key, c.Size)
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/example/bucket/getACL.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 | "os"
8 |
9 | "net/http"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func main() {
16 | u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
17 | b := &cos.BaseURL{
18 | BucketURL: u,
19 | }
20 | c := cos.NewClient(b, &http.Client{
21 | Transport: &cos.AuthorizationTransport{
22 | SecretID: os.Getenv("SECRETID"),
23 | SecretKey: os.Getenv("SECRETKEY"),
24 | Transport: &debug.DebugRequestTransport{
25 | RequestHeader: true,
26 | RequestBody: true,
27 | ResponseHeader: true,
28 | ResponseBody: true,
29 | },
30 | },
31 | })
32 |
33 | v, _, err := c.Bucket.GetACL(context.Background())
34 | if err != nil {
35 | panic(err)
36 | }
37 | for _, a := range v.AccessControlList {
38 | fmt.Printf("%s, %s, %s\n", a.Grantee.Type, a.Grantee.ID, a.Permission)
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/example/bucket/getCORS.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 | "os"
8 |
9 | "net/http"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func main() {
16 | u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
17 | b := &cos.BaseURL{
18 | BucketURL: u,
19 | }
20 | c := cos.NewClient(b, &http.Client{
21 | Transport: &cos.AuthorizationTransport{
22 | SecretID: os.Getenv("SECRETID"),
23 | SecretKey: os.Getenv("SECRETKEY"),
24 | Transport: &debug.DebugRequestTransport{
25 | RequestHeader: true,
26 | RequestBody: true,
27 | ResponseHeader: true,
28 | ResponseBody: true,
29 | },
30 | },
31 | })
32 |
33 | v, _, err := c.Bucket.GetCORS(context.Background())
34 | if err != nil {
35 | panic(err)
36 | }
37 | for _, r := range v.Rules {
38 |
39 | fmt.Printf("%s, %s\n", r.AllowedOrigins, r.AllowedMethods)
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/example/bucket/getLifecycle.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 | "os"
8 |
9 | "net/http"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func main() {
16 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
17 | b := &cos.BaseURL{
18 | BucketURL: u,
19 | }
20 | c := cos.NewClient(b, &http.Client{
21 | Transport: &cos.AuthorizationTransport{
22 | SecretID: os.Getenv("SECRETID"),
23 | SecretKey: os.Getenv("SECRETKEY"),
24 | Transport: &debug.DebugRequestTransport{
25 | RequestHeader: true,
26 | RequestBody: true,
27 | ResponseHeader: true,
28 | ResponseBody: true,
29 | },
30 | },
31 | })
32 |
33 | v, _, err := c.Bucket.GetLifecycle(context.Background())
34 | if err != nil {
35 | panic(err)
36 | }
37 | for _, r := range v.Rules {
38 | fmt.Printf("%+v\n", r.ID)
39 | fmt.Printf("%+v\n", r.Filter)
40 | fmt.Printf("%+v\n", r.Status)
41 | fmt.Printf("%+v\n", r.Transition)
42 | fmt.Printf("%+v\n", r.Expiration)
43 | fmt.Printf("%+v\n", r.NoncurrentVersionExpiration)
44 | fmt.Printf("%+v\n", r.NoncurrentVersionTransition)
45 | fmt.Printf("%+v\n", r.AbortIncompleteMultipartUpload)
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/example/bucket/getLocation.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 | "os"
8 |
9 | "net/http"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func main() {
16 | u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
17 | b := &cos.BaseURL{
18 | BucketURL: u,
19 | }
20 | c := cos.NewClient(b, &http.Client{
21 | Transport: &cos.AuthorizationTransport{
22 | SecretID: os.Getenv("SECRETID"),
23 | SecretKey: os.Getenv("SECRETKEY"),
24 | Transport: &debug.DebugRequestTransport{
25 | RequestHeader: true,
26 | RequestBody: true,
27 | ResponseHeader: true,
28 | ResponseBody: true,
29 | },
30 | },
31 | })
32 |
33 | v, _, err := c.Bucket.GetLocation(context.Background())
34 | if err != nil {
35 | panic(err)
36 | }
37 | fmt.Printf("%s\n", v.Location)
38 | }
39 |
--------------------------------------------------------------------------------
/example/bucket/getLogging.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 | "os"
8 |
9 | "net/http"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func main() {
16 | u, _ := url.Parse("https://bj-1259654469.cos.ap-beijing.myqcloud.com")
17 | b := &cos.BaseURL{
18 | BucketURL: u,
19 | }
20 | c := cos.NewClient(b, &http.Client{
21 | Transport: &cos.AuthorizationTransport{
22 | SecretID: os.Getenv("SECRETID"),
23 | SecretKey: os.Getenv("SECRETKEY"),
24 | Transport: &debug.DebugRequestTransport{
25 | RequestHeader: true,
26 | RequestBody: true,
27 | ResponseHeader: true,
28 | ResponseBody: true,
29 | },
30 | },
31 | })
32 |
33 | v, _, err := c.Bucket.GetLogging(context.Background())
34 | if err != nil {
35 | panic(err)
36 | }
37 | fmt.Printf("%+v\n", v.LoggingEnabled)
38 | }
39 |
--------------------------------------------------------------------------------
/example/bucket/getMeta.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 | "os"
8 |
9 | "net/http"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func main() {
16 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
17 | b := &cos.BaseURL{
18 | BucketURL: u,
19 | }
20 | c := cos.NewClient(b, &http.Client{
21 | Transport: &cos.AuthorizationTransport{
22 | SecretID: os.Getenv("SECRETID"),
23 | SecretKey: os.Getenv("SECRETKEY"),
24 | Transport: &debug.DebugRequestTransport{
25 | RequestHeader: true,
26 | RequestBody: true,
27 | ResponseHeader: true,
28 | ResponseBody: true,
29 | },
30 | },
31 | })
32 |
33 | v, _, err := c.Bucket.GetMeta(context.Background())
34 | if err != nil {
35 | panic(err)
36 | }
37 | fmt.Printf("%+v", *v)
38 | }
39 |
--------------------------------------------------------------------------------
/example/bucket/getObjectVersion.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "os"
7 |
8 | "net/url"
9 |
10 | "net/http"
11 |
12 | "github.com/tencentyun/cos-go-sdk-v5"
13 | "github.com/tencentyun/cos-go-sdk-v5/debug"
14 | )
15 |
16 | func logStatus(err error) {
17 | if err == nil {
18 | return
19 | }
20 | if cos.IsNotFoundError(err) {
21 | // WARN
22 | fmt.Println("WARN: Resource is not existed")
23 | } else if e, ok := cos.IsCOSError(err); ok {
24 | fmt.Printf("ERROR: Code: %v\n", e.Code)
25 | fmt.Printf("ERROR: Message: %v\n", e.Message)
26 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
27 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
28 | // ERROR
29 | } else {
30 | fmt.Printf("ERROR: %v\n", err)
31 | // ERROR
32 | }
33 | }
34 |
35 | func main() {
36 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
37 | b := &cos.BaseURL{
38 | BucketURL: u,
39 | }
40 | c := cos.NewClient(b, &http.Client{
41 | Transport: &cos.AuthorizationTransport{
42 | SecretID: os.Getenv("SECRETID"),
43 | SecretKey: os.Getenv("SECRETKEY"),
44 | Transport: &debug.DebugRequestTransport{
45 | RequestHeader: true,
46 | RequestBody: true,
47 | ResponseHeader: true,
48 | ResponseBody: true,
49 | },
50 | },
51 | })
52 |
53 | keyMarker := ""
54 | versionIdMarker := ""
55 | isTruncated := true
56 | opt := &cos.BucketGetObjectVersionsOptions{}
57 | for isTruncated {
58 | opt.KeyMarker = keyMarker
59 | opt.VersionIdMarker = versionIdMarker
60 | v, _, err := c.Bucket.GetObjectVersions(context.Background(), opt)
61 | if err != nil {
62 | logStatus(err)
63 | break
64 | }
65 | for _, vc := range v.Version {
66 | fmt.Printf("Version: %v, %v, %v, %v\n", vc.Key, vc.Size, vc.VersionId, vc.IsLatest)
67 | }
68 | for _, dc := range v.DeleteMarker {
69 | fmt.Printf("DeleteMarker: %v, %v, %v\n", dc.Key, dc.VersionId, dc.IsLatest)
70 | }
71 | keyMarker = v.NextKeyMarker
72 | versionIdMarker = v.NextVersionIdMarker
73 | isTruncated = v.IsTruncated
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/example/bucket/getPolicy.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "encoding/json"
6 | "fmt"
7 | "net/http"
8 | "net/url"
9 | "os"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func main() {
16 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
17 | b := &cos.BaseURL{
18 | BucketURL: u,
19 | }
20 | c := cos.NewClient(b, &http.Client{
21 | Transport: &cos.AuthorizationTransport{
22 | SecretID: os.Getenv("SECRETID"),
23 | SecretKey: os.Getenv("SECRETKEY"),
24 | Transport: &debug.DebugRequestTransport{
25 | RequestHeader: true,
26 | RequestBody: true,
27 | ResponseHeader: true,
28 | ResponseBody: true,
29 | },
30 | },
31 | })
32 |
33 | res, _, err := c.Bucket.GetPolicy(context.Background())
34 | if err != nil {
35 | panic(err)
36 | }
37 | bs, err := json.Marshal(res)
38 | fmt.Println(string(bs))
39 | }
40 |
--------------------------------------------------------------------------------
/example/bucket/getReplication.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 | "os"
8 |
9 | "net/http"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func main() {
16 | u, _ := url.Parse("https://alanbj-1251668577.cos.ap-beijing.myqcloud.com")
17 | b := &cos.BaseURL{
18 | BucketURL: u,
19 | }
20 | c := cos.NewClient(b, &http.Client{
21 | Transport: &cos.AuthorizationTransport{
22 | SecretID: os.Getenv("SECRETID"),
23 | SecretKey: os.Getenv("SECRETKEY"),
24 | Transport: &debug.DebugRequestTransport{
25 | RequestHeader: true,
26 | RequestBody: true,
27 | ResponseHeader: true,
28 | ResponseBody: true,
29 | },
30 | },
31 | })
32 |
33 | v, _, err := c.Bucket.GetBucketReplication(context.Background())
34 | if err != nil {
35 | panic(err)
36 | }
37 | for _, r := range v.Rule {
38 |
39 | fmt.Printf("%s, %s\n", r.Status, r.ID)
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/example/bucket/getTagging.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 | "os"
8 |
9 | "net/http"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func main() {
16 | u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
17 | b := &cos.BaseURL{
18 | BucketURL: u,
19 | }
20 | c := cos.NewClient(b, &http.Client{
21 | Transport: &cos.AuthorizationTransport{
22 | SecretID: os.Getenv("SECRETID"),
23 | SecretKey: os.Getenv("SECRETKEY"),
24 | Transport: &debug.DebugRequestTransport{
25 | RequestHeader: true,
26 | RequestBody: true,
27 | ResponseHeader: true,
28 | ResponseBody: true,
29 | },
30 | },
31 | })
32 |
33 | v, _, err := c.Bucket.GetTagging(context.Background())
34 | if err != nil {
35 | panic(err)
36 | }
37 | for _, t := range v.TagSet {
38 | fmt.Printf("%s: %s\n", t.Key, t.Value)
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/example/bucket/getVersioning.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 | "os"
8 |
9 | "net/http"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func main() {
16 | u, _ := url.Parse("https://alanbj-1251668577.cos.ap-beijing.myqcloud.com")
17 | b := &cos.BaseURL{
18 | BucketURL: u,
19 | }
20 | c := cos.NewClient(b, &http.Client{
21 | Transport: &cos.AuthorizationTransport{
22 | SecretID: os.Getenv("SECRETID"),
23 | SecretKey: os.Getenv("SECRETKEY"),
24 | Transport: &debug.DebugRequestTransport{
25 | RequestHeader: true,
26 | RequestBody: true,
27 | ResponseHeader: true,
28 | ResponseBody: true,
29 | },
30 | },
31 | })
32 |
33 | v, _, err := c.Bucket.GetVersioning(context.Background())
34 | if err != nil {
35 | panic(err)
36 | }
37 |
38 | fmt.Printf("status is %s \n", v.Status)
39 | }
40 |
--------------------------------------------------------------------------------
/example/bucket/getWebsite.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/http"
6 | "net/url"
7 | "os"
8 |
9 | "github.com/tencentyun/cos-go-sdk-v5"
10 | "github.com/tencentyun/cos-go-sdk-v5/debug"
11 | )
12 |
13 | func main() {
14 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
15 | b := &cos.BaseURL{
16 | BucketURL: u,
17 | }
18 | c := cos.NewClient(b, &http.Client{
19 | Transport: &cos.AuthorizationTransport{
20 | SecretID: os.Getenv("SECRETID"),
21 | SecretKey: os.Getenv("SECRETKEY"),
22 | Transport: &debug.DebugRequestTransport{
23 | RequestHeader: true,
24 | RequestBody: true,
25 | ResponseHeader: true,
26 | ResponseBody: true,
27 | },
28 | },
29 | })
30 |
31 | _, _, err := c.Bucket.GetWebsite(context.Background())
32 | if err != nil {
33 | panic(err)
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/example/bucket/head.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 | "os"
8 |
9 | "net/http"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func main() {
16 | u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
17 | b := &cos.BaseURL{
18 | BucketURL: u,
19 | }
20 | c := cos.NewClient(b, &http.Client{
21 | Transport: &cos.AuthorizationTransport{
22 | SecretID: os.Getenv("SECRETID"),
23 | SecretKey: os.Getenv("SECRETKEY"),
24 | Transport: &debug.DebugRequestTransport{
25 | RequestHeader: true,
26 | RequestBody: true,
27 | ResponseHeader: true,
28 | ResponseBody: true,
29 | },
30 | },
31 | })
32 |
33 | resp, err := c.Bucket.Head(context.Background())
34 | if err != nil {
35 | panic(err)
36 | }
37 | fmt.Println(resp.Status)
38 | }
39 |
--------------------------------------------------------------------------------
/example/bucket/intelligenttiering.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func logStatus(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("Code: %v\n", e.Code)
23 | fmt.Printf("Message: %v\n", e.Message)
24 | fmt.Printf("Resource: %v\n", e.Resource)
25 | fmt.Printf("RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Println(err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
35 | b := &cos.BaseURL{
36 | BucketURL: u,
37 | }
38 | c := cos.NewClient(b, &http.Client{
39 | Transport: &cos.AuthorizationTransport{
40 | SecretID: os.Getenv("SECRETID"),
41 | SecretKey: os.Getenv("SECRETKEY"),
42 | Transport: &debug.DebugRequestTransport{
43 | RequestHeader: true,
44 | RequestBody: false,
45 | ResponseHeader: true,
46 | ResponseBody: false,
47 | },
48 | },
49 | })
50 |
51 | opt := &cos.BucketPutIntelligentTieringOptions{
52 | Status: "Enabled",
53 | Transition: &cos.BucketIntelligentTieringTransition{
54 | Days: 30,
55 | },
56 | }
57 | _, err := c.Bucket.PutIntelligentTiering(context.Background(), opt)
58 | logStatus(err)
59 | res, _, err := c.Bucket.GetIntelligentTiering(context.Background())
60 | logStatus(err)
61 | fmt.Printf("%+v\n", res)
62 | fmt.Printf("%+v\n", res.Status)
63 | fmt.Printf("%+v\n", res.Transition.Days)
64 | }
65 |
--------------------------------------------------------------------------------
/example/bucket/intelligenttieringv2.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func logStatus(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("Code: %v\n", e.Code)
23 | fmt.Printf("Message: %v\n", e.Message)
24 | fmt.Printf("Resource: %v\n", e.Resource)
25 | fmt.Printf("RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Println(err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
35 | b := &cos.BaseURL{
36 | BucketURL: u,
37 | }
38 | c := cos.NewClient(b, &http.Client{
39 | Transport: &cos.AuthorizationTransport{
40 | SecretID: os.Getenv("SECRETID"),
41 | SecretKey: os.Getenv("SECRETKEY"),
42 | Transport: &debug.DebugRequestTransport{
43 | RequestHeader: true,
44 | RequestBody: true,
45 | ResponseHeader: true,
46 | ResponseBody: true,
47 | },
48 | },
49 | })
50 | id := "test"
51 | opt := &cos.BucketPutIntelligentTieringOptions{
52 | Id: id,
53 | Status: "Enabled",
54 | Tiering: []*cos.BucketIntelligentTieringTransition{
55 | {
56 | Days: 91,
57 | AccessTier: "ARCHIVE_ACCESS",
58 | },
59 | },
60 | Filter: &cos.BucketIntelligentTieringFilter{
61 | And: &cos.BucketIntelligentTieringFilterAnd{
62 | Prefix: "test",
63 | Tag: []*cos.BucketTaggingTag{
64 | {
65 | Key: "key",
66 | Value: "value",
67 | },
68 | },
69 | },
70 | },
71 | }
72 | _, err := c.Bucket.PutIntelligentTieringV2(context.Background(), opt)
73 | logStatus(err)
74 |
75 | res, _, err := c.Bucket.GetIntelligentTieringV2(context.Background(), id)
76 | logStatus(err)
77 | fmt.Printf("%+v\n", res)
78 |
79 | r, _, err := c.Bucket.ListIntelligentTiering(context.Background())
80 | logStatus(err)
81 | fmt.Printf("%+v\n", r)
82 |
83 | _, err = c.Bucket.DeleteIntelligentTiering(context.Background(), id)
84 | logStatus(err)
85 | }
86 |
--------------------------------------------------------------------------------
/example/bucket/is_exist.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 | "os"
8 |
9 | "net/http"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func main() {
16 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
17 | b := &cos.BaseURL{
18 | BucketURL: u,
19 | }
20 | c := cos.NewClient(b, &http.Client{
21 | Transport: &cos.AuthorizationTransport{
22 | SecretID: os.Getenv("SECRETID"),
23 | SecretKey: os.Getenv("SECRETKEY"),
24 | Transport: &debug.DebugRequestTransport{
25 | RequestHeader: true,
26 | RequestBody: true,
27 | ResponseHeader: true,
28 | ResponseBody: true,
29 | },
30 | },
31 | })
32 |
33 | ok, err := c.Bucket.IsExist(context.Background())
34 | if err == nil && ok {
35 | fmt.Printf("bucket exists\n")
36 | } else if err != nil {
37 | fmt.Printf("head bucket failed: %v\n", err)
38 | } else {
39 | fmt.Printf("bucket does not exist\n")
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/example/bucket/listMultipartUploads.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 | "os"
8 |
9 | "net/http"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func main() {
16 | u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
17 | b := &cos.BaseURL{
18 | BucketURL: u,
19 | }
20 | c := cos.NewClient(b, &http.Client{
21 | Transport: &cos.AuthorizationTransport{
22 | SecretID: os.Getenv("SECRETID"),
23 | SecretKey: os.Getenv("SECRETKEY"),
24 | Transport: &debug.DebugRequestTransport{
25 | RequestHeader: true,
26 | RequestBody: true,
27 | ResponseHeader: true,
28 | ResponseBody: true,
29 | },
30 | },
31 | })
32 |
33 | opt := &cos.ListMultipartUploadsOptions{
34 | Prefix: "t",
35 | }
36 | v, _, err := c.Bucket.ListMultipartUploads(context.Background(), opt)
37 | if err != nil {
38 | panic(err)
39 | }
40 | for _, p := range v.Uploads {
41 | fmt.Printf("%s\n", p.Key)
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/example/bucket/object_lock.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func logStatus(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("Code: %v\n", e.Code)
23 | fmt.Printf("Message: %v\n", e.Message)
24 | fmt.Printf("Resource: %v\n", e.Resource)
25 | fmt.Printf("RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Println(err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
35 | b := &cos.BaseURL{
36 | BucketURL: u,
37 | }
38 | c := cos.NewClient(b, &http.Client{
39 | Transport: &cos.AuthorizationTransport{
40 | SecretID: os.Getenv("SECRETID"),
41 | SecretKey: os.Getenv("SECRETKEY"),
42 | Transport: &debug.DebugRequestTransport{
43 | RequestHeader: true,
44 | RequestBody: true,
45 | ResponseHeader: true,
46 | ResponseBody: true,
47 | },
48 | },
49 | })
50 | opt := &cos.BucketPutObjectLockOptions{
51 | ObjectLockEnabled: "Enabled",
52 | Rule: &cos.ObjectLockRule{
53 | Days: 1,
54 | },
55 | }
56 | _, err := c.Bucket.PutObjectLockConfiguration(context.Background(), opt)
57 | logStatus(err)
58 |
59 | res, _, err := c.Bucket.GetObjectLockConfiguration(context.Background())
60 | logStatus(err)
61 | fmt.Printf("%+v\n", res)
62 |
63 | ropt := &cos.ObjectPutRetentionOptions{
64 | RetainUntilDate: "2022-12-10T08:34:48.000Z",
65 | Mode: "COMPLIANCE",
66 | }
67 | _, err = c.Object.PutRetention(context.Background(), "test", ropt)
68 | logStatus(err)
69 |
70 | r, _, err := c.Object.GetRetention(context.Background(), "test", nil)
71 | logStatus(err)
72 | fmt.Printf("%+v\n", r)
73 | }
74 |
--------------------------------------------------------------------------------
/example/bucket/put.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/url"
6 | "os"
7 |
8 | "net/http"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func main() {
15 | u, _ := url.Parse("https://testdelete-1253846586.cos.ap-guangzhou.myqcloud.com")
16 | b := &cos.BaseURL{
17 | BucketURL: u,
18 | }
19 | c := cos.NewClient(b, &http.Client{
20 | Transport: &cos.AuthorizationTransport{
21 | SecretID: os.Getenv("SECRETID"),
22 | SecretKey: os.Getenv("SECRETKEY"),
23 | Transport: &debug.DebugRequestTransport{
24 | RequestHeader: true,
25 | RequestBody: true,
26 | ResponseHeader: true,
27 | ResponseBody: true,
28 | },
29 | },
30 | })
31 |
32 | //opt := &cos.BucketPutOptions{
33 | // XCosACL: "public-read",
34 | //}
35 | _, err := c.Bucket.Put(context.Background(), nil)
36 | if err != nil {
37 | panic(err)
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/example/bucket/putACL.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/url"
6 | "os"
7 |
8 | "net/http"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func main() {
15 | u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
16 | b := &cos.BaseURL{
17 | BucketURL: u,
18 | }
19 | c := cos.NewClient(b, &http.Client{
20 | Transport: &cos.AuthorizationTransport{
21 | SecretID: os.Getenv("SECRETID"),
22 | SecretKey: os.Getenv("SECRETKEY"),
23 | Transport: &debug.DebugRequestTransport{
24 | RequestHeader: true,
25 | RequestBody: true,
26 | ResponseHeader: true,
27 | ResponseBody: true,
28 | },
29 | },
30 | })
31 |
32 | // with header
33 | opt := &cos.BucketPutACLOptions{
34 | Header: &cos.ACLHeaderOptions{
35 | XCosACL: "private",
36 | },
37 | }
38 | _, err := c.Bucket.PutACL(context.Background(), opt)
39 | if err != nil {
40 | panic(err)
41 | }
42 |
43 | // with body
44 | opt = &cos.BucketPutACLOptions{
45 | Body: &cos.ACLXml{
46 | Owner: &cos.Owner{
47 | ID: "qcs::cam::uin/100000760461:uin/100000760461",
48 | },
49 | AccessControlList: []cos.ACLGrant{
50 | {
51 | Grantee: &cos.ACLGrantee{
52 | Type: "RootAccount",
53 | ID: "qcs::cam::uin/100000760461:uin/100000760461",
54 | },
55 |
56 | Permission: "FULL_CONTROL",
57 | },
58 | },
59 | },
60 | }
61 | _, err = c.Bucket.PutACL(context.Background(), opt)
62 | if err != nil {
63 | panic(err)
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/example/bucket/putCORS.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/url"
6 | "os"
7 |
8 | "net/http"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func main() {
15 | u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
16 | b := &cos.BaseURL{
17 | BucketURL: u,
18 | }
19 | c := cos.NewClient(b, &http.Client{
20 | Transport: &cos.AuthorizationTransport{
21 | SecretID: os.Getenv("SECRETID"),
22 | SecretKey: os.Getenv("SECRETKEY"),
23 | Transport: &debug.DebugRequestTransport{
24 | RequestHeader: true,
25 | RequestBody: true,
26 | ResponseHeader: true,
27 | ResponseBody: true,
28 | },
29 | },
30 | })
31 |
32 | opt := &cos.BucketPutCORSOptions{
33 | Rules: []cos.BucketCORSRule{
34 | {
35 | AllowedOrigins: []string{"http://www.qq.com"},
36 | AllowedMethods: []string{"PUT", "GET"},
37 | AllowedHeaders: []string{"x-cos-meta-test", "x-cos-xx"},
38 | MaxAgeSeconds: 500,
39 | ExposeHeaders: []string{"x-cos-meta-test1"},
40 | },
41 | {
42 | ID: "1234",
43 | AllowedOrigins: []string{"http://www.baidu.com", "twitter.com"},
44 | AllowedMethods: []string{"PUT", "GET"},
45 | MaxAgeSeconds: 500,
46 | },
47 | },
48 | }
49 | _, err := c.Bucket.PutCORS(context.Background(), opt)
50 | if err != nil {
51 | panic(err)
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/example/bucket/putLifecycle.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/url"
6 | "os"
7 |
8 | "net/http"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func main() {
15 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
16 | b := &cos.BaseURL{
17 | BucketURL: u,
18 | }
19 | c := cos.NewClient(b, &http.Client{
20 | Transport: &cos.AuthorizationTransport{
21 | SecretID: os.Getenv("SECRETID"),
22 | SecretKey: os.Getenv("SECRETKEY"),
23 | Transport: &debug.DebugRequestTransport{
24 | RequestHeader: true,
25 | RequestBody: true,
26 | ResponseHeader: true,
27 | ResponseBody: true,
28 | },
29 | },
30 | })
31 |
32 | lc := &cos.BucketPutLifecycleOptions{
33 | Rules: []cos.BucketLifecycleRule{
34 | {
35 | ID: "1234",
36 | Filter: &cos.BucketLifecycleFilter{Prefix: "test"},
37 | Status: "Enabled",
38 | Transition: []cos.BucketLifecycleTransition{
39 | {
40 | Days: 30,
41 | StorageClass: "STANDARD_IA",
42 | },
43 | {
44 | Days: 90,
45 | StorageClass: "ARCHIVE",
46 | },
47 | },
48 | Expiration: &cos.BucketLifecycleExpiration{
49 | Days: 360,
50 | },
51 | NoncurrentVersionExpiration: &cos.BucketLifecycleNoncurrentVersion{
52 | NoncurrentDays: 360,
53 | },
54 | NoncurrentVersionTransition: []cos.BucketLifecycleNoncurrentVersion{
55 | {
56 | NoncurrentDays: 90,
57 | StorageClass: "ARCHIVE",
58 | },
59 | {
60 | NoncurrentDays: 180,
61 | StorageClass: "DEEP_ARCHIVE",
62 | },
63 | },
64 | AbortIncompleteMultipartUpload: &cos.BucketLifecycleAbortIncompleteMultipartUpload{
65 | DaysAfterInitiation: 90,
66 | },
67 | },
68 | },
69 | }
70 | _, err := c.Bucket.PutLifecycle(context.Background(), lc)
71 | if err != nil {
72 | panic(err)
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/example/bucket/putPolicy.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/http"
6 | "net/url"
7 | "os"
8 |
9 | "github.com/tencentyun/cos-go-sdk-v5"
10 | "github.com/tencentyun/cos-go-sdk-v5/debug"
11 | )
12 |
13 | func main() {
14 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
15 | b := &cos.BaseURL{
16 | BucketURL: u,
17 | }
18 | c := cos.NewClient(b, &http.Client{
19 | Transport: &cos.AuthorizationTransport{
20 | SecretID: os.Getenv("SECRETID"),
21 | SecretKey: os.Getenv("SECRETKEY"),
22 | Transport: &debug.DebugRequestTransport{
23 | RequestHeader: true,
24 | RequestBody: true,
25 | ResponseHeader: true,
26 | ResponseBody: true,
27 | },
28 | },
29 | })
30 |
31 | opt := &cos.BucketPutPolicyOptions{
32 | Version: "2.0",
33 | Statement: []cos.BucketStatement{
34 | {
35 | Principal: map[string][]string{
36 | "qcs": []string{
37 | "qcs::cam::uin/100000000001:uin/100000000011", //替换成您想授予权限的账户uin
38 | },
39 | },
40 | Action: []string{
41 | "name/cos:GetObject",
42 | },
43 | Effect: "allow",
44 | Resource: []string{
45 | //这里改成允许的路径前缀,可以根据自己网站的用户登录态判断允许上传的具体路径,例子: a.jpg 或者 a/* 或者 * (使用通配符*存在重大安全风险, 请谨慎评估使用)
46 | "qcs::cos:ap-guangzhou:uid/1259654469:test-1259654469/exampleobject",
47 | },
48 | Condition: map[string]map[string]interface{}{
49 | "ip_not_equal": map[string]interface{}{
50 | "qcs:ip": []string{
51 | "",
52 | },
53 | },
54 | },
55 | },
56 | },
57 | }
58 |
59 | _, err := c.Bucket.PutPolicy(context.Background(), opt)
60 | if err != nil {
61 | panic(err)
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/example/bucket/putReplication.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/url"
6 | "os"
7 |
8 | "net/http"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func main() {
15 | u, _ := url.Parse("https://alanbj-1251668577.cos.ap-beijing.myqcloud.com")
16 | b := &cos.BaseURL{
17 | BucketURL: u,
18 | }
19 | c := cos.NewClient(b, &http.Client{
20 | Transport: &cos.AuthorizationTransport{
21 | SecretID: os.Getenv("SECRETID"),
22 | SecretKey: os.Getenv("SECRETKEY"),
23 | Transport: &debug.DebugRequestTransport{
24 | RequestHeader: true,
25 | RequestBody: true,
26 | ResponseHeader: true,
27 | ResponseBody: true,
28 | },
29 | },
30 | })
31 |
32 | opt := &cos.PutBucketReplicationOptions{
33 | // qcs::cam::uin/[UIN]:uin/[Subaccount]
34 | Role: "qcs::cam::uin/2779643970:uin/2779643970",
35 | Rule: []cos.BucketReplicationRule{
36 | {
37 | ID: "1",
38 | // Enabled or Disabled
39 | Status: "Enabled",
40 | Destination: &cos.ReplicationDestination{
41 | // qcs::cos:[Region]::[Bucketname-Appid]
42 | Bucket: "qcs::cos:ap-guangzhou::alangz-1251668577",
43 | },
44 | },
45 | },
46 | }
47 | _, err := c.Bucket.PutBucketReplication(context.Background(), opt)
48 | if err != nil {
49 | panic(err)
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/example/bucket/putTagging.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/url"
6 | "os"
7 | "time"
8 |
9 | "net/http"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func main() {
16 | u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
17 | b := &cos.BaseURL{
18 | BucketURL: u,
19 | }
20 | c := cos.NewClient(b, &http.Client{
21 | Transport: &cos.AuthorizationTransport{
22 | SecretID: os.Getenv("SECRETID"),
23 | SecretKey: os.Getenv("SECRETKEY"),
24 | Transport: &debug.DebugRequestTransport{
25 | RequestHeader: true,
26 | RequestBody: true,
27 | ResponseHeader: true,
28 | ResponseBody: true,
29 | },
30 | },
31 | })
32 | startTime := time.Now()
33 |
34 | tg := &cos.BucketPutTaggingOptions{
35 | TagSet: []cos.BucketTaggingTag{
36 | {
37 | Key: "test_k2",
38 | Value: "test_v2",
39 | },
40 | {
41 | Key: "test_k3",
42 | Value: "test_v3",
43 | },
44 | {
45 | Key: startTime.Format("02_Jan_06_15_04_MST"),
46 | Value: "test_time",
47 | },
48 | },
49 | }
50 | _, err := c.Bucket.PutTagging(context.Background(), tg)
51 | if err != nil {
52 | panic(err)
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/example/bucket/putVersioning.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/url"
6 | "os"
7 |
8 | "net/http"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func main() {
15 | u, _ := url.Parse("https://alanbj-1251668577.cos.ap-beijing.myqcloud.com")
16 | b := &cos.BaseURL{
17 | BucketURL: u,
18 | }
19 | c := cos.NewClient(b, &http.Client{
20 | Transport: &cos.AuthorizationTransport{
21 | SecretID: os.Getenv("SECRETID"),
22 | SecretKey: os.Getenv("SECRETKEY"),
23 | Transport: &debug.DebugRequestTransport{
24 | RequestHeader: true,
25 | RequestBody: true,
26 | ResponseHeader: true,
27 | ResponseBody: true,
28 | },
29 | },
30 | })
31 |
32 | opt := &cos.BucketPutVersionOptions{
33 | // Enabled or Suspended, the versioning once opened can not close.
34 | Status: "Enabled",
35 | }
36 |
37 | _, err := c.Bucket.PutVersioning(context.Background(), opt)
38 | if err != nil {
39 | panic(err)
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/example/bucket/putWebsite.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/http"
6 | "net/url"
7 | "os"
8 |
9 | "github.com/tencentyun/cos-go-sdk-v5"
10 | "github.com/tencentyun/cos-go-sdk-v5/debug"
11 | )
12 |
13 | func main() {
14 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
15 | b := &cos.BaseURL{
16 | BucketURL: u,
17 | }
18 | c := cos.NewClient(b, &http.Client{
19 | Transport: &cos.AuthorizationTransport{
20 | SecretID: os.Getenv("SECRETID"),
21 | SecretKey: os.Getenv("SECRETKEY"),
22 | Transport: &debug.DebugRequestTransport{
23 | RequestHeader: true,
24 | RequestBody: true,
25 | ResponseHeader: true,
26 | ResponseBody: true,
27 | },
28 | },
29 | })
30 |
31 | opt := &cos.BucketPutWebsiteOptions{
32 | Index: "index.html",
33 | Error: &cos.ErrorDocument{"index_backup.html"},
34 | RedirectProtocol: &cos.RedirectRequestsProtocol{"https"},
35 | RoutingRules: &cos.WebsiteRoutingRules{
36 | []cos.WebsiteRoutingRule{
37 | {
38 | ConditionErrorCode: "404",
39 | RedirectProtocol: "https",
40 | RedirectReplaceKey: "404.html",
41 | },
42 | {
43 | ConditionPrefix: "docs/",
44 | RedirectProtocol: "https",
45 | RedirectReplaceKeyPrefix: "documents/",
46 | },
47 | },
48 | },
49 | }
50 |
51 | _, err := c.Bucket.PutWebsite(context.Background(), opt)
52 | if err != nil {
53 | panic(err)
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/example/bucket/referer.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func logStatus(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("Code: %v\n", e.Code)
23 | fmt.Printf("Message: %v\n", e.Message)
24 | fmt.Printf("Resource: %v\n", e.Resource)
25 | fmt.Printf("RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Println(err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
35 | b := &cos.BaseURL{
36 | BucketURL: u,
37 | }
38 | c := cos.NewClient(b, &http.Client{
39 | Transport: &cos.AuthorizationTransport{
40 | SecretID: os.Getenv("SECRETID"),
41 | SecretKey: os.Getenv("SECRETKEY"),
42 | Transport: &debug.DebugRequestTransport{
43 | RequestHeader: true,
44 | RequestBody: true,
45 | ResponseHeader: true,
46 | ResponseBody: true,
47 | },
48 | },
49 | })
50 |
51 | opt := &cos.BucketPutRefererOptions{
52 | Status: "Enabled",
53 | RefererType: "White-List",
54 | DomainList: []string{
55 | "*.qq.com",
56 | "*.qcloud.com",
57 | },
58 | EmptyReferConfiguration: "Allow",
59 | }
60 |
61 | _, err := c.Bucket.PutReferer(context.Background(), opt)
62 | logStatus(err)
63 |
64 | res, _, err := c.Bucket.GetReferer(context.Background())
65 | logStatus(err)
66 | fmt.Printf("%+v\n", res)
67 | }
68 |
--------------------------------------------------------------------------------
/example/object/MultiUpload.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/http"
6 | "net/url"
7 | "os"
8 | "time"
9 |
10 | "fmt"
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func logStatus(err error) {
16 | if err == nil {
17 | return
18 | }
19 | if cos.IsNotFoundError(err) {
20 | // WARN
21 | fmt.Println("WARN: Resource is not existed")
22 | } else if e, ok := cos.IsCOSError(err); ok {
23 | fmt.Printf("ERROR: Code: %v\n", e.Code)
24 | fmt.Printf("ERROR: Message: %v\n", e.Message)
25 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
26 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
27 | // ERROR
28 | } else {
29 | fmt.Printf("ERROR: %v\n", err)
30 | // ERROR
31 | }
32 | }
33 |
34 | func main() {
35 | // 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket
36 | // 替换为用户的 region,存储桶region可以在COS控制台“存储桶概览”查看 https://console.cloud.tencent.com/ ,关于地域的详情见 https://cloud.tencent.com/document/product/436/6224 。
37 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
38 | b := &cos.BaseURL{BucketURL: u}
39 | c := cos.NewClient(b, &http.Client{
40 | Transport: &cos.AuthorizationTransport{
41 | // 通过环境变量获取密钥
42 | // 环境变量 SECRETID 表示用户的 SecretId,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
43 | SecretID: os.Getenv("SECRETID"),
44 | // 环境变量 SECRETKEY 表示用户的 SecretKey,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
45 | SecretKey: os.Getenv("SECRETKEY"),
46 | // Debug 模式,把对应 请求头部、请求内容、响应头部、响应内容 输出到标准输出
47 | Transport: &debug.DebugRequestTransport{
48 | RequestHeader: false,
49 | RequestBody: false,
50 | ResponseHeader: false,
51 | ResponseBody: false,
52 | },
53 | },
54 | })
55 |
56 | opt := &cos.MultiUploadOptions{
57 | OptIni: nil,
58 | PartSize: 1,
59 | }
60 | v, _, err := c.Object.MultiUpload(
61 | context.Background(), "test/gomulput1G", "./test1G", opt,
62 | )
63 | logStatus(err)
64 | fmt.Println(v)
65 | }
66 |
--------------------------------------------------------------------------------
/example/object/abortMultipartUpload.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 | "os"
8 |
9 | "net/http"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func logStatus(err error) {
16 | if err == nil {
17 | return
18 | }
19 | if cos.IsNotFoundError(err) {
20 | // WARN
21 | fmt.Println("WARN: Resource is not existed")
22 | } else if e, ok := cos.IsCOSError(err); ok {
23 | fmt.Printf("ERROR: Code: %v\n", e.Code)
24 | fmt.Printf("ERROR: Message: %v\n", e.Message)
25 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
26 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
27 | // ERROR
28 | } else {
29 | fmt.Printf("ERROR: %v\n", err)
30 | // ERROR
31 | }
32 | }
33 |
34 | func main() {
35 | // 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket
36 | // 替换为用户的 region,存储桶region可以在COS控制台“存储桶概览”查看 https://console.cloud.tencent.com/ ,关于地域的详情见 https://cloud.tencent.com/document/product/436/6224 。
37 | u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
38 | b := &cos.BaseURL{BucketURL: u}
39 | c := cos.NewClient(b, &http.Client{
40 | Transport: &cos.AuthorizationTransport{
41 | // 通过环境变量获取密钥
42 | // 环境变量 SECRETID 表示用户的 SecretId,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
43 | SecretID: os.Getenv("SECRETID"),
44 | // 环境变量 SECRETKEY 表示用户的 SecretKey,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
45 | SecretKey: os.Getenv("SECRETKEY"),
46 | // Debug 模式,把对应 请求头部、请求内容、响应头部、响应内容 输出到标准输出
47 | Transport: &debug.DebugRequestTransport{
48 | RequestHeader: true,
49 | RequestBody: false,
50 | ResponseHeader: true,
51 | ResponseBody: true,
52 | },
53 | },
54 | })
55 |
56 | name := "test_multipart.txt"
57 | v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil)
58 | logStatus(err)
59 | fmt.Printf("%s\n", v.UploadID)
60 |
61 | resp, err := c.Object.AbortMultipartUpload(context.Background(), name, v.UploadID)
62 | logStatus(err)
63 | fmt.Printf("%s\n", resp.Status)
64 | }
65 |
--------------------------------------------------------------------------------
/example/object/delete.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 | "os"
8 |
9 | "net/http"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func logStatus(err error) {
16 | if err == nil {
17 | return
18 | }
19 | if cos.IsNotFoundError(err) {
20 | // WARN
21 | fmt.Println("WARN: Resource is not existed")
22 | } else if e, ok := cos.IsCOSError(err); ok {
23 | fmt.Printf("ERROR: Code: %v\n", e.Code)
24 | fmt.Printf("ERROR: Message: %v\n", e.Message)
25 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
26 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
27 | // ERROR
28 | } else {
29 | fmt.Printf("ERROR: %v\n", err)
30 | // ERROR
31 | }
32 | }
33 |
34 | func main() {
35 | // 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket
36 | // 替换为用户的 region,存储桶region可以在COS控制台“存储桶概览”查看 https://console.cloud.tencent.com/ ,关于地域的详情见 https://cloud.tencent.com/document/product/436/6224 。
37 | u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
38 | b := &cos.BaseURL{BucketURL: u}
39 | c := cos.NewClient(b, &http.Client{
40 | Transport: &cos.AuthorizationTransport{
41 | // 通过环境变量获取密钥
42 | // 环境变量 SECRETID 表示用户的 SecretId,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
43 | SecretID: os.Getenv("SECRETID"),
44 | // 环境变量 SECRETKEY 表示用户的 SecretKey,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
45 | SecretKey: os.Getenv("SECRETKEY"),
46 | // Debug 模式,把对应 请求头部、请求内容、响应头部、响应内容 输出到标准输出
47 | Transport: &debug.DebugRequestTransport{
48 | RequestHeader: true,
49 | RequestBody: true,
50 | ResponseHeader: true,
51 | ResponseBody: true,
52 | },
53 | },
54 | })
55 |
56 | name := "test/objectPut.go"
57 |
58 | _, err := c.Object.Delete(context.Background(), name, nil)
59 | logStatus(err)
60 | }
61 |
--------------------------------------------------------------------------------
/example/object/download.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/http"
6 | "net/url"
7 | "os"
8 |
9 | "fmt"
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func logStatus(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("WARN: Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("ERROR: Code: %v\n", e.Code)
23 | fmt.Printf("ERROR: Message: %v\n", e.Message)
24 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Printf("ERROR: %v\n", err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | // 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket
35 | // 替换为用户的 region,存储桶region可以在COS控制台“存储桶概览”查看 https://console.cloud.tencent.com/ ,关于地域的详情见 https://cloud.tencent.com/document/product/436/6224 。
36 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
37 | b := &cos.BaseURL{BucketURL: u}
38 | c := cos.NewClient(b, &http.Client{
39 | Transport: &cos.AuthorizationTransport{
40 | // 通过环境变量获取密钥
41 | // 环境变量 SECRETID 表示用户的 SecretId,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
42 | SecretID: os.Getenv("SECRETID"),
43 | // 环境变量 SECRETKEY 表示用户的 SecretKey,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
44 | SecretKey: os.Getenv("SECRETKEY"),
45 | // Debug 模式,把对应 请求头部、请求内容、响应头部、响应内容 输出到标准输出
46 | Transport: &debug.DebugRequestTransport{
47 | RequestHeader: false,
48 | RequestBody: false,
49 | ResponseHeader: false,
50 | ResponseBody: false,
51 | },
52 | },
53 | })
54 |
55 | opt := &cos.MultiDownloadOptions{
56 | ThreadPoolSize: 5,
57 | }
58 | resp, err := c.Object.Download(
59 | context.Background(), "test", "./test1G", opt,
60 | )
61 | logStatus(err)
62 | fmt.Printf("done, %v\n", resp.Header)
63 | }
64 |
--------------------------------------------------------------------------------
/example/object/fetch_task.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "github.com/tencentyun/cos-go-sdk-v5"
7 | "github.com/tencentyun/cos-go-sdk-v5/debug"
8 | "net/http"
9 | "net/url"
10 | "os"
11 | "time"
12 | )
13 |
14 | func logStatus(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("WARN: Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("ERROR: Code: %v\n", e.Code)
23 | fmt.Printf("ERROR: Message: %v\n", e.Message)
24 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Printf("ERROR: %v\n", err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | bucket := "test-1259654469"
35 | bu, _ := url.Parse("https://" + bucket + ".cos.ap-guangzhou.myqcloud.com")
36 | u, _ := url.Parse("http://ap-guangzhou.migration.myqcloud.com")
37 | b := &cos.BaseURL{BucketURL: bu, FetchURL: u}
38 | c := cos.NewClient(b, &http.Client{
39 | Transport: &cos.AuthorizationTransport{
40 | SecretID: os.Getenv("SECRETID"),
41 | SecretKey: os.Getenv("SECRETKEY"),
42 | Transport: &debug.DebugRequestTransport{
43 | RequestHeader: true,
44 | RequestBody: true,
45 | ResponseHeader: true,
46 | ResponseBody: true,
47 | },
48 | },
49 | })
50 | opt := &cos.PutFetchTaskOptions{
51 | // 源站资源地址,需要url encode,不支持https
52 | Url: "http://" + bucket + ".cos.ap-guangzhou.myqcloud.com/exampleobject",
53 | // COS中的文件路径,不需要url encode
54 | Key: "exampleobject",
55 | /*
56 | MD5: 文件 MD5 校验值, 可选
57 | SuccessCallbackUrl: 回源拉取成功的回调地址,可选
58 | FailureCallbackUrl: 回源拉取失败的回调地址,可选
59 | */
60 | }
61 |
62 | // 发起离线回源
63 | res, _, err := c.Object.PutFetchTask(context.Background(), bucket, opt)
64 | logStatus(err)
65 | fmt.Printf("res: %+v\n", res)
66 |
67 | time.Sleep(time.Second * 3)
68 |
69 | // 查询回源进度
70 | rs, _, err := c.Object.GetFetchTask(context.Background(), bucket, res.Data.TaskId)
71 | logStatus(err)
72 | fmt.Printf("res: %+v\n", rs)
73 | }
74 |
--------------------------------------------------------------------------------
/example/object/getACL.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 | "os"
8 |
9 | "net/http"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func logStatus(err error) {
16 | if err == nil {
17 | return
18 | }
19 | if cos.IsNotFoundError(err) {
20 | // WARN
21 | fmt.Println("WARN: Resource is not existed")
22 | } else if e, ok := cos.IsCOSError(err); ok {
23 | fmt.Printf("ERROR: Code: %v\n", e.Code)
24 | fmt.Printf("ERROR: Message: %v\n", e.Message)
25 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
26 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
27 | // ERROR
28 | } else {
29 | fmt.Printf("ERROR: %v\n", err)
30 | // ERROR
31 | }
32 | }
33 |
34 | func main() {
35 | // 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket
36 | // 替换为用户的 region,存储桶region可以在COS控制台“存储桶概览”查看 https://console.cloud.tencent.com/ ,关于地域的详情见 https://cloud.tencent.com/document/product/436/6224 。
37 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
38 | b := &cos.BaseURL{BucketURL: u}
39 | c := cos.NewClient(b, &http.Client{
40 | Transport: &cos.AuthorizationTransport{
41 | // 通过环境变量获取密钥
42 | // 环境变量 SECRETID 表示用户的 SecretId,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
43 | SecretID: os.Getenv("SECRETID"),
44 | // 环境变量 SECRETKEY 表示用户的 SecretKey,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
45 | SecretKey: os.Getenv("SECRETKEY"),
46 | // Debug 模式,把对应 请求头部、请求内容、响应头部、响应内容 输出到标准输出
47 | Transport: &debug.DebugRequestTransport{
48 | RequestHeader: true,
49 | RequestBody: true,
50 | ResponseHeader: true,
51 | ResponseBody: true,
52 | },
53 | },
54 | })
55 |
56 | name := "test/hello.txt"
57 | v, _, err := c.Object.GetACL(context.Background(), name)
58 | logStatus(err)
59 | for _, a := range v.AccessControlList {
60 | fmt.Printf("%s, %s, %s\n", a.Grantee.Type, a.Grantee.ID, a.Permission)
61 | }
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/example/object/getAnonymous.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 | "strings"
8 |
9 | "io/ioutil"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | )
13 |
14 | func logStatus(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("WARN: Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("ERROR: Code: %v\n", e.Code)
23 | fmt.Printf("ERROR: Message: %v\n", e.Message)
24 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Printf("ERROR: %v\n", err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func upload(c *cos.Client, name string) {
34 | f := strings.NewReader("test")
35 | f = strings.NewReader("test xxx")
36 | opt := &cos.ObjectPutOptions{
37 | ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
38 | ContentType: "text/html",
39 | },
40 | ACLHeaderOptions: &cos.ACLHeaderOptions{
41 | XCosACL: "public-read",
42 | },
43 | }
44 | c.Object.Put(context.Background(), name, f, opt)
45 | return
46 | }
47 |
48 | func main() {
49 | u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
50 | b := &cos.BaseURL{BucketURL: u}
51 | c := cos.NewClient(b, nil)
52 |
53 | name := "test/anonymous_get.go"
54 | upload(c, name)
55 |
56 | resp, err := c.Object.Get(context.Background(), name, nil)
57 | logStatus(err)
58 | bs, _ := ioutil.ReadAll(resp.Body)
59 | defer resp.Body.Close()
60 | fmt.Printf("%s\n", string(bs))
61 | }
62 |
--------------------------------------------------------------------------------
/example/object/getByPresignedURL.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 | "time"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func logStatus(err error) {
16 | if err == nil {
17 | return
18 | }
19 | if cos.IsNotFoundError(err) {
20 | // WARN
21 | fmt.Println("WARN: Resource is not existed")
22 | } else if e, ok := cos.IsCOSError(err); ok {
23 | fmt.Printf("ERROR: Code: %v\n", e.Code)
24 | fmt.Printf("ERROR: Message: %v\n", e.Message)
25 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
26 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
27 | // ERROR
28 | } else {
29 | fmt.Printf("ERROR: %v\n", err)
30 | // ERROR
31 | }
32 | }
33 |
34 | func main() {
35 | // 通过环境变量获取密钥
36 | // 环境变量 SECRETID 表示用户的 SecretId,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
37 | ak := os.Getenv("SECRETID")
38 | // 环境变量 SECRETKEY 表示用户的 SecretKey,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
39 | sk := os.Getenv("SECRETKEY")
40 | // 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket
41 | // 替换为用户的 region,存储桶region可以在COS控制台“存储桶概览”查看 https://console.cloud.tencent.com/ ,关于地域的详情见 https://cloud.tencent.com/document/product/436/6224 。
42 | u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
43 | b := &cos.BaseURL{BucketURL: u}
44 | c := cos.NewClient(b, &http.Client{
45 | Transport: &cos.AuthorizationTransport{
46 | SecretID: ak,
47 | SecretKey: sk,
48 | Expire: time.Hour,
49 | Transport: &debug.DebugRequestTransport{
50 | RequestHeader: true,
51 | RequestBody: true,
52 | ResponseHeader: true,
53 | ResponseBody: true,
54 | },
55 | },
56 | })
57 |
58 | name := "test"
59 | ctx := context.Background()
60 |
61 | // Get presigned
62 | // http Method需要和实际http请求一致,如PUT请求设置成http.MethodPut,GET请求设置成http.MethodGet
63 | presignedURL, err := c.Object.GetPresignedURL(ctx, http.MethodGet, name, ak, sk, time.Hour, nil)
64 | logStatus(err)
65 | fmt.Println(presignedURL.String())
66 | }
67 |
--------------------------------------------------------------------------------
/example/object/getByPresignedURL2.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "github.com/tencentyun/cos-go-sdk-v5"
7 | "net/http"
8 | "net/url"
9 | "os"
10 | "time"
11 | )
12 |
13 | func main() {
14 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
15 | b := &cos.BaseURL{BucketURL: u}
16 | c := cos.NewClient(b, &http.Client{
17 | Transport: &cos.AuthorizationTransport{
18 | // 通过环境变量获取密钥
19 | // 环境变量 SECRETID 表示用户的 SecretId,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
20 | SecretID: os.Getenv("SECRETID"),
21 | // 环境变量 SECRETKEY 表示用户的 SecretKey,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
22 | SecretKey: os.Getenv("SECRETKEY"),
23 | SessionToken: "", // 请替换成您的临时密钥
24 | },
25 | })
26 |
27 | name := "exampleobject"
28 | ctx := context.Background()
29 |
30 | // 获取预签名
31 | // http Method需要和实际http请求一致,如PUT请求设置成http.MethodPut,GET请求设置成http.MethodGet
32 | presignedURL, err := c.Object.GetPresignedURL2(ctx, http.MethodPut, name, time.Hour, nil)
33 | if err != nil {
34 | fmt.Printf("Error: %v\n", err)
35 | return
36 | }
37 | fmt.Printf("url: %v\n", presignedURL.String())
38 | }
39 |
--------------------------------------------------------------------------------
/example/object/get_object_url.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "fmt"
5 | "net/http"
6 | "net/url"
7 | "os"
8 |
9 | "github.com/tencentyun/cos-go-sdk-v5"
10 | )
11 |
12 | func logStatus(err error) {
13 | if err == nil {
14 | return
15 | }
16 | if cos.IsNotFoundError(err) {
17 | // WARN
18 | fmt.Println("WARN: Resource is not existed")
19 | } else if e, ok := cos.IsCOSError(err); ok {
20 | fmt.Printf("ERROR: Code: %v\n", e.Code)
21 | fmt.Printf("ERROR: Message: %v\n", e.Message)
22 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
23 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
24 | // ERROR
25 | } else {
26 | fmt.Printf("ERROR: %v\n", err)
27 | // ERROR
28 | }
29 | }
30 |
31 | func main() {
32 | // 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket
33 | // 替换为用户的 region,存储桶region可以在COS控制台“存储桶概览”查看 https://console.cloud.tencent.com/ ,关于地域的详情见 https://cloud.tencent.com/document/product/436/6224 。
34 | u, _ := url.Parse("http://test-1259654469.cos.ap-guangzhou.myqcloud.com")
35 | b := &cos.BaseURL{BucketURL: u}
36 | c := cos.NewClient(b, &http.Client{
37 | Transport: &cos.AuthorizationTransport{
38 | // 通过环境变量获取密钥
39 | // 环境变量 SECRETID 表示用户的 SecretId,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
40 | SecretID: os.Getenv("SECRETID"),
41 | // 环境变量 SECRETKEY 表示用户的 SecretKey,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
42 | SecretKey: os.Getenv("SECRETKEY"),
43 | },
44 | })
45 |
46 | name := "exampleobject"
47 | object_url := c.Object.GetObjectURL(name)
48 | fmt.Println(object_url.String())
49 | }
50 |
--------------------------------------------------------------------------------
/example/object/get_signature.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 | "strings"
10 | "time"
11 |
12 | "github.com/tencentyun/cos-go-sdk-v5"
13 | "github.com/tencentyun/cos-go-sdk-v5/debug"
14 | )
15 |
16 | func logStatus(err error) {
17 | if err == nil {
18 | return
19 | }
20 | if cos.IsNotFoundError(err) {
21 | // WARN
22 | fmt.Println("WARN: Resource is not existed")
23 | } else if e, ok := cos.IsCOSError(err); ok {
24 | fmt.Printf("ERROR: Code: %v\n", e.Code)
25 | fmt.Printf("ERROR: Message: %v\n", e.Message)
26 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
27 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
28 | // ERROR
29 | } else {
30 | fmt.Printf("ERROR: %v\n", err)
31 | // ERROR
32 | }
33 | }
34 |
35 | func main() {
36 | ak := os.Getenv("SECRETID")
37 | sk := os.Getenv("SECRETKEY")
38 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
39 | b := &cos.BaseURL{BucketURL: u}
40 | c := cos.NewClient(b, nil)
41 |
42 | name := "中文测试"
43 |
44 | // 把相关header和query签入到签名中
45 | opt := &cos.PresignedURLOptions{
46 | Query: &url.Values{},
47 | Header: &http.Header{},
48 | }
49 | opt.Query.Add("test_key", "中文测试")
50 | opt.Header.Add("x-cos-meta-test", "中文测试")
51 |
52 | // 获取签名
53 | auth := c.Object.GetSignature(context.Background(), http.MethodPut, name, ak, sk, time.Hour, opt)
54 | fmt.Printf("signature: %s\n", auth)
55 |
56 | cli := &http.Client{
57 | Transport: &debug.DebugRequestTransport{
58 | RequestHeader: true,
59 | RequestBody: true,
60 | ResponseHeader: true,
61 | ResponseBody: true,
62 | },
63 | }
64 | // 请求需要和签名保持一致
65 | uristr := fmt.Sprintf("https://%s/%s?%s", u.Host, name, opt.Query.Encode())
66 | req, err := http.NewRequest(http.MethodPut, uristr, strings.NewReader("test"))
67 | req.Header.Add("x-cos-meta-test", "中文测试")
68 | req.Header.Add("Authorization", auth)
69 |
70 | resp, err := cli.Do(req)
71 | logStatus(err)
72 | defer resp.Body.Close()
73 | }
74 |
--------------------------------------------------------------------------------
/example/object/get_with_cos_internal.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "github.com/tencentyun/cos-go-sdk-v5"
7 | "net/http"
8 | "net/url"
9 | "os"
10 | )
11 |
12 | func logStatus(err error) {
13 | if err == nil {
14 | return
15 | }
16 | if cos.IsNotFoundError(err) {
17 | // WARN
18 | fmt.Println("WARN: Resource is not existed")
19 | } else if e, ok := cos.IsCOSError(err); ok {
20 | fmt.Printf("ERROR: Code: %v\n", e.Code)
21 | fmt.Printf("ERROR: Message: %v\n", e.Message)
22 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
23 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
24 | // ERROR
25 | } else {
26 | fmt.Printf("ERROR: %v\n", err)
27 | // ERROR
28 | }
29 | }
30 |
31 | func main() {
32 | // 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket
33 | // 替换为用户的 region,存储桶region可以在COS控制台“存储桶概览”查看 https://console.cloud.tencent.com/ ,关于地域的详情见 https://cloud.tencent.com/document/product/436/6224 。
34 | u, _ := url.Parse("http://test-1259654469.cos-internal.ap-guangzhou.tencentcos.cn")
35 | b := &cos.BaseURL{BucketURL: u}
36 | c := cos.NewClient(b, &http.Client{
37 | Transport: &cos.AuthorizationTransport{
38 | // 通过环境变量获取密钥
39 | // 环境变量 SECRETID 表示用户的 SecretId,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
40 | SecretID: os.Getenv("SECRETID"),
41 | // 环境变量 SECRETKEY 表示用户的 SecretKey,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
42 | SecretKey: os.Getenv("SECRETKEY"),
43 | // 1、不设置 Transport 时:
44 | // 1)对于内部域名,Transport 默认使用 &cos.DNSScatterTransport, cos.DNSScatterTransport 获取DNS响应后,随机获取IP
45 | // 2)对于其他域名,Transport 默认使用 &http.DefaultTransport (go http 库默认 Transport)
46 | // 2、设置 Transport 时,以用户设置为准
47 | // Transport: http.DefaultTransport,
48 | },
49 | })
50 |
51 | name := "exampleobject"
52 | _, err := c.Object.Get(context.Background(), name, nil)
53 | logStatus(err)
54 | }
55 |
--------------------------------------------------------------------------------
/example/object/get_with_credential.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 |
8 | "net/http"
9 | "os"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func logStatus(err error) {
16 | if err == nil {
17 | return
18 | }
19 | if cos.IsNotFoundError(err) {
20 | // WARN
21 | fmt.Println("WARN: Resource is not existed")
22 | } else if e, ok := cos.IsCOSError(err); ok {
23 | fmt.Printf("ERROR: Code: %v\n", e.Code)
24 | fmt.Printf("ERROR: Message: %v\n", e.Message)
25 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
26 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
27 | // ERROR
28 | } else {
29 | fmt.Printf("ERROR: %v\n", err)
30 | // ERROR
31 | }
32 | }
33 |
34 | type Credential struct {
35 | }
36 |
37 | // 需实现 CredentialIface 三个方法
38 | func (c *Credential) GetSecretId() string {
39 | return os.Getenv("SECRETID")
40 | }
41 |
42 | func (c *Credential) GetSecretKey() string {
43 | return os.Getenv("SECRETKEY")
44 | }
45 |
46 | func (c *Credential) GetToken() string {
47 | return ""
48 | }
49 |
50 | func main() {
51 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
52 | b := &cos.BaseURL{BucketURL: u}
53 | c := cos.NewClient(b, &http.Client{
54 | // 使用 CredentialsTransport
55 | Transport: &cos.CredentialTransport{
56 | // 通过 CredentialIface 获取密钥, 需实现 GetSecretKey,GetSecretId,GetToken 方法。
57 | Credential: &Credential{},
58 | Transport: &debug.DebugRequestTransport{
59 | RequestHeader: true,
60 | // Notice when put a large file and set need the request body, might happend out of memory error.
61 | RequestBody: false,
62 | ResponseHeader: true,
63 | ResponseBody: false,
64 | },
65 | },
66 | })
67 |
68 | name := "exampleobject"
69 | _, err := c.Object.Get(context.Background(), name, nil)
70 | logStatus(err)
71 | }
72 |
--------------------------------------------------------------------------------
/example/object/get_with_cvm_role.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 |
8 | "net/http"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func logStatus(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("WARN: Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("ERROR: Code: %v\n", e.Code)
23 | fmt.Printf("ERROR: Message: %v\n", e.Message)
24 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Printf("ERROR: %v\n", err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
35 | b := &cos.BaseURL{BucketURL: u}
36 | c := cos.NewClient(b, &http.Client{
37 | // 使用 CVMCredentialTransport
38 | Transport: &cos.CVMCredentialTransport{
39 | Transport: &debug.DebugRequestTransport{
40 | RequestHeader: true,
41 | // Notice when put a large file and set need the request body, might happend out of memory error.
42 | RequestBody: false,
43 | ResponseHeader: true,
44 | ResponseBody: false,
45 | },
46 | },
47 | })
48 |
49 | name := "exampleobject"
50 | _, err := c.Object.Get(context.Background(), name, nil)
51 | logStatus(err)
52 | }
53 |
--------------------------------------------------------------------------------
/example/object/get_with_retry.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 |
8 | "net/http"
9 | "os"
10 | "time"
11 |
12 | "github.com/tencentyun/cos-go-sdk-v5"
13 | "github.com/tencentyun/cos-go-sdk-v5/debug"
14 | )
15 |
16 | func logStatus(err error) {
17 | if err == nil {
18 | return
19 | }
20 | if cos.IsNotFoundError(err) {
21 | // WARN
22 | fmt.Println("WARN: Resource is not existed")
23 | } else if e, ok := cos.IsCOSError(err); ok {
24 | fmt.Printf("ERROR: Code: %v\n", e.Code)
25 | fmt.Printf("ERROR: Message: %v\n", e.Message)
26 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
27 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
28 | // ERROR
29 | } else {
30 | fmt.Printf("ERROR: %v\n", err)
31 | // ERROR
32 | }
33 | }
34 |
35 | func main() {
36 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
37 | b := &cos.BaseURL{BucketURL: u}
38 | c := cos.NewClient(b, &http.Client{
39 | Transport: &cos.AuthorizationTransport{
40 | SecretID: os.Getenv("SECRETID"),
41 | SecretKey: os.Getenv("SECRETKEY"),
42 | Transport: &debug.DebugRequestTransport{
43 | RequestHeader: true,
44 | // Notice when put a large file and set need the request body, might happend out of memory error.
45 | RequestBody: false,
46 | ResponseHeader: true,
47 | ResponseBody: false,
48 | },
49 | },
50 | })
51 | // Get 请求配置重试
52 | c.Conf.RetryOpt.Count = 3 // 错误重试次数,默认重试3次
53 | c.Conf.RetryOpt.Interval = time.Millisecond // 错误重试间隔时间,默认0
54 | c.Conf.RetryOpt.StatusCode = []int{} // 默认5xx都会重试,该参数配置其余需要重试的响应码
55 |
56 | name := "exampleobject"
57 | _, err := c.Object.Get(context.Background(), name, nil)
58 | logStatus(err)
59 | }
60 |
--------------------------------------------------------------------------------
/example/object/head.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 | "os"
8 |
9 | "net/http"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func logStatus(err error) {
16 | if err == nil {
17 | return
18 | }
19 | if cos.IsNotFoundError(err) {
20 | // WARN
21 | fmt.Println("WARN: Resource is not existed")
22 | } else if e, ok := cos.IsCOSError(err); ok {
23 | fmt.Printf("ERROR: Code: %v\n", e.Code)
24 | fmt.Printf("ERROR: Message: %v\n", e.Message)
25 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
26 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
27 | // ERROR
28 | } else {
29 | fmt.Printf("ERROR: %v\n", err)
30 | // ERROR
31 | }
32 | }
33 |
34 | func main() {
35 | // 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket
36 | // 替换为用户的 region,存储桶region可以在COS控制台“存储桶概览”查看 https://console.cloud.tencent.com/ ,关于地域的详情见 https://cloud.tencent.com/document/product/436/6224 。
37 | u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
38 | b := &cos.BaseURL{BucketURL: u}
39 | c := cos.NewClient(b, &http.Client{
40 | Transport: &cos.AuthorizationTransport{
41 | // 通过环境变量获取密钥
42 | // 环境变量 SECRETID 表示用户的 SecretId,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
43 | SecretID: os.Getenv("SECRETID"),
44 | // 环境变量 SECRETKEY 表示用户的 SecretKey,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
45 | SecretKey: os.Getenv("SECRETKEY"),
46 | // Debug 模式,把对应 请求头部、请求内容、响应头部、响应内容 输出到标准输出
47 | Transport: &debug.DebugRequestTransport{
48 | RequestHeader: true,
49 | RequestBody: true,
50 | ResponseHeader: true,
51 | ResponseBody: true,
52 | },
53 | },
54 | })
55 |
56 | name := "test/hello.txt"
57 | _, err := c.Object.Head(context.Background(), name, nil)
58 | logStatus(err)
59 | }
60 |
--------------------------------------------------------------------------------
/example/object/initiateMultipartUpload.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 | "os"
8 | "time"
9 |
10 | "net/http"
11 |
12 | "github.com/tencentyun/cos-go-sdk-v5"
13 | "github.com/tencentyun/cos-go-sdk-v5/debug"
14 | )
15 |
16 | func logStatus(err error) {
17 | if err == nil {
18 | return
19 | }
20 | if cos.IsNotFoundError(err) {
21 | // WARN
22 | fmt.Println("WARN: Resource is not existed")
23 | } else if e, ok := cos.IsCOSError(err); ok {
24 | fmt.Printf("ERROR: Code: %v\n", e.Code)
25 | fmt.Printf("ERROR: Message: %v\n", e.Message)
26 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
27 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
28 | // ERROR
29 | } else {
30 | fmt.Printf("ERROR: %v\n", err)
31 | // ERROR
32 | }
33 | }
34 |
35 | func main() {
36 | u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
37 | b := &cos.BaseURL{BucketURL: u}
38 | c := cos.NewClient(b, &http.Client{
39 | Transport: &cos.AuthorizationTransport{
40 | SecretID: os.Getenv("SECRETID"),
41 | SecretKey: os.Getenv("SECRETKEY"),
42 | Transport: &debug.DebugRequestTransport{
43 | RequestHeader: true,
44 | RequestBody: true,
45 | ResponseHeader: true,
46 | ResponseBody: true,
47 | },
48 | },
49 | })
50 |
51 | name := "test_multipart" + time.Now().Format(time.RFC3339)
52 | v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil)
53 | logStatus(err)
54 | fmt.Printf("%s\n", v.UploadID)
55 | }
56 |
--------------------------------------------------------------------------------
/example/object/is_exist.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 | "os"
8 |
9 | "net/http"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func main() {
16 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
17 | b := &cos.BaseURL{
18 | BucketURL: u,
19 | }
20 | c := cos.NewClient(b, &http.Client{
21 | Transport: &cos.AuthorizationTransport{
22 | SecretID: os.Getenv("SECRETID"),
23 | SecretKey: os.Getenv("SECRETKEY"),
24 | Transport: &debug.DebugRequestTransport{
25 | RequestHeader: true,
26 | RequestBody: true,
27 | ResponseHeader: true,
28 | ResponseBody: true,
29 | },
30 | },
31 | })
32 |
33 | name := "exampleobject"
34 | ok, err := c.Object.IsExist(context.Background(), name)
35 | if err == nil && ok {
36 | fmt.Printf("object exists\n")
37 | } else if err != nil {
38 | fmt.Printf("head object failed: %v\n", err)
39 | } else {
40 | fmt.Printf("object does not exist\n")
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/example/object/moveObject.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/url"
6 | "os"
7 | "strings"
8 |
9 | "net/http"
10 |
11 | "fmt"
12 |
13 | "github.com/tencentyun/cos-go-sdk-v5"
14 | "github.com/tencentyun/cos-go-sdk-v5/debug"
15 | )
16 |
17 | func logStatus(err error) {
18 | if err == nil {
19 | return
20 | }
21 | if cos.IsNotFoundError(err) {
22 | // WARN
23 | fmt.Println("WARN: Resource is not existed")
24 | } else if e, ok := cos.IsCOSError(err); ok {
25 | fmt.Printf("ERROR: Code: %v\n", e.Code)
26 | fmt.Printf("ERROR: Message: %v\n", e.Message)
27 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
28 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
29 | // ERROR
30 | } else {
31 | fmt.Printf("ERROR: %v\n", err)
32 | // ERROR
33 | }
34 | }
35 |
36 | func main() {
37 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
38 | b := &cos.BaseURL{BucketURL: u}
39 | c := cos.NewClient(b, &http.Client{
40 | Transport: &cos.AuthorizationTransport{
41 | SecretID: os.Getenv("SECRETID"),
42 | SecretKey: os.Getenv("SECRETKEY"),
43 | Transport: &debug.DebugRequestTransport{
44 | RequestHeader: true,
45 | RequestBody: true,
46 | ResponseHeader: true,
47 | ResponseBody: true,
48 | },
49 | },
50 | })
51 |
52 | source := "test/oldfile"
53 | f := strings.NewReader("test")
54 |
55 | // 上传文件
56 | _, err := c.Object.Put(context.Background(), source, f, nil)
57 | logStatus(err)
58 |
59 | // 重命名
60 | dest := "test/newfile"
61 | soruceURL := fmt.Sprintf("%s/%s", u.Host, source)
62 | _, _, err = c.Object.Copy(context.Background(), dest, soruceURL, nil)
63 | logStatus(err)
64 | if err == nil {
65 | _, err = c.Object.Delete(context.Background(), source, nil)
66 | logStatus(err)
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/example/object/multicopy.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "net/url"
6 | "os"
7 |
8 | "net/http"
9 |
10 | "fmt"
11 |
12 | "github.com/tencentyun/cos-go-sdk-v5"
13 | "github.com/tencentyun/cos-go-sdk-v5/debug"
14 | )
15 |
16 | func logStatus(err error) {
17 | if err == nil {
18 | return
19 | }
20 | if cos.IsNotFoundError(err) {
21 | // WARN
22 | fmt.Println("WARN: Resource is not existed")
23 | } else if e, ok := cos.IsCOSError(err); ok {
24 | fmt.Printf("ERROR: Code: %v\n", e.Code)
25 | fmt.Printf("ERROR: Message: %v\n", e.Message)
26 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
27 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
28 | // ERROR
29 | } else {
30 | fmt.Printf("ERROR: %v\n", err)
31 | // ERROR
32 | }
33 | }
34 |
35 | func main() {
36 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
37 | b := &cos.BaseURL{BucketURL: u}
38 | c := cos.NewClient(b, &http.Client{
39 | Transport: &cos.AuthorizationTransport{
40 | SecretID: os.Getenv("SECRETID"),
41 | SecretKey: os.Getenv("SECRETKEY"),
42 | Transport: &debug.DebugRequestTransport{
43 | RequestHeader: true,
44 | RequestBody: true,
45 | ResponseHeader: true,
46 | ResponseBody: true,
47 | },
48 | },
49 | })
50 |
51 | opt := &cos.MultiCopyOptions{
52 | OptCopy: &cos.ObjectCopyOptions{
53 | &cos.ObjectCopyHeaderOptions{
54 | XCosStorageClass: "Archive",
55 | },
56 | nil,
57 | },
58 | ThreadPoolSize: 10,
59 | }
60 | source := "exampleobject"
61 | soruceURL := fmt.Sprintf("%s/%s", u.Host, source)
62 | dest := fmt.Sprintf("destobject")
63 | res, _, err := c.Object.MultiCopy(context.Background(), dest, soruceURL, opt)
64 | logStatus(err)
65 | fmt.Printf("res:%+v\n", res)
66 | }
67 |
--------------------------------------------------------------------------------
/example/object/options.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 | "os"
8 |
9 | "net/http"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func logStatus(err error) {
16 | if err == nil {
17 | return
18 | }
19 | if cos.IsNotFoundError(err) {
20 | // WARN
21 | fmt.Println("WARN: Resource is not existed")
22 | } else if e, ok := cos.IsCOSError(err); ok {
23 | fmt.Printf("ERROR: Code: %v\n", e.Code)
24 | fmt.Printf("ERROR: Message: %v\n", e.Message)
25 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
26 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
27 | // ERROR
28 | } else {
29 | fmt.Printf("ERROR: %v\n", err)
30 | // ERROR
31 | }
32 | }
33 |
34 | func main() {
35 | u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
36 | b := &cos.BaseURL{BucketURL: u}
37 | c := cos.NewClient(b, &http.Client{
38 | Transport: &cos.AuthorizationTransport{
39 | SecretID: os.Getenv("SECRETID"),
40 | SecretKey: os.Getenv("SECRETKEY"),
41 | Transport: &debug.DebugRequestTransport{
42 | RequestHeader: true,
43 | RequestBody: true,
44 | ResponseHeader: true,
45 | ResponseBody: true,
46 | },
47 | },
48 | })
49 |
50 | name := "test/hello.txt"
51 | opt := &cos.ObjectOptionsOptions{
52 | Origin: "http://www.qq.com",
53 | AccessControlRequestMethod: "PUT",
54 | }
55 | _, err := c.Object.Options(context.Background(), name, opt)
56 | logStatus(err)
57 | }
58 |
--------------------------------------------------------------------------------
/example/object/presigned_url_with_token.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 | "time"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | )
13 |
14 | type URLToken struct {
15 | SessionToken string `url:"x-cos-security-token,omitempty" header:"-"`
16 | }
17 |
18 | func main() {
19 | // 替换成您的临时密钥
20 | tak := os.Getenv("SECRETID")
21 | tsk := os.Getenv("SECRETKEY")
22 | token := &URLToken{
23 | SessionToken: "",
24 | }
25 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
26 | b := &cos.BaseURL{BucketURL: u}
27 | c := cos.NewClient(b, &http.Client{})
28 | name := "xx/../exampleobject"
29 | ctx := context.Background()
30 |
31 | // 方法1 通过 tag 设置 x-cos-security-token
32 | // Get presigned
33 | // http Method需要和实际http请求一致,如PUT请求设置成http.MethodPut,GET请求设置成http.MethodGet
34 | presignedURL, err := c.Object.GetPresignedURL(ctx, http.MethodGet, name, tak, tsk, time.Hour, token)
35 | if err != nil {
36 | fmt.Printf("Error: %v\n", err)
37 | return
38 | }
39 | fmt.Println(presignedURL.String())
40 |
41 | // 方法2 通过 PresignedURLOptions 设置 x-cos-security-token
42 | opt := &cos.PresignedURLOptions{
43 | Query: &url.Values{},
44 | Header: &http.Header{},
45 | }
46 | opt.Query.Add("x-cos-security-token", "")
47 | // Get presigned
48 | // http Method需要和实际http请求一致,如PUT请求设置成http.MethodPut,GET请求设置成http.MethodGet
49 | presignedURL, err = c.Object.GetPresignedURL(ctx, http.MethodGet, name, tak, tsk, time.Hour, opt)
50 | if err != nil {
51 | fmt.Printf("Error: %v\n", err)
52 | return
53 | }
54 | fmt.Println(presignedURL.String())
55 | }
56 |
--------------------------------------------------------------------------------
/example/object/putACL.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 | "os"
8 |
9 | "net/http"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func logStatus(err error) {
16 | if err == nil {
17 | return
18 | }
19 | if cos.IsNotFoundError(err) {
20 | // WARN
21 | fmt.Println("WARN: Resource is not existed")
22 | } else if e, ok := cos.IsCOSError(err); ok {
23 | fmt.Printf("ERROR: Code: %v\n", e.Code)
24 | fmt.Printf("ERROR: Message: %v\n", e.Message)
25 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
26 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
27 | // ERROR
28 | } else {
29 | fmt.Printf("ERROR: %v\n", err)
30 | // ERROR
31 | }
32 | }
33 |
34 | func main() {
35 | u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
36 | b := &cos.BaseURL{BucketURL: u}
37 | c := cos.NewClient(b, &http.Client{
38 | Transport: &cos.AuthorizationTransport{
39 | SecretID: os.Getenv("SECRETID"),
40 | SecretKey: os.Getenv("SECRETKEY"),
41 | Transport: &debug.DebugRequestTransport{
42 | RequestHeader: true,
43 | RequestBody: true,
44 | ResponseHeader: true,
45 | ResponseBody: true,
46 | },
47 | },
48 | })
49 |
50 | opt := &cos.ObjectPutACLOptions{
51 | Header: &cos.ACLHeaderOptions{
52 | XCosACL: cos.ACL.Private,
53 | },
54 | }
55 | name := "test/hello.txt"
56 | _, err := c.Object.PutACL(context.Background(), name, opt)
57 | logStatus(err)
58 |
59 | // with body
60 | opt = &cos.ObjectPutACLOptions{
61 | Body: &cos.ACLXml{
62 | Owner: &cos.Owner{
63 | ID: "qcs::cam::uin/100000760461:uin/100000760461",
64 | },
65 | AccessControlList: []cos.ACLGrant{
66 | {
67 | Grantee: &cos.ACLGrantee{
68 | Type: "RootAccount",
69 | ID: "qcs::cam::uin/100000760461:uin/100000760461",
70 | },
71 |
72 | Permission: "FULL_CONTROL",
73 | },
74 | },
75 | },
76 | }
77 |
78 | _, err = c.Object.PutACL(context.Background(), name, opt)
79 | logStatus(err)
80 | }
81 |
--------------------------------------------------------------------------------
/example/object/restore.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/http"
7 | "net/url"
8 | "os"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func logStatus(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("WARN: Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("ERROR: Code: %v\n", e.Code)
23 | fmt.Printf("ERROR: Message: %v\n", e.Message)
24 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
25 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Printf("ERROR: %v\n", err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | // 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket
35 | // 替换为用户的 region,存储桶region可以在COS控制台“存储桶概览”查看 https://console.cloud.tencent.com/ ,关于地域的详情见 https://cloud.tencent.com/document/product/436/6224 。
36 | u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
37 | b := &cos.BaseURL{BucketURL: u}
38 | c := cos.NewClient(b, &http.Client{
39 | Transport: &cos.AuthorizationTransport{
40 | // 通过环境变量获取密钥
41 | // 环境变量 SECRETID 表示用户的 SecretId,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
42 | SecretID: os.Getenv("SECRETID"),
43 | // 环境变量 SECRETKEY 表示用户的 SecretKey,登录访问管理控制台查看密钥,https://console.cloud.tencent.com/cam/capi
44 | SecretKey: os.Getenv("SECRETKEY"),
45 | // Debug 模式,把对应 请求头部、请求内容、响应头部、响应内容 输出到标准输出
46 | Transport: &debug.DebugRequestTransport{
47 | RequestHeader: true,
48 | RequestBody: true,
49 | ResponseHeader: true,
50 | ResponseBody: true,
51 | },
52 | },
53 | })
54 |
55 | opt := &cos.ObjectRestoreOptions{
56 | Days: 2,
57 | Tier: &cos.CASJobParameters{
58 | // Standard, Exepdited and Bulk
59 | Tier: "Expedited",
60 | },
61 | }
62 | name := "archivetest"
63 | _, err := c.Object.PostRestore(context.Background(), name, opt)
64 | logStatus(err)
65 | }
66 |
--------------------------------------------------------------------------------
/example/object/select_csv.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 | "os"
8 |
9 | "io/ioutil"
10 | "net/http"
11 |
12 | "github.com/tencentyun/cos-go-sdk-v5"
13 | "github.com/tencentyun/cos-go-sdk-v5/debug"
14 | )
15 |
16 | func main() {
17 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
18 | b := &cos.BaseURL{BucketURL: u}
19 | c := cos.NewClient(b, &http.Client{
20 | Transport: &cos.AuthorizationTransport{
21 | SecretID: os.Getenv("SECRETID"),
22 | SecretKey: os.Getenv("SECRETKEY"),
23 | Transport: &debug.DebugRequestTransport{
24 | RequestHeader: true,
25 | // Notice when put a large file and set need the request body, might happend out of memory error.
26 | RequestBody: false,
27 | ResponseHeader: true,
28 | ResponseBody: false,
29 | },
30 | },
31 | })
32 |
33 | opt := &cos.ObjectSelectOptions{
34 | Expression: "Select * from COSObject",
35 | ExpressionType: "SQL",
36 | InputSerialization: &cos.SelectInputSerialization{
37 | CSV: &cos.CSVInputSerialization{
38 | FileHeaderInfo: "IGNORE",
39 | },
40 | },
41 | OutputSerialization: &cos.SelectOutputSerialization{
42 | CSV: &cos.CSVOutputSerialization{
43 | RecordDelimiter: "\n",
44 | },
45 | },
46 | RequestProgress: "TRUE",
47 | }
48 | res, err := c.Object.Select(context.Background(), "test.csv", opt)
49 | if err != nil {
50 | panic(err)
51 | }
52 | defer res.Close()
53 | data, err := ioutil.ReadAll(res)
54 | if err != nil {
55 | panic(err)
56 | }
57 | fmt.Printf("data: %v\n", string(data))
58 | resp, _ := res.(*cos.ObjectSelectResponse)
59 | fmt.Printf("data: %+v\n", resp.Frame)
60 |
61 | // Select To File
62 | _, err = c.Object.SelectToFile(context.Background(), "test.csv", "./test.csv", opt)
63 | if err != nil {
64 | panic(err)
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/example/object/sse_cos.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "errors"
6 | "fmt"
7 | "io/ioutil"
8 | "net/http"
9 | "net/url"
10 | "os"
11 | "strings"
12 |
13 | "github.com/tencentyun/cos-go-sdk-v5"
14 | "github.com/tencentyun/cos-go-sdk-v5/debug"
15 | )
16 |
17 | func logStatus(err error) {
18 | if err == nil {
19 | return
20 | }
21 | if cos.IsNotFoundError(err) {
22 | // WARN
23 | fmt.Println("WARN: Resource is not existed")
24 | } else if e, ok := cos.IsCOSError(err); ok {
25 | fmt.Printf("ERROR: Code: %v\n", e.Code)
26 | fmt.Printf("ERROR: Message: %v\n", e.Message)
27 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
28 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
29 | // ERROR
30 | } else {
31 | fmt.Printf("ERROR: %v\n", err)
32 | // ERROR
33 | }
34 | }
35 |
36 | func main() {
37 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
38 | b := &cos.BaseURL{BucketURL: u}
39 | c := cos.NewClient(b, &http.Client{
40 | Transport: &cos.AuthorizationTransport{
41 | SecretID: os.Getenv("SECRETID"),
42 | SecretKey: os.Getenv("SECRETKEY"),
43 | Transport: &debug.DebugRequestTransport{
44 | RequestHeader: true,
45 | // Notice when put a large file and set need the request body, might happend out of memory error.
46 | RequestBody: false,
47 | ResponseHeader: true,
48 | ResponseBody: true,
49 | },
50 | },
51 | })
52 | opt := &cos.ObjectPutOptions{
53 | ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
54 | ContentType: "text/html",
55 | XCosServerSideEncryption: "AES256",
56 | },
57 | ACLHeaderOptions: &cos.ACLHeaderOptions{},
58 | }
59 | name := "PutFromGoWithSSE-COS"
60 | content := "Put Object From Go With SSE-COS"
61 | f := strings.NewReader(content)
62 | _, err := c.Object.Put(context.Background(), name, f, opt)
63 | logStatus(err)
64 |
65 | getopt := &cos.ObjectGetOptions{}
66 | var resp *cos.Response
67 | resp, err = c.Object.Get(context.Background(), name, getopt)
68 | logStatus(err)
69 |
70 | bodyBytes, _ := ioutil.ReadAll(resp.Body)
71 | bodyContent := string(bodyBytes)
72 | if bodyContent != content {
73 | logStatus(errors.New("Content inconsistency"))
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/example/object/tagging.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "net/url"
7 | "os"
8 |
9 | "net/http"
10 |
11 | "github.com/tencentyun/cos-go-sdk-v5"
12 | "github.com/tencentyun/cos-go-sdk-v5/debug"
13 | )
14 |
15 | func logStatus(err error) {
16 | if err == nil {
17 | return
18 | }
19 | if cos.IsNotFoundError(err) {
20 | // WARN
21 | fmt.Println("WARN: Resource is not existed")
22 | } else if e, ok := cos.IsCOSError(err); ok {
23 | fmt.Printf("ERROR: Code: %v\n", e.Code)
24 | fmt.Printf("ERROR: Message: %v\n", e.Message)
25 | fmt.Printf("ERROR: Resource: %v\n", e.Resource)
26 | fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
27 | // ERROR
28 | } else {
29 | fmt.Printf("ERROR: %v\n", err)
30 | // ERROR
31 | }
32 | }
33 |
34 | func main() {
35 | u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
36 | b := &cos.BaseURL{
37 | BucketURL: u,
38 | }
39 | c := cos.NewClient(b, &http.Client{
40 | Transport: &cos.AuthorizationTransport{
41 | SecretID: os.Getenv("SECRETID"),
42 | SecretKey: os.Getenv("SECRETKEY"),
43 | Transport: &debug.DebugRequestTransport{
44 | RequestHeader: true,
45 | RequestBody: true,
46 | ResponseHeader: true,
47 | ResponseBody: true,
48 | },
49 | },
50 | })
51 | name := "test"
52 |
53 | opt := &cos.ObjectPutTaggingOptions{
54 | TagSet: []cos.ObjectTaggingTag{
55 | {
56 | Key: "test_k2",
57 | Value: "test_v2",
58 | },
59 | {
60 | Key: "test_k3",
61 | Value: "test_v3",
62 | },
63 | },
64 | }
65 |
66 | _, err := c.Object.PutTagging(context.Background(), name, opt)
67 | logStatus(err)
68 |
69 | res, _, err := c.Object.GetTagging(context.Background(), name)
70 | logStatus(err)
71 | fmt.Printf("%v\n", res.TagSet)
72 |
73 | _, err = c.Object.DeleteTagging(context.Background(), name)
74 | logStatus(err)
75 | }
76 |
--------------------------------------------------------------------------------
/example/service/get.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "os"
7 |
8 | "net/http"
9 |
10 | "github.com/tencentyun/cos-go-sdk-v5"
11 | "github.com/tencentyun/cos-go-sdk-v5/debug"
12 | )
13 |
14 | func logStatus(err error) {
15 | if err == nil {
16 | return
17 | }
18 | if cos.IsNotFoundError(err) {
19 | // WARN
20 | fmt.Println("Resource is not existed")
21 | } else if e, ok := cos.IsCOSError(err); ok {
22 | fmt.Printf("Code: %v\n", e.Code)
23 | fmt.Printf("Message: %v\n", e.Message)
24 | fmt.Printf("Resource: %v\n", e.Resource)
25 | fmt.Printf("RequestId: %v\n", e.RequestID)
26 | // ERROR
27 | } else {
28 | fmt.Println(err)
29 | // ERROR
30 | }
31 | }
32 |
33 | func main() {
34 | c := cos.NewClient(nil, &http.Client{
35 | Transport: &cos.AuthorizationTransport{
36 | SecretID: os.Getenv("SECRETID"),
37 | SecretKey: os.Getenv("SECRETKEY"),
38 | Transport: &debug.DebugRequestTransport{
39 | RequestHeader: true,
40 | RequestBody: true,
41 | ResponseHeader: true,
42 | ResponseBody: true,
43 | },
44 | },
45 | })
46 |
47 | s, _, err := c.Service.Get(context.Background())
48 | if err != nil {
49 | logStatus(err)
50 | }
51 |
52 | for _, b := range s.Buckets {
53 | fmt.Printf("%#v\n", b)
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/example/test.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | run() {
4 | go run "$@"
5 |
6 | if [ $? -ne 0 ]
7 | then
8 | exit 3
9 | fi
10 | }
11 |
12 | echo '###### service ####'
13 | run ./service/get.go
14 |
15 |
16 | echo '##### bucket ####'
17 |
18 | run ./bucket/delete.go
19 | run ./bucket/put.go
20 | run ./bucket/putACL.go
21 | run ./bucket/putCORS.go
22 | run ./bucket/putLifecycle.go
23 | run ./bucket/putTagging.go
24 | run ./bucket/get.go
25 | run ./bucket/getACL.go
26 | run ./bucket/getCORS.go
27 | run ./bucket/getLifecycle.go
28 | run ./bucket/getTagging.go
29 | run ./bucket/getLocation.go
30 | run ./bucket/head.go
31 | run ./bucket/listMultipartUploads.go
32 | run ./bucket/delete.go
33 | run ./bucket/deleteCORS.go
34 | run ./bucket/deleteLifecycle.go
35 | run ./bucket/deleteTagging.go
36 |
37 |
38 | echo '##### object ####'
39 |
40 | run ./bucket/putCORS.go
41 | run ./object/put.go
42 | run ./object/uploadFile.go
43 | run ./object/putACL.go
44 | run ./object/append.go
45 | run ./object/get.go
46 | run ./object/head.go
47 | run ./object/getAnonymous.go
48 | run ./object/getACL.go
49 | run ./object/listParts.go
50 | run ./object/options.go
51 | run ./object/initiateMultipartUpload.go
52 | run ./object/uploadPart.go
53 | run ./object/completeMultipartUpload.go
54 | run ./object/abortMultipartUpload.go
55 | run ./object/delete.go
56 | run ./object/deleteMultiple.go
57 | run ./object/copy.go
58 |
--------------------------------------------------------------------------------
/go.mod:
--------------------------------------------------------------------------------
1 | module github.com/tencentyun/cos-go-sdk-v5
2 |
3 | go 1.12
4 |
5 | require (
6 | github.com/QcloudApi/qcloud_sign_golang v0.0.0-20141224014652-e4130a326409
7 | github.com/clbanning/mxj v1.8.4
8 | github.com/golang-jwt/jwt/v5 v5.2.1
9 | github.com/google/go-querystring v1.0.0
10 | github.com/google/uuid v1.1.1
11 | github.com/mitchellh/mapstructure v1.4.3
12 | github.com/mozillazg/go-httpheader v0.2.1
13 | github.com/stretchr/testify v1.3.0
14 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.563
15 | github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.563
16 | )
17 |
--------------------------------------------------------------------------------
/service.go:
--------------------------------------------------------------------------------
1 | package cos
2 |
3 | import (
4 | "context"
5 | "encoding/xml"
6 | "net/http"
7 | )
8 |
9 | // Service 相关 API
10 | type ServiceService service
11 |
12 | // ServiceGetResult is the result of Get Service
13 | type ServiceGetResult struct {
14 | XMLName xml.Name `xml:"ListAllMyBucketsResult"`
15 | Owner *Owner `xml:"Owner"`
16 | Buckets []Bucket `xml:"Buckets>Bucket,omitempty"`
17 | Marker string `xml:"Marker"`
18 | NextMarker string `xml:"NextMarker"`
19 | IsTruncated bool `xml:"IsTruncated"`
20 | }
21 |
22 | type ServiceGetOptions struct {
23 | TagKey string `url:"tagkey,omitempty"`
24 | TagValue string `url:"tagvalue,omitempty"`
25 | MaxKeys int64 `url:"max-keys,omitempty"`
26 | Marker string `url:"marker,omitempty"`
27 | Range string `url:"range,omitempty"`
28 | CreateTime int64 `url:"create-time,omitempty"`
29 | Region string `url:"region,omitempty"`
30 | }
31 |
32 | // Get Service 接口实现获取该用户下所有Bucket列表。
33 | //
34 | // 该API接口需要使用Authorization签名认证,
35 | // 且只能获取签名中AccessID所属账户的Bucket列表。
36 | //
37 | // https://www.qcloud.com/document/product/436/8291
38 | func (s *ServiceService) Get(ctx context.Context, opt ...*ServiceGetOptions) (*ServiceGetResult, *Response, error) {
39 | var sopt *ServiceGetOptions
40 | if len(opt) > 0 {
41 | sopt = opt[0]
42 | }
43 | var res ServiceGetResult
44 | sendOpt := sendOptions{
45 | baseURL: s.client.BaseURL.ServiceURL,
46 | uri: "/",
47 | method: http.MethodGet,
48 | optQuery: sopt,
49 | result: &res,
50 | }
51 | resp, err := s.client.doRetry(ctx, &sendOpt)
52 | return &res, resp, err
53 | }
54 |
--------------------------------------------------------------------------------
/service_test.go:
--------------------------------------------------------------------------------
1 | package cos
2 |
3 | import (
4 | "context"
5 | "encoding/xml"
6 | "fmt"
7 | "net/http"
8 | "reflect"
9 | "testing"
10 | )
11 |
12 | func TestServiceService_Get(t *testing.T) {
13 | setup()
14 | defer teardown()
15 |
16 | mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
17 | testMethod(t, r, "GET")
18 | fmt.Fprint(w, `
19 |
20 | xbaccxx
21 | 100000760461
22 |
23 |
24 |
25 | huadong-1253846586
26 | ap-shanghai
27 | 2017-06-16T13:08:28Z
28 |
29 |
30 | huanan-1253846586
31 | ap-guangzhou
32 | 2017-06-10T09:00:07Z
33 |
34 |
35 | `)
36 | })
37 |
38 | opt := &ServiceGetOptions{
39 | MaxKeys: 10,
40 | }
41 |
42 | ref, _, err := client.Service.Get(context.Background(), opt)
43 | if err != nil {
44 | t.Fatalf("Service.Get returned error: %v", err)
45 | }
46 |
47 | want := &ServiceGetResult{
48 | XMLName: xml.Name{Local: "ListAllMyBucketsResult"},
49 | Owner: &Owner{
50 | ID: "xbaccxx",
51 | DisplayName: "100000760461",
52 | },
53 | Buckets: []Bucket{
54 | {
55 | Name: "huadong-1253846586",
56 | Region: "ap-shanghai",
57 | CreationDate: "2017-06-16T13:08:28Z",
58 | },
59 | {
60 | Name: "huanan-1253846586",
61 | Region: "ap-guangzhou",
62 | CreationDate: "2017-06-10T09:00:07Z",
63 | },
64 | },
65 | }
66 |
67 | if !reflect.DeepEqual(ref, want) {
68 | t.Errorf("Service.Get returned %+v, want %+v", ref, want)
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/vendor/github.com/google/go-querystring/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2013 Google. All rights reserved.
2 |
3 | Redistribution and use in source and binary forms, with or without
4 | modification, are permitted provided that the following conditions are
5 | met:
6 |
7 | * Redistributions of source code must retain the above copyright
8 | notice, this list of conditions and the following disclaimer.
9 | * Redistributions in binary form must reproduce the above
10 | copyright notice, this list of conditions and the following disclaimer
11 | in the documentation and/or other materials provided with the
12 | distribution.
13 | * Neither the name of Google Inc. nor the names of its
14 | contributors may be used to endorse or promote products derived from
15 | this software without specific prior written permission.
16 |
17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 |
--------------------------------------------------------------------------------
/vendor/github.com/google/go-querystring/README.md:
--------------------------------------------------------------------------------
1 | # go-querystring #
2 |
3 | go-querystring is Go library for encoding structs into URL query parameters.
4 |
5 |
6 | **Documentation:**
7 | **Build Status:** [](https://drone.io/github.com/google/go-querystring/latest)
8 |
9 | ## Usage ##
10 |
11 | ```go
12 | import "github.com/google/go-querystring/query"
13 | ```
14 |
15 | go-querystring is designed to assist in scenarios where you want to construct a
16 | URL using a struct that represents the URL query parameters. You might do this
17 | to enforce the type safety of your parameters, for example, as is done in the
18 | [go-github][] library.
19 |
20 | The query package exports a single `Values()` function. A simple example:
21 |
22 | ```go
23 | type Options struct {
24 | Query string `url:"q"`
25 | ShowAll bool `url:"all"`
26 | Page int `url:"page"`
27 | }
28 |
29 | opt := Options{ "foo", true, 2 }
30 | v, _ := query.Values(opt)
31 | fmt.Print(v.Encode()) // will output: "q=foo&all=true&page=2"
32 | ```
33 |
34 | [go-github]: https://github.com/google/go-github/commit/994f6f8405f052a117d2d0b500054341048fbb08
35 |
36 | ## License ##
37 |
38 | This library is distributed under the BSD-style license found in the [LICENSE](./LICENSE)
39 | file.
40 |
--------------------------------------------------------------------------------
/vendor/github.com/google/go-querystring/go.mod:
--------------------------------------------------------------------------------
1 | module github.com/google/go-querystring
2 |
--------------------------------------------------------------------------------
/vendor/github.com/mozillazg/go-httpheader/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Changelog
2 |
3 | ## 0.2.1 (2018-11-03)
4 |
5 | * add go.mod file to identify as a module
6 |
7 |
8 | ## 0.2.0 (2017-06-24)
9 |
10 | * support http.Header field.
11 |
12 |
13 | ## 0.1.0 (2017-06-10)
14 |
15 | * Initial Release
16 |
--------------------------------------------------------------------------------
/vendor/github.com/mozillazg/go-httpheader/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 mozillazg
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/vendor/github.com/mozillazg/go-httpheader/Makefile:
--------------------------------------------------------------------------------
1 | help:
2 | @echo "test run test"
3 | @echo "lint run lint"
4 |
5 | .PHONY: test
6 | test:
7 | go test -v -cover -coverprofile cover.out
8 | go tool cover -html=cover.out -o cover.html
9 |
10 | .PHONY: lint
11 | lint:
12 | gofmt -s -w .
13 | goimports -w .
14 | golint .
15 | go vet
16 |
--------------------------------------------------------------------------------
/vendor/github.com/mozillazg/go-httpheader/README.md:
--------------------------------------------------------------------------------
1 | # go-httpheader
2 |
3 | go-httpheader is a Go library for encoding structs into Header fields.
4 |
5 | [](https://travis-ci.org/mozillazg/go-httpheader)
6 | [](https://coveralls.io/r/mozillazg/go-httpheader?branch=master)
7 | [](https://goreportcard.com/report/github.com/mozillazg/go-httpheader)
8 | [](https://godoc.org/github.com/mozillazg/go-httpheader)
9 |
10 | ## install
11 |
12 | `go get -u github.com/mozillazg/go-httpheader`
13 |
14 |
15 | ## usage
16 |
17 | ```go
18 | package main
19 |
20 | import (
21 | "fmt"
22 | "net/http"
23 |
24 | "github.com/mozillazg/go-httpheader"
25 | )
26 |
27 | type Options struct {
28 | hide string
29 | ContentType string `header:"Content-Type"`
30 | Length int
31 | XArray []string `header:"X-Array"`
32 | TestHide string `header:"-"`
33 | IgnoreEmpty string `header:"X-Empty,omitempty"`
34 | IgnoreEmptyN string `header:"X-Empty-N,omitempty"`
35 | CustomHeader http.Header
36 | }
37 |
38 | func main() {
39 | opt := Options{
40 | hide: "hide",
41 | ContentType: "application/json",
42 | Length: 2,
43 | XArray: []string{"test1", "test2"},
44 | TestHide: "hide",
45 | IgnoreEmptyN: "n",
46 | CustomHeader: http.Header{
47 | "X-Test-1": []string{"233"},
48 | "X-Test-2": []string{"666"},
49 | },
50 | }
51 | h, _ := httpheader.Header(opt)
52 | fmt.Printf("%#v", h)
53 | // h:
54 | // http.Header{
55 | // "X-Test-1": []string{"233"},
56 | // "X-Test-2": []string{"666"},
57 | // "Content-Type": []string{"application/json"},
58 | // "Length": []string{"2"},
59 | // "X-Array": []string{"test1", "test2"},
60 | // "X-Empty-N": []string{"n"},
61 | //}
62 | }
63 | ```
64 |
--------------------------------------------------------------------------------
/vendor/github.com/mozillazg/go-httpheader/example_test.go:
--------------------------------------------------------------------------------
1 | package httpheader_test
2 |
3 | import (
4 | "fmt"
5 | "net/http"
6 |
7 | "github.com/mozillazg/go-httpheader"
8 | )
9 |
10 | func ExampleHeader() {
11 | type Options struct {
12 | ContentType string `header:"Content-Type"`
13 | Length int
14 | XArray []string `header:"X-Array"`
15 | TestHide string `header:"-"`
16 | IgnoreEmpty string `header:"X-Empty,omitempty"`
17 | IgnoreEmptyN string `header:"X-Empty-N,omitempty"`
18 | CustomHeader http.Header
19 | }
20 |
21 | opt := Options{
22 | ContentType: "application/json",
23 | Length: 2,
24 | XArray: []string{"test1", "test2"},
25 | TestHide: "hide",
26 | IgnoreEmptyN: "n",
27 | CustomHeader: http.Header{
28 | "X-Test-1": []string{"233"},
29 | "X-Test-2": []string{"666"},
30 | },
31 | }
32 | h, _ := httpheader.Header(opt)
33 | fmt.Println(h["Content-Type"])
34 | fmt.Println(h["Length"])
35 | fmt.Println(h["X-Array"])
36 | _, ok := h["TestHide"]
37 | fmt.Println(ok)
38 | _, ok = h["X-Empty"]
39 | fmt.Println(ok)
40 | fmt.Println(h["X-Empty-N"])
41 | fmt.Println(h["X-Test-1"])
42 | fmt.Println(h["X-Test-2"])
43 | // Output:
44 | // [application/json]
45 | // [2]
46 | // [test1 test2]
47 | // false
48 | // false
49 | // [n]
50 | // [233]
51 | // [666]
52 | }
53 |
--------------------------------------------------------------------------------
/vendor/github.com/mozillazg/go-httpheader/go.mod:
--------------------------------------------------------------------------------
1 | module github.com/mozillazg/go-httpheader
2 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/Gopkg.lock:
--------------------------------------------------------------------------------
1 | # This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
2 |
3 |
4 | [[projects]]
5 | name = "github.com/davecgh/go-spew"
6 | packages = ["spew"]
7 | revision = "346938d642f2ec3594ed81d874461961cd0faa76"
8 | version = "v1.1.0"
9 |
10 | [[projects]]
11 | name = "github.com/pmezard/go-difflib"
12 | packages = ["difflib"]
13 | revision = "792786c7400a136282c1664665ae0a8db921c6c2"
14 | version = "v1.0.0"
15 |
16 | [[projects]]
17 | name = "github.com/stretchr/objx"
18 | packages = ["."]
19 | revision = "facf9a85c22f48d2f52f2380e4efce1768749a89"
20 | version = "v0.1"
21 |
22 | [solve-meta]
23 | analyzer-name = "dep"
24 | analyzer-version = 1
25 | inputs-digest = "448ddae4702c6aded2555faafd390c537789bb1c483f70b0431e6634f73f2090"
26 | solver-name = "gps-cdcl"
27 | solver-version = 1
28 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/Gopkg.toml:
--------------------------------------------------------------------------------
1 | [prune]
2 | unused-packages = true
3 | non-go = true
4 | go-tests = true
5 |
6 | [[constraint]]
7 | name = "github.com/davecgh/go-spew"
8 | version = "~1.1.0"
9 |
10 | [[constraint]]
11 | name = "github.com/pmezard/go-difflib"
12 | version = "~1.0.0"
13 |
14 | [[constraint]]
15 | name = "github.com/stretchr/objx"
16 | version = "~0.1.0"
17 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2012-2018 Mat Ryer and Tyler Bunnell
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/assert/assertion_format.go.tmpl:
--------------------------------------------------------------------------------
1 | {{.CommentFormat}}
2 | func {{.DocInfo.Name}}f(t TestingT, {{.ParamsFormat}}) bool {
3 | if h, ok := t.(tHelper); ok { h.Helper() }
4 | return {{.DocInfo.Name}}(t, {{.ForwardedParamsFormat}})
5 | }
6 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/assert/assertion_forward.go.tmpl:
--------------------------------------------------------------------------------
1 | {{.CommentWithoutT "a"}}
2 | func (a *Assertions) {{.DocInfo.Name}}({{.Params}}) bool {
3 | if h, ok := a.t.(tHelper); ok { h.Helper() }
4 | return {{.DocInfo.Name}}(a.t, {{.ForwardedParams}})
5 | }
6 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/assert/doc.go:
--------------------------------------------------------------------------------
1 | // Package assert provides a set of comprehensive testing tools for use with the normal Go testing system.
2 | //
3 | // Example Usage
4 | //
5 | // The following is a complete example using assert in a standard test function:
6 | // import (
7 | // "testing"
8 | // "github.com/stretchr/testify/assert"
9 | // )
10 | //
11 | // func TestSomething(t *testing.T) {
12 | //
13 | // var a string = "Hello"
14 | // var b string = "Hello"
15 | //
16 | // assert.Equal(t, a, b, "The two words should be the same.")
17 | //
18 | // }
19 | //
20 | // if you assert many times, use the format below:
21 | //
22 | // import (
23 | // "testing"
24 | // "github.com/stretchr/testify/assert"
25 | // )
26 | //
27 | // func TestSomething(t *testing.T) {
28 | // assert := assert.New(t)
29 | //
30 | // var a string = "Hello"
31 | // var b string = "Hello"
32 | //
33 | // assert.Equal(a, b, "The two words should be the same.")
34 | // }
35 | //
36 | // Assertions
37 | //
38 | // Assertions allow you to easily write test code, and are global funcs in the `assert` package.
39 | // All assertion functions take, as the first argument, the `*testing.T` object provided by the
40 | // testing framework. This allows the assertion funcs to write the failings and other details to
41 | // the correct place.
42 | //
43 | // Every assertion function also takes an optional string message as the final argument,
44 | // allowing custom error messages to be appended to the message the assertion method outputs.
45 | package assert
46 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/assert/errors.go:
--------------------------------------------------------------------------------
1 | package assert
2 |
3 | import (
4 | "errors"
5 | )
6 |
7 | // AnError is an error instance useful for testing. If the code does not care
8 | // about error specifics, and only needs to return the error for example, this
9 | // error should be used to make the test code more readable.
10 | var AnError = errors.New("assert.AnError general error for testing")
11 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/assert/forward_assertions.go:
--------------------------------------------------------------------------------
1 | package assert
2 |
3 | // Assertions provides assertion methods around the
4 | // TestingT interface.
5 | type Assertions struct {
6 | t TestingT
7 | }
8 |
9 | // New makes a new Assertions object for the specified TestingT.
10 | func New(t TestingT) *Assertions {
11 | return &Assertions{
12 | t: t,
13 | }
14 | }
15 |
16 | //go:generate go run ../_codegen/main.go -output-package=assert -template=assertion_forward.go.tmpl -include-format-funcs
17 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/doc.go:
--------------------------------------------------------------------------------
1 | // Package testify is a set of packages that provide many tools for testifying that your code will behave as you intend.
2 | //
3 | // testify contains the following packages:
4 | //
5 | // The assert package provides a comprehensive set of assertion functions that tie in to the Go testing system.
6 | //
7 | // The http package contains tools to make it easier to test http activity using the Go testing system.
8 | //
9 | // The mock package provides a system by which it is possible to mock your objects and verify calls are happening as expected.
10 | //
11 | // The suite package provides a basic structure for using structs as testing suites, and methods on those structs as tests. It includes setup/teardown functionality in the way of interfaces.
12 | package testify
13 |
14 | // blank imports help docs.
15 | import (
16 | // assert package
17 | _ "github.com/stretchr/testify/assert"
18 | // http package
19 | _ "github.com/stretchr/testify/http"
20 | // mock package
21 | _ "github.com/stretchr/testify/mock"
22 | )
23 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/go.mod:
--------------------------------------------------------------------------------
1 | module github.com/stretchr/testify
2 |
3 | require (
4 | github.com/davecgh/go-spew v1.1.0
5 | github.com/pmezard/go-difflib v1.0.0
6 | github.com/stretchr/objx v0.1.0
7 | )
8 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/go.sum:
--------------------------------------------------------------------------------
1 | github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5 | github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
6 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/http/doc.go:
--------------------------------------------------------------------------------
1 | // Package http DEPRECATED USE net/http/httptest
2 | package http
3 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/http/test_response_writer.go:
--------------------------------------------------------------------------------
1 | package http
2 |
3 | import (
4 | "net/http"
5 | )
6 |
7 | // TestResponseWriter DEPRECATED: We recommend you use http://golang.org/pkg/net/http/httptest instead.
8 | type TestResponseWriter struct {
9 |
10 | // StatusCode is the last int written by the call to WriteHeader(int)
11 | StatusCode int
12 |
13 | // Output is a string containing the written bytes using the Write([]byte) func.
14 | Output string
15 |
16 | // header is the internal storage of the http.Header object
17 | header http.Header
18 | }
19 |
20 | // Header DEPRECATED: We recommend you use http://golang.org/pkg/net/http/httptest instead.
21 | func (rw *TestResponseWriter) Header() http.Header {
22 |
23 | if rw.header == nil {
24 | rw.header = make(http.Header)
25 | }
26 |
27 | return rw.header
28 | }
29 |
30 | // Write DEPRECATED: We recommend you use http://golang.org/pkg/net/http/httptest instead.
31 | func (rw *TestResponseWriter) Write(bytes []byte) (int, error) {
32 |
33 | // assume 200 success if no header has been set
34 | if rw.StatusCode == 0 {
35 | rw.WriteHeader(200)
36 | }
37 |
38 | // add these bytes to the output string
39 | rw.Output = rw.Output + string(bytes)
40 |
41 | // return normal values
42 | return 0, nil
43 |
44 | }
45 |
46 | // WriteHeader DEPRECATED: We recommend you use http://golang.org/pkg/net/http/httptest instead.
47 | func (rw *TestResponseWriter) WriteHeader(i int) {
48 | rw.StatusCode = i
49 | }
50 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/http/test_round_tripper.go:
--------------------------------------------------------------------------------
1 | package http
2 |
3 | import (
4 | "github.com/stretchr/testify/mock"
5 | "net/http"
6 | )
7 |
8 | // TestRoundTripper DEPRECATED USE net/http/httptest
9 | type TestRoundTripper struct {
10 | mock.Mock
11 | }
12 |
13 | // RoundTrip DEPRECATED USE net/http/httptest
14 | func (t *TestRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
15 | args := t.Called(req)
16 | return args.Get(0).(*http.Response), args.Error(1)
17 | }
18 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/mock/doc.go:
--------------------------------------------------------------------------------
1 | // Package mock provides a system by which it is possible to mock your objects
2 | // and verify calls are happening as expected.
3 | //
4 | // Example Usage
5 | //
6 | // The mock package provides an object, Mock, that tracks activity on another object. It is usually
7 | // embedded into a test object as shown below:
8 | //
9 | // type MyTestObject struct {
10 | // // add a Mock object instance
11 | // mock.Mock
12 | //
13 | // // other fields go here as normal
14 | // }
15 | //
16 | // When implementing the methods of an interface, you wire your functions up
17 | // to call the Mock.Called(args...) method, and return the appropriate values.
18 | //
19 | // For example, to mock a method that saves the name and age of a person and returns
20 | // the year of their birth or an error, you might write this:
21 | //
22 | // func (o *MyTestObject) SavePersonDetails(firstname, lastname string, age int) (int, error) {
23 | // args := o.Called(firstname, lastname, age)
24 | // return args.Int(0), args.Error(1)
25 | // }
26 | //
27 | // The Int, Error and Bool methods are examples of strongly typed getters that take the argument
28 | // index position. Given this argument list:
29 | //
30 | // (12, true, "Something")
31 | //
32 | // You could read them out strongly typed like this:
33 | //
34 | // args.Int(0)
35 | // args.Bool(1)
36 | // args.String(2)
37 | //
38 | // For objects of your own type, use the generic Arguments.Get(index) method and make a type assertion:
39 | //
40 | // return args.Get(0).(*MyObject), args.Get(1).(*AnotherObjectOfMine)
41 | //
42 | // This may cause a panic if the object you are getting is nil (the type assertion will fail), in those
43 | // cases you should check for nil first.
44 | package mock
45 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/package_test.go:
--------------------------------------------------------------------------------
1 | package testify
2 |
3 | import (
4 | "github.com/stretchr/testify/assert"
5 | "testing"
6 | )
7 |
8 | func TestImports(t *testing.T) {
9 | if assert.Equal(t, 1, 1) != true {
10 | t.Error("Something is wrong.")
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/require/doc.go:
--------------------------------------------------------------------------------
1 | // Package require implements the same assertions as the `assert` package but
2 | // stops test execution when a test fails.
3 | //
4 | // Example Usage
5 | //
6 | // The following is a complete example using require in a standard test function:
7 | // import (
8 | // "testing"
9 | // "github.com/stretchr/testify/require"
10 | // )
11 | //
12 | // func TestSomething(t *testing.T) {
13 | //
14 | // var a string = "Hello"
15 | // var b string = "Hello"
16 | //
17 | // require.Equal(t, a, b, "The two words should be the same.")
18 | //
19 | // }
20 | //
21 | // Assertions
22 | //
23 | // The `require` package have same global functions as in the `assert` package,
24 | // but instead of returning a boolean result they call `t.FailNow()`.
25 | //
26 | // Every assertion function also takes an optional string message as the final argument,
27 | // allowing custom error messages to be appended to the message the assertion method outputs.
28 | package require
29 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/require/forward_requirements.go:
--------------------------------------------------------------------------------
1 | package require
2 |
3 | // Assertions provides assertion methods around the
4 | // TestingT interface.
5 | type Assertions struct {
6 | t TestingT
7 | }
8 |
9 | // New makes a new Assertions object for the specified TestingT.
10 | func New(t TestingT) *Assertions {
11 | return &Assertions{
12 | t: t,
13 | }
14 | }
15 |
16 | //go:generate go run ../_codegen/main.go -output-package=require -template=require_forward.go.tmpl -include-format-funcs
17 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/require/require.go.tmpl:
--------------------------------------------------------------------------------
1 | {{.Comment}}
2 | func {{.DocInfo.Name}}(t TestingT, {{.Params}}) {
3 | if assert.{{.DocInfo.Name}}(t, {{.ForwardedParams}}) { return }
4 | if h, ok := t.(tHelper); ok { h.Helper() }
5 | t.FailNow()
6 | }
7 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/require/require_forward.go.tmpl:
--------------------------------------------------------------------------------
1 | {{.CommentWithoutT "a"}}
2 | func (a *Assertions) {{.DocInfo.Name}}({{.Params}}) {
3 | if h, ok := a.t.(tHelper); ok { h.Helper() }
4 | {{.DocInfo.Name}}(a.t, {{.ForwardedParams}})
5 | }
6 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/require/requirements.go:
--------------------------------------------------------------------------------
1 | package require
2 |
3 | // TestingT is an interface wrapper around *testing.T
4 | type TestingT interface {
5 | Errorf(format string, args ...interface{})
6 | FailNow()
7 | }
8 |
9 | type tHelper interface {
10 | Helper()
11 | }
12 |
13 | // ComparisonAssertionFunc is a common function prototype when comparing two values. Can be useful
14 | // for table driven tests.
15 | type ComparisonAssertionFunc func(TestingT, interface{}, interface{}, ...interface{})
16 |
17 | // ValueAssertionFunc is a common function prototype when validating a single value. Can be useful
18 | // for table driven tests.
19 | type ValueAssertionFunc func(TestingT, interface{}, ...interface{})
20 |
21 | // BoolAssertionFunc is a common function prototype when validating a bool value. Can be useful
22 | // for table driven tests.
23 | type BoolAssertionFunc func(TestingT, bool, ...interface{})
24 |
25 | // ErrorAssertionFunc is a common function prototype when validating an error value. Can be useful
26 | // for table driven tests.
27 | type ErrorAssertionFunc func(TestingT, error, ...interface{})
28 |
29 | //go:generate go run ../_codegen/main.go -output-package=require -template=require.go.tmpl -include-format-funcs
30 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/suite/interfaces.go:
--------------------------------------------------------------------------------
1 | package suite
2 |
3 | import "testing"
4 |
5 | // TestingSuite can store and return the current *testing.T context
6 | // generated by 'go test'.
7 | type TestingSuite interface {
8 | T() *testing.T
9 | SetT(*testing.T)
10 | }
11 |
12 | // SetupAllSuite has a SetupSuite method, which will run before the
13 | // tests in the suite are run.
14 | type SetupAllSuite interface {
15 | SetupSuite()
16 | }
17 |
18 | // SetupTestSuite has a SetupTest method, which will run before each
19 | // test in the suite.
20 | type SetupTestSuite interface {
21 | SetupTest()
22 | }
23 |
24 | // TearDownAllSuite has a TearDownSuite method, which will run after
25 | // all the tests in the suite have been run.
26 | type TearDownAllSuite interface {
27 | TearDownSuite()
28 | }
29 |
30 | // TearDownTestSuite has a TearDownTest method, which will run after
31 | // each test in the suite.
32 | type TearDownTestSuite interface {
33 | TearDownTest()
34 | }
35 |
36 | // BeforeTest has a function to be executed right before the test
37 | // starts and receives the suite and test names as input
38 | type BeforeTest interface {
39 | BeforeTest(suiteName, testName string)
40 | }
41 |
42 | // AfterTest has a function to be executed right after the test
43 | // finishes and receives the suite and test names as input
44 | type AfterTest interface {
45 | AfterTest(suiteName, testName string)
46 | }
47 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/vendor/github.com/davecgh/go-spew/LICENSE:
--------------------------------------------------------------------------------
1 | ISC License
2 |
3 | Copyright (c) 2012-2016 Dave Collins
4 |
5 | Permission to use, copy, modify, and distribute this software for any
6 | purpose with or without fee is hereby granted, provided that the above
7 | copyright notice and this permission notice appear in all copies.
8 |
9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/vendor/github.com/davecgh/go-spew/spew/bypasssafe.go:
--------------------------------------------------------------------------------
1 | // Copyright (c) 2015-2016 Dave Collins
2 | //
3 | // Permission to use, copy, modify, and distribute this software for any
4 | // purpose with or without fee is hereby granted, provided that the above
5 | // copyright notice and this permission notice appear in all copies.
6 | //
7 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 | // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 | // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 | // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 | // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 | // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 | // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 |
15 | // NOTE: Due to the following build constraints, this file will only be compiled
16 | // when the code is running on Google App Engine, compiled by GopherJS, or
17 | // "-tags safe" is added to the go build command line. The "disableunsafe"
18 | // tag is deprecated and thus should not be used.
19 | // +build js appengine safe disableunsafe
20 |
21 | package spew
22 |
23 | import "reflect"
24 |
25 | const (
26 | // UnsafeDisabled is a build-time constant which specifies whether or
27 | // not access to the unsafe package is available.
28 | UnsafeDisabled = true
29 | )
30 |
31 | // unsafeReflectValue typically converts the passed reflect.Value into a one
32 | // that bypasses the typical safety restrictions preventing access to
33 | // unaddressable and unexported data. However, doing this relies on access to
34 | // the unsafe package. This is a stub version which simply returns the passed
35 | // reflect.Value when the unsafe package is not available.
36 | func unsafeReflectValue(v reflect.Value) reflect.Value {
37 | return v
38 | }
39 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/vendor/github.com/pmezard/go-difflib/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2013, Patrick Mezard
2 | All rights reserved.
3 |
4 | Redistribution and use in source and binary forms, with or without
5 | modification, are permitted provided that the following conditions are
6 | met:
7 |
8 | Redistributions of source code must retain the above copyright
9 | notice, this list of conditions and the following disclaimer.
10 | Redistributions in binary form must reproduce the above copyright
11 | notice, this list of conditions and the following disclaimer in the
12 | documentation and/or other materials provided with the distribution.
13 | The names of its contributors may not be used to endorse or promote
14 | products derived from this software without specific prior written
15 | permission.
16 |
17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 | IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/vendor/github.com/stretchr/objx/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License
2 |
3 | Copyright (c) 2014 Stretchr, Inc.
4 | Copyright (c) 2017-2018 objx contributors
5 |
6 | Permission is hereby granted, free of charge, to any person obtaining a copy
7 | of this software and associated documentation files (the "Software"), to deal
8 | in the Software without restriction, including without limitation the rights
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | copies of the Software, and to permit persons to whom the Software is
11 | furnished to do so, subject to the following conditions:
12 |
13 | The above copyright notice and this permission notice shall be included in all
14 | copies or substantial portions of the Software.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/vendor/github.com/stretchr/objx/constants.go:
--------------------------------------------------------------------------------
1 | package objx
2 |
3 | const (
4 | // PathSeparator is the character used to separate the elements
5 | // of the keypath.
6 | //
7 | // For example, `location.address.city`
8 | PathSeparator string = "."
9 |
10 | // SignatureSeparator is the character that is used to
11 | // separate the Base64 string from the security signature.
12 | SignatureSeparator = "_"
13 | )
14 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/vendor/github.com/stretchr/objx/security.go:
--------------------------------------------------------------------------------
1 | package objx
2 |
3 | import (
4 | "crypto/sha1"
5 | "encoding/hex"
6 | )
7 |
8 | // HashWithKey hashes the specified string using the security
9 | // key.
10 | func HashWithKey(data, key string) string {
11 | hash := sha1.New()
12 | _, err := hash.Write([]byte(data + ":" + key))
13 | if err != nil {
14 | return ""
15 | }
16 | return hex.EncodeToString(hash.Sum(nil))
17 | }
18 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/vendor/github.com/stretchr/objx/tests.go:
--------------------------------------------------------------------------------
1 | package objx
2 |
3 | // Has gets whether there is something at the specified selector
4 | // or not.
5 | //
6 | // If m is nil, Has will always return false.
7 | func (m Map) Has(selector string) bool {
8 | if m == nil {
9 | return false
10 | }
11 | return !m.Get(selector).IsNil()
12 | }
13 |
14 | // IsNil gets whether the data is nil or not.
15 | func (v *Value) IsNil() bool {
16 | return v == nil || v.data == nil
17 | }
18 |
--------------------------------------------------------------------------------
/vendor/github.com/stretchr/testify/vendor/github.com/stretchr/objx/value.go:
--------------------------------------------------------------------------------
1 | package objx
2 |
3 | import (
4 | "fmt"
5 | "strconv"
6 | )
7 |
8 | // Value provides methods for extracting interface{} data in various
9 | // types.
10 | type Value struct {
11 | // data contains the raw data being managed by this Value
12 | data interface{}
13 | }
14 |
15 | // Data returns the raw data contained by this Value
16 | func (v *Value) Data() interface{} {
17 | return v.data
18 | }
19 |
20 | // String returns the value always as a string
21 | func (v *Value) String() string {
22 | switch {
23 | case v.IsStr():
24 | return v.Str()
25 | case v.IsBool():
26 | return strconv.FormatBool(v.Bool())
27 | case v.IsFloat32():
28 | return strconv.FormatFloat(float64(v.Float32()), 'f', -1, 32)
29 | case v.IsFloat64():
30 | return strconv.FormatFloat(v.Float64(), 'f', -1, 64)
31 | case v.IsInt():
32 | return strconv.FormatInt(int64(v.Int()), 10)
33 | case v.IsInt():
34 | return strconv.FormatInt(int64(v.Int()), 10)
35 | case v.IsInt8():
36 | return strconv.FormatInt(int64(v.Int8()), 10)
37 | case v.IsInt16():
38 | return strconv.FormatInt(int64(v.Int16()), 10)
39 | case v.IsInt32():
40 | return strconv.FormatInt(int64(v.Int32()), 10)
41 | case v.IsInt64():
42 | return strconv.FormatInt(v.Int64(), 10)
43 | case v.IsUint():
44 | return strconv.FormatUint(uint64(v.Uint()), 10)
45 | case v.IsUint8():
46 | return strconv.FormatUint(uint64(v.Uint8()), 10)
47 | case v.IsUint16():
48 | return strconv.FormatUint(uint64(v.Uint16()), 10)
49 | case v.IsUint32():
50 | return strconv.FormatUint(uint64(v.Uint32()), 10)
51 | case v.IsUint64():
52 | return strconv.FormatUint(v.Uint64(), 10)
53 | }
54 |
55 | return fmt.Sprintf("%#v", v.Data())
56 | }
57 |
--------------------------------------------------------------------------------