Compare commits
35 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6bc7b5256c | ||
|
|
95b127211f | ||
|
|
e4b5ad8337 | ||
|
|
1942f2303e | ||
|
|
9a672cde0b | ||
|
|
deaefed41a | ||
|
|
d18e8cfac2 | ||
|
|
695c4466f5 | ||
|
|
98eab2886c | ||
|
|
5e69c19d34 | ||
|
|
f6c91c92d6 | ||
|
|
64d31f318a | ||
|
|
dd7c41ca08 | ||
|
|
e6f823c7c1 | ||
|
|
52c110b7ed | ||
|
|
287669a677 | ||
|
|
c88b73871d | ||
|
|
b0a399e92d | ||
|
|
5804e86747 | ||
|
|
cb662cdad5 | ||
|
|
0e9536d989 | ||
|
|
0206a7d026 | ||
|
|
d5130075f0 | ||
|
|
eb4e1ac4c9 | ||
|
|
649bd027d2 | ||
|
|
14683910e1 | ||
|
|
31af2decf4 | ||
|
|
17c5ed144f | ||
|
|
e870e71637 | ||
|
|
f9f617878d | ||
|
|
7b799aff21 | ||
|
|
a0ab0eb0f8 | ||
|
|
1cc49706b4 | ||
|
|
bcc1ed2b83 | ||
|
|
517ae20fdc |
@@ -1,11 +1,5 @@
|
||||
language: go
|
||||
go:
|
||||
- '1.7'
|
||||
- '1.8'
|
||||
- '1.9'
|
||||
- 1.10.x
|
||||
- 1.11.x
|
||||
- 1.12.x
|
||||
- master
|
||||
sudo: false
|
||||
before_install:
|
||||
|
||||
1
auth.go
1
auth.go
@@ -125,6 +125,7 @@ func newAuthorization(secretID, secretKey string, req *http.Request, authTime *A
|
||||
keyTime := authTime.keyString()
|
||||
signKey := calSignKey(secretKey, keyTime)
|
||||
|
||||
req.Header.Set("Host", req.Host)
|
||||
formatHeaders := *new(string)
|
||||
signedHeaderList := *new([]string)
|
||||
formatHeaders, signedHeaderList = genFormatHeaders(req.Header)
|
||||
|
||||
58
bucket.go
58
bucket.go
@@ -102,3 +102,61 @@ type Bucket struct {
|
||||
Region string `xml:"Location,omitempty"`
|
||||
CreationDate string `xml:",omitempty"`
|
||||
}
|
||||
|
||||
type BucketGetObjectVersionsOptions struct {
|
||||
Prefix string `url:"prefix,omitempty"`
|
||||
Delimiter string `url:"delimiter,omitempty"`
|
||||
EncodingType string `url:"encoding-type,omitempty"`
|
||||
KeyMarker string `url:"key-marker,omitempty"`
|
||||
VersionIdMarker string `url:"version-id-marker,omitempty"`
|
||||
MaxKeys int `url:"max-keys,omitempty"`
|
||||
}
|
||||
|
||||
type BucketGetObjectVersionsResult struct {
|
||||
XMLName xml.Name `xml:"ListVersionsResult"`
|
||||
Name string `xml:"Name,omitempty"`
|
||||
EncodingType string `xml:"EncodingType,omitempty"`
|
||||
Prefix string `xml:"Prefix,omitempty"`
|
||||
KeyMarker string `xml:"KeyMarker,omitempty"`
|
||||
VersionIdMarker string `xml:"VersionIdMarker,omitempty"`
|
||||
MaxKeys int `xml:"MaxKeys,omitempty"`
|
||||
Delimiter string `xml:"Delimiter,omitempty"`
|
||||
IsTruncated bool `xml:"IsTruncated,omitempty"`
|
||||
NextKeyMarker string `xml:"NextKeyMarker,omitempty"`
|
||||
NextVersionIdMarker string `xml:"NextVersionIdMarker,omitempty"`
|
||||
CommonPrefixes []string `xml:"CommonPrefixes>Prefix,omitempty"`
|
||||
Version []ListVersionsResultVersion `xml:"Version,omitempty"`
|
||||
DeleteMarker []ListVersionsResultDeleteMarker `xml:"DeleteMarker,omitempty"`
|
||||
}
|
||||
|
||||
type ListVersionsResultVersion struct {
|
||||
Key string `xml:"Key,omitempty"`
|
||||
VersionId string `xml:"VersionId,omitempty"`
|
||||
IsLatest bool `xml:"IsLatest,omitempty"`
|
||||
LastModified string `xml:"LastModified,omitempty"`
|
||||
ETag string `xml:"ETag,omitempty"`
|
||||
Size int `xml:"Size,omitempty"`
|
||||
StorageClass string `xml:"StorageClass,omitempty"`
|
||||
Owner *Owner `xml:"Owner,omitempty"`
|
||||
}
|
||||
|
||||
type ListVersionsResultDeleteMarker struct {
|
||||
Key string `xml:"Key,omitempty"`
|
||||
VersionId string `xml:"VersionId,omitempty"`
|
||||
IsLatest bool `xml:"IsLatest,omitempty"`
|
||||
LastModified string `xml:"LastModified,omitempty"`
|
||||
Owner *Owner `xml:"Owner,omitempty"`
|
||||
}
|
||||
|
||||
func (s *BucketService) GetObjectVersions(ctx context.Context, opt *BucketGetObjectVersionsOptions) (*BucketGetObjectVersionsResult, *Response, error) {
|
||||
var res BucketGetObjectVersionsResult
|
||||
sendOpt := sendOptions{
|
||||
baseURL: s.client.BaseURL.BucketURL,
|
||||
uri: "/?versions",
|
||||
method: http.MethodGet,
|
||||
optQuery: opt,
|
||||
result: &res,
|
||||
}
|
||||
resp, err := s.client.send(ctx, &sendOpt)
|
||||
return &res, resp, err
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
// BucketGetACLResult is same to the ACLXml
|
||||
type BucketGetACLResult ACLXml
|
||||
type BucketGetACLResult = ACLXml
|
||||
|
||||
// GetACL 使用API读取Bucket的ACL表,只有所有者有权操作。
|
||||
//
|
||||
@@ -20,6 +20,9 @@ func (s *BucketService) GetACL(ctx context.Context) (*BucketGetACLResult, *Respo
|
||||
result: &res,
|
||||
}
|
||||
resp, err := s.client.send(ctx, &sendOpt)
|
||||
if err == nil {
|
||||
decodeACL(resp, &res)
|
||||
}
|
||||
return &res, resp, err
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ type BucketEncryptionConfiguration struct {
|
||||
|
||||
type BucketPutEncryptionOptions struct {
|
||||
XMLName xml.Name `xml:"ServerSideEncryptionConfiguration"`
|
||||
Rule *BucketEncryptionConfiguration `xml:"Rule>ApplySideEncryptionConfiguration"`
|
||||
Rule *BucketEncryptionConfiguration `xml:"Rule>ApplyServerSideEncryptionByDefault"`
|
||||
}
|
||||
|
||||
type BucketGetEncryptionResult BucketPutEncryptionOptions
|
||||
|
||||
@@ -21,9 +21,9 @@ func TestBucketService_GetEncryption(t *testing.T) {
|
||||
testFormValues(t, r, vs)
|
||||
fmt.Fprint(w, `<ServerSideEncryptionConfiguration>
|
||||
<Rule>
|
||||
<ApplySideEncryptionConfiguration>
|
||||
<ApplyServerSideEncryptionByDefault>
|
||||
<SSEAlgorithm>AES256</SSEAlgorithm>
|
||||
</ApplySideEncryptionConfiguration>
|
||||
</ApplyServerSideEncryptionByDefault>
|
||||
</Rule>
|
||||
</ServerSideEncryptionConfiguration>`)
|
||||
|
||||
|
||||
47
bucket_intelligenttiering.go
Normal file
47
bucket_intelligenttiering.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package cos
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type BucketIntelligentTieringTransition struct {
|
||||
Days int `xml:"Days,omitempty"`
|
||||
RequestFrequent int `xml:"RequestFrequent,omitempty"`
|
||||
}
|
||||
|
||||
type BucketPutIntelligentTieringOptions struct {
|
||||
XMLName xml.Name `xml:"IntelligentTieringConfiguration"`
|
||||
Status string `xml:"Status,omitempty"`
|
||||
Transition *BucketIntelligentTieringTransition `xml:"Transition,omitempty"`
|
||||
}
|
||||
|
||||
type BucketGetIntelligentTieringResult BucketPutIntelligentTieringOptions
|
||||
|
||||
func (s *BucketService) PutIntelligentTiering(ctx context.Context, opt *BucketPutIntelligentTieringOptions) (*Response, error) {
|
||||
if opt != nil && opt.Transition != nil {
|
||||
opt.Transition.RequestFrequent = 1
|
||||
}
|
||||
sendOpt := sendOptions{
|
||||
baseURL: s.client.BaseURL.BucketURL,
|
||||
uri: "/?intelligenttiering",
|
||||
method: http.MethodPut,
|
||||
body: opt,
|
||||
}
|
||||
resp, err := s.client.send(ctx, &sendOpt)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (s *BucketService) GetIntelligentTiering(ctx context.Context) (*BucketGetIntelligentTieringResult, *Response, error) {
|
||||
var res BucketGetIntelligentTieringResult
|
||||
sendOpt := sendOptions{
|
||||
baseURL: s.client.BaseURL.BucketURL,
|
||||
uri: "/?intelligenttiering",
|
||||
method: http.MethodGet,
|
||||
result: &res,
|
||||
}
|
||||
resp, err := s.client.send(ctx, &sendOpt)
|
||||
return &res, resp, err
|
||||
|
||||
}
|
||||
76
bucket_intelligenttiering_test.go
Normal file
76
bucket_intelligenttiering_test.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package cos
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBucketService_PutIntelligentTiering(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
opt := &BucketPutIntelligentTieringOptions{
|
||||
Status: "Enabled",
|
||||
Transition: &BucketIntelligentTieringTransition{
|
||||
Days: 30,
|
||||
},
|
||||
}
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
testMethod(t, r, http.MethodPut)
|
||||
vs := values{
|
||||
"intelligenttiering": "",
|
||||
}
|
||||
testFormValues(t, r, vs)
|
||||
|
||||
body := &BucketPutIntelligentTieringOptions{}
|
||||
xml.NewDecoder(r.Body).Decode(body)
|
||||
want := opt
|
||||
want.XMLName = xml.Name{Local: "IntelligentTieringConfiguration"}
|
||||
if !reflect.DeepEqual(want, body) {
|
||||
t.Fatalf("Bucket.PutIntelligentTiering request\n body: %+v\n, want %+v\n", body, want)
|
||||
}
|
||||
})
|
||||
|
||||
_, err := client.Bucket.PutIntelligentTiering(context.Background(), opt)
|
||||
if err != nil {
|
||||
t.Fatalf("Bucket.PutIntelligentTiering failed, error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBucketService_GetIntelligentTiering(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
testMethod(t, r, http.MethodGet)
|
||||
vs := values{
|
||||
"intelligenttiering": "",
|
||||
}
|
||||
testFormValues(t, r, vs)
|
||||
|
||||
fmt.Fprint(w, `<IntelligentTieringConfiguration>
|
||||
<Status>Enabled</Status>
|
||||
<Transition>
|
||||
<Days>30</Days>
|
||||
</Transition>
|
||||
</IntelligentTieringConfiguration>`)
|
||||
})
|
||||
res, _, err := client.Bucket.GetIntelligentTiering(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("Bucket.GetIntelligentTiering failed, error: %v", err)
|
||||
}
|
||||
want := &BucketGetIntelligentTieringResult{
|
||||
XMLName: xml.Name{Local: "IntelligentTieringConfiguration"},
|
||||
Status: "Enabled",
|
||||
Transition: &BucketIntelligentTieringTransition{
|
||||
Days: 30,
|
||||
},
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(res, want) {
|
||||
t.Errorf("Bucket.GetIntelligentTiering returned\n%+v, want\n%+v", res, want)
|
||||
}
|
||||
}
|
||||
90
bucket_origin.go
Normal file
90
bucket_origin.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package cos
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type BucketPutOriginOptions struct {
|
||||
XMLName xml.Name `xml:"OriginConfiguration"`
|
||||
Rule []BucketOriginRule `xml:"OriginRule"`
|
||||
}
|
||||
|
||||
type BucketOriginRule struct {
|
||||
OriginType string `xml:"OriginType"`
|
||||
OriginCondition *BucketOriginCondition `xml:"OriginCondition"`
|
||||
OriginParameter *BucketOriginParameter `xml:"OriginParameter"`
|
||||
OriginInfo *BucketOriginInfo `xml:"OriginInfo"`
|
||||
}
|
||||
|
||||
type BucketOriginCondition struct {
|
||||
HTTPStatusCode string `xml:"HTTPStatusCode,omitempty"`
|
||||
Prefix string `xml:"Prefix,omitempty"`
|
||||
}
|
||||
|
||||
type BucketOriginParameter struct {
|
||||
Protocol string `xml:"Protocol,omitempty"`
|
||||
FollowQueryString bool `xml:"FollowQueryString,omitempty"`
|
||||
HttpHeader *BucketOriginHttpHeader `xml:"HttpHeader,omitempty"`
|
||||
FollowRedirection bool `xml:"FollowRedirection,omitempty"`
|
||||
HttpRedirectCode string `xml:"HttpRedirectCode,omitempty"`
|
||||
CopyOriginData bool `xml:"CopyOriginData,omitempty"`
|
||||
}
|
||||
|
||||
type BucketOriginHttpHeader struct {
|
||||
// 目前还不支持 FollowAllHeaders
|
||||
// FollowAllHeaders bool `xml:"FollowAllHeaders,omitempty"`
|
||||
NewHttpHeaders []OriginHttpHeader `xml:"NewHttpHeaders>Header,omitempty"`
|
||||
FollowHttpHeaders []OriginHttpHeader `xml:"FollowHttpHeaders>Header,omitempty"`
|
||||
}
|
||||
|
||||
type OriginHttpHeader struct {
|
||||
Key string `xml:"Key,omitempty"`
|
||||
Value string `xml:"Value,omitempty"`
|
||||
}
|
||||
|
||||
type BucketOriginInfo struct {
|
||||
HostInfo string `xml:"HostInfo>HostName,omitempty"`
|
||||
FileInfo *BucketOriginFileInfo `xml:"FileInfo,omitempty"`
|
||||
}
|
||||
type BucketOriginFileInfo struct {
|
||||
PrefixDirective bool `xml:"PrefixDirective,omitempty"`
|
||||
Prefix string `xml:"Prefix,omitempty"`
|
||||
Suffix string `xml:"Suffix,omitempty"`
|
||||
}
|
||||
|
||||
type BucketGetOriginResult BucketPutOriginOptions
|
||||
|
||||
func (s *BucketService) PutOrigin(ctx context.Context, opt *BucketPutOriginOptions) (*Response, error) {
|
||||
sendOpt := &sendOptions{
|
||||
baseURL: s.client.BaseURL.BucketURL,
|
||||
uri: "/?origin",
|
||||
method: http.MethodPut,
|
||||
body: opt,
|
||||
}
|
||||
resp, err := s.client.send(ctx, sendOpt)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (s *BucketService) GetOrigin(ctx context.Context) (*BucketGetOriginResult, *Response, error) {
|
||||
var res BucketGetOriginResult
|
||||
sendOpt := &sendOptions{
|
||||
baseURL: s.client.BaseURL.BucketURL,
|
||||
uri: "/?origin",
|
||||
method: http.MethodGet,
|
||||
result: &res,
|
||||
}
|
||||
resp, err := s.client.send(ctx, sendOpt)
|
||||
return &res, resp, err
|
||||
}
|
||||
|
||||
func (s *BucketService) DeleteOrigin(ctx context.Context) (*Response, error) {
|
||||
sendOpt := &sendOptions{
|
||||
baseURL: s.client.BaseURL.BucketURL,
|
||||
uri: "/?origin",
|
||||
method: http.MethodDelete,
|
||||
}
|
||||
resp, err := s.client.send(ctx, sendOpt)
|
||||
return resp, err
|
||||
}
|
||||
105
bucket_test.go
105
bucket_test.go
@@ -150,3 +150,108 @@ func TestBucketService_Head(t *testing.T) {
|
||||
t.Fatalf("Bucket.Head returned error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBucketService_GetObjectVersions(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
testMethod(t, r, http.MethodGet)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
vs := values{
|
||||
"versions": "",
|
||||
"delimiter": "/",
|
||||
}
|
||||
testFormValues(t, r, vs)
|
||||
|
||||
fmt.Fprint(w, `<?xml version='1.0' encoding='utf-8' ?>
|
||||
<ListVersionsResult>
|
||||
<Name>examplebucket-1250000000</Name>
|
||||
<Prefix/>
|
||||
<KeyMarker/>
|
||||
<VersionIdMarker/>
|
||||
<MaxKeys>1000</MaxKeys>
|
||||
<IsTruncated>false</IsTruncated>
|
||||
<Delimiter>/</Delimiter>
|
||||
<CommonPrefixes>
|
||||
<Prefix>example-folder-1/</Prefix>
|
||||
</CommonPrefixes>
|
||||
<CommonPrefixes>
|
||||
<Prefix>example-folder-2/</Prefix>
|
||||
</CommonPrefixes>
|
||||
<Version>
|
||||
<Key>example-object-1.jpg</Key>
|
||||
<VersionId>MTg0NDUxNzgxMjEzNTU3NTk1Mjg</VersionId>
|
||||
<IsLatest>true</IsLatest>
|
||||
<LastModified>2019-08-16T10:45:53.000Z</LastModified>
|
||||
<ETag>"5d1143df07a17b23320d0da161e2819e"</ETag>
|
||||
<Size>30</Size>
|
||||
<StorageClass>STANDARD</StorageClass>
|
||||
<Owner>
|
||||
<ID>1250000000</ID>
|
||||
<DisplayName>1250000000</DisplayName>
|
||||
</Owner>
|
||||
</Version>
|
||||
<DeleteMarker>
|
||||
<Key>example-object-1.jpg</Key>
|
||||
<VersionId>MTg0NDUxNzgxMjEzNjE1OTcxMzM</VersionId>
|
||||
<IsLatest>false</IsLatest>
|
||||
<LastModified>2019-08-16T10:45:47.000Z</LastModified>
|
||||
<Owner>
|
||||
<ID>1250000000</ID>
|
||||
<DisplayName>1250000000</DisplayName>
|
||||
</Owner>
|
||||
</DeleteMarker>
|
||||
</ListVersionsResult>`)
|
||||
})
|
||||
|
||||
want := &BucketGetObjectVersionsResult {
|
||||
XMLName: xml.Name { Local: "ListVersionsResult" },
|
||||
Name: "examplebucket-1250000000",
|
||||
MaxKeys: 1000,
|
||||
IsTruncated: false,
|
||||
Delimiter: "/",
|
||||
CommonPrefixes: []string {
|
||||
"example-folder-1/",
|
||||
"example-folder-2/",
|
||||
},
|
||||
Version: []ListVersionsResultVersion {
|
||||
{
|
||||
Key: "example-object-1.jpg",
|
||||
VersionId: "MTg0NDUxNzgxMjEzNTU3NTk1Mjg",
|
||||
IsLatest: true,
|
||||
LastModified: "2019-08-16T10:45:53.000Z",
|
||||
ETag: "\"5d1143df07a17b23320d0da161e2819e\"",
|
||||
Size: 30,
|
||||
StorageClass: "STANDARD",
|
||||
Owner: &Owner {
|
||||
ID: "1250000000",
|
||||
DisplayName: "1250000000",
|
||||
},
|
||||
},
|
||||
},
|
||||
DeleteMarker: []ListVersionsResultDeleteMarker {
|
||||
{
|
||||
Key: "example-object-1.jpg",
|
||||
VersionId: "MTg0NDUxNzgxMjEzNjE1OTcxMzM",
|
||||
IsLatest: false,
|
||||
LastModified: "2019-08-16T10:45:47.000Z",
|
||||
Owner: &Owner {
|
||||
ID: "1250000000",
|
||||
DisplayName: "1250000000",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
opt := &BucketGetObjectVersionsOptions {
|
||||
Delimiter: "/",
|
||||
}
|
||||
res, _, err := client.Bucket.GetObjectVersions(context.Background(), opt)
|
||||
if err != nil {
|
||||
t.Fatalf("Bucket.GetObjectVersions returned error: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(res, want) {
|
||||
t.Errorf("Bucket.GetObjectVersions returned\n%+v\nwant\n%+v", res, want)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
177
ci.go
177
ci.go
@@ -1,14 +1,18 @@
|
||||
package cos
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type CIService service
|
||||
|
||||
type PicOperations struct {
|
||||
IsPicInfo int `json:"is_pic_info,omitempty"`
|
||||
Rules []PicOperationsRules `json:"rules,omitemtpy"`
|
||||
}
|
||||
|
||||
type PicOperationsRules struct {
|
||||
Bucket string `json:"bucket,omitempty"`
|
||||
FileId string `json:"fileid"`
|
||||
@@ -16,9 +20,180 @@ type PicOperationsRules struct {
|
||||
}
|
||||
|
||||
func EncodePicOperations(pic *PicOperations) string {
|
||||
if pic == nil {
|
||||
return ""
|
||||
}
|
||||
bs, err := json.Marshal(pic)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(bs)
|
||||
}
|
||||
|
||||
type ImageProcessResult struct {
|
||||
XMLName xml.Name `xml:"UploadResult"`
|
||||
OriginalInfo *PicOriginalInfo `xml:"OriginalInfo,omitempty"`
|
||||
ProcessObject *PicProcessObject `xml:"ProcessResults>Object,omitempty"`
|
||||
}
|
||||
type PicOriginalInfo struct {
|
||||
Key string `xml:"Key,omitempty"`
|
||||
Location string `xml:"Location,omitempty"`
|
||||
ImageInfo *PicImageInfo `xml:"ImageInfo,omitempty"`
|
||||
}
|
||||
type PicImageInfo struct {
|
||||
Format string `xml:"Format,omitempty"`
|
||||
Width int `xml:"Width,omitempty"`
|
||||
Height int `xml:"Height,omitempty"`
|
||||
Size int `xml:"Size,omitempty"`
|
||||
Quality int `xml:"Quality,omitempty"`
|
||||
}
|
||||
type PicProcessObject struct {
|
||||
Key string `xml:"Key,omitempty"`
|
||||
Location string `xml:"Location,omitempty"`
|
||||
Format string `xml:"Format,omitempty"`
|
||||
Width int `xml:"Width,omitempty"`
|
||||
Height int `xml:"Height,omitempty"`
|
||||
Size int `xml:"Size,omitempty"`
|
||||
Quality int `xml:"Quality,omitempty"`
|
||||
}
|
||||
|
||||
type picOperationsHeader struct {
|
||||
PicOperations string `header:"Pic-Operations" xml:"-" url:"-"`
|
||||
}
|
||||
|
||||
type ImageProcessOptions = PicOperations
|
||||
|
||||
// 云上数据处理 https://cloud.tencent.com/document/product/460/18147
|
||||
func (s *CIService) ImageProcess(ctx context.Context, name string, opt *ImageProcessOptions) (*ImageProcessResult, *Response, error) {
|
||||
header := &picOperationsHeader{
|
||||
PicOperations: EncodePicOperations(opt),
|
||||
}
|
||||
var res ImageProcessResult
|
||||
sendOpt := sendOptions{
|
||||
baseURL: s.client.BaseURL.BucketURL,
|
||||
uri: "/" + encodeURIComponent(name) + "?image_process",
|
||||
method: http.MethodPost,
|
||||
optHeader: header,
|
||||
result: &res,
|
||||
}
|
||||
resp, err := s.client.send(ctx, &sendOpt)
|
||||
return &res, resp, err
|
||||
}
|
||||
|
||||
type ImageRecognitionOptions struct {
|
||||
CIProcess string `url:"ci-process,omitempty"`
|
||||
DetectType string `url:"detect-type,omitempty"`
|
||||
}
|
||||
|
||||
type ImageRecognitionResult struct {
|
||||
XMLName xml.Name `xml:"RecognitionResult"`
|
||||
PornInfo *RecognitionInfo `xml:"PornInfo,omitempty"`
|
||||
TerroristInfo *RecognitionInfo `xml:"TerroristInfo,omitempty"`
|
||||
PoliticsInfo *RecognitionInfo `xml:"PoliticsInfo,omitempty"`
|
||||
AdsInfo *RecognitionInfo `xml:"AdsInfo,omitempty"`
|
||||
}
|
||||
type RecognitionInfo struct {
|
||||
Code int `xml:"Code,omitempty"`
|
||||
Msg string `xml:"Msg,omitempty"`
|
||||
HitFlag int `xml:"HitFlag,omitempty"`
|
||||
Score int `xml:"Score,omitempty"`
|
||||
Label string `xml:"Label,omitempty"`
|
||||
Count int `xml:"Count,omitempty"`
|
||||
}
|
||||
|
||||
// 图片审核 https://cloud.tencent.com/document/product/460/37318
|
||||
func (s *CIService) ImageRecognition(ctx context.Context, name string, opt *ImageRecognitionOptions) (*ImageRecognitionResult, *Response, error) {
|
||||
if opt != nil && opt.CIProcess == "" {
|
||||
opt.CIProcess = "sensitive-content-recognition"
|
||||
}
|
||||
var res ImageRecognitionResult
|
||||
sendOpt := sendOptions{
|
||||
baseURL: s.client.BaseURL.BucketURL,
|
||||
uri: "/" + encodeURIComponent(name),
|
||||
method: http.MethodGet,
|
||||
optQuery: opt,
|
||||
result: &res,
|
||||
}
|
||||
resp, err := s.client.send(ctx, &sendOpt)
|
||||
return &res, resp, err
|
||||
}
|
||||
|
||||
type PutVideoAuditingJobOptions struct {
|
||||
XMLName xml.Name `xml:"Request"`
|
||||
InputObject string `xml:"Input>Object"`
|
||||
Conf *VideoAuditingJobConf `xml:"Conf"`
|
||||
}
|
||||
type VideoAuditingJobConf struct {
|
||||
DetectType string `xml:",omitempty"`
|
||||
Snapshot *PutVideoAuditingJobSnapshot `xml:",omitempty"`
|
||||
Callback string `xml:",omitempty"`
|
||||
}
|
||||
type PutVideoAuditingJobSnapshot struct {
|
||||
Mode string `xml:",omitempty"`
|
||||
Count int `xml:",omitempty"`
|
||||
TimeInterval float32 `xml:",omitempty"`
|
||||
Start float32 `xml:",omitempty"`
|
||||
}
|
||||
|
||||
type PutVideoAuditingJobResult struct {
|
||||
XMLName xml.Name `xml:"Response"`
|
||||
JobsDetail struct {
|
||||
JobId string `xml:"JobId,omitempty"`
|
||||
State string `xml:"State,omitempty"`
|
||||
CreationTime string `xml:"CreationTime,omitempty"`
|
||||
Object string `xml:"Object,omitempty"`
|
||||
} `xml:"JobsDetail,omitempty"`
|
||||
}
|
||||
|
||||
func (s *CIService) PutVideoAuditingJob(ctx context.Context, opt *PutVideoAuditingJobOptions) (*PutVideoAuditingJobResult, *Response, error) {
|
||||
var res PutVideoAuditingJobResult
|
||||
sendOpt := sendOptions{
|
||||
baseURL: s.client.BaseURL.CIURL,
|
||||
uri: "/video/auditing",
|
||||
method: http.MethodPost,
|
||||
body: opt,
|
||||
result: &res,
|
||||
}
|
||||
resp, err := s.client.send(ctx, &sendOpt)
|
||||
return &res, resp, err
|
||||
}
|
||||
|
||||
type GetVideoAuditingJobResult struct {
|
||||
XMLName xml.Name `xml:"Response"`
|
||||
JobsDetail *VideoAuditingJobDetail `xml:",omitempty"`
|
||||
NonExistJobIds string `xml:",omitempty"`
|
||||
}
|
||||
type VideoAuditingJobDetail struct {
|
||||
Code string `xml:",omitempty"`
|
||||
Message string `xml:",omitempty"`
|
||||
JobId string `xml:",omitempty"`
|
||||
State string `xml:",omitempty"`
|
||||
CreationTime string `xml:",omitempty"`
|
||||
Object string `xml:",omitempty"`
|
||||
SnapshotCount string `xml:",omitempty"`
|
||||
result int `xml:",omitempty"`
|
||||
PornInfo *RecognitionInfo `xml:",omitempty"`
|
||||
TerrorismInfo *RecognitionInfo `xml:",omitempty"`
|
||||
PoliticsInfo *RecognitionInfo `xml:",omitempty"`
|
||||
AdsInfo *RecognitionInfo `xml:",omitempty"`
|
||||
Snapshot *GetVideoAuditingJobSnapshot `xml:",omitempty"`
|
||||
}
|
||||
type GetVideoAuditingJobSnapshot struct {
|
||||
Url string `xml:",omitempty"`
|
||||
PornInfo *RecognitionInfo `xml:",omitempty"`
|
||||
TerrorismInfo *RecognitionInfo `xml:",omitempty"`
|
||||
PoliticsInfo *RecognitionInfo `xml:",omitempty"`
|
||||
AdsInfo *RecognitionInfo `xml:",omitempty"`
|
||||
}
|
||||
|
||||
func (s *CIService) GetVideoAuditingJob(ctx context.Context, jobid string) (*GetVideoAuditingJobResult, *Response, error) {
|
||||
var res GetVideoAuditingJobResult
|
||||
sendOpt := sendOptions{
|
||||
baseURL: s.client.BaseURL.CIURL,
|
||||
uri: "/video/auditing/" + jobid,
|
||||
method: http.MethodGet,
|
||||
result: &res,
|
||||
}
|
||||
resp, err := s.client.send(ctx, &sendOpt)
|
||||
return &res, resp, err
|
||||
}
|
||||
|
||||
63
cos.go
63
cos.go
@@ -11,6 +11,7 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"strconv"
|
||||
@@ -21,7 +22,7 @@ import (
|
||||
|
||||
const (
|
||||
// Version current go sdk version
|
||||
Version = "0.7.5"
|
||||
Version = "0.7.12"
|
||||
userAgent = "cos-go-sdk-v5/" + Version
|
||||
contentTypeXML = "application/xml"
|
||||
defaultServiceBaseURL = "http://service.cos.myqcloud.com"
|
||||
@@ -41,6 +42,8 @@ type BaseURL struct {
|
||||
ServiceURL *url.URL
|
||||
// 访问 job API 的基础 URL (不包含 path 部分): http://example.com
|
||||
BatchURL *url.URL
|
||||
// 访问 CI 的基础 URL
|
||||
CIURL *url.URL
|
||||
}
|
||||
|
||||
// NewBucketURL 生成 BaseURL 所需的 BucketURL
|
||||
@@ -81,6 +84,7 @@ type Client struct {
|
||||
Bucket *BucketService
|
||||
Object *ObjectService
|
||||
Batch *BatchService
|
||||
CI *CIService
|
||||
}
|
||||
|
||||
type service struct {
|
||||
@@ -98,6 +102,7 @@ func NewClient(uri *BaseURL, httpClient *http.Client) *Client {
|
||||
baseURL.BucketURL = uri.BucketURL
|
||||
baseURL.ServiceURL = uri.ServiceURL
|
||||
baseURL.BatchURL = uri.BatchURL
|
||||
baseURL.CIURL = uri.CIURL
|
||||
}
|
||||
if baseURL.ServiceURL == nil {
|
||||
baseURL.ServiceURL, _ = url.Parse(defaultServiceBaseURL)
|
||||
@@ -113,6 +118,7 @@ func NewClient(uri *BaseURL, httpClient *http.Client) *Client {
|
||||
c.Bucket = (*BucketService)(&c.common)
|
||||
c.Object = (*ObjectService)(&c.common)
|
||||
c.Batch = (*BatchService)(&c.common)
|
||||
c.CI = (*CIService)(&c.common)
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -243,9 +249,6 @@ func (c *Client) send(ctx context.Context, opt *sendOptions) (resp *Response, er
|
||||
}
|
||||
|
||||
resp, err = c.doAPI(ctx, req, opt.result, !opt.disableCloseBody)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -329,6 +332,8 @@ type ACLHeaderOptions struct {
|
||||
XCosGrantRead string `header:"x-cos-grant-read,omitempty" url:"-" xml:"-"`
|
||||
XCosGrantWrite string `header:"x-cos-grant-write,omitempty" url:"-" xml:"-"`
|
||||
XCosGrantFullControl string `header:"x-cos-grant-full-control,omitempty" url:"-" xml:"-"`
|
||||
XCosGrantReadACP string `header:"x-cos-grant-read-acp,omitempty" url:"-" xml:"-"`
|
||||
XCosGrantWriteACP string `header:"x-cos-grant-write-acp,omitempty" url:"-" xml:"-"`
|
||||
}
|
||||
|
||||
// ACLGrantee is the param of ACLGrant
|
||||
@@ -353,3 +358,53 @@ type ACLXml struct {
|
||||
Owner *Owner
|
||||
AccessControlList []ACLGrant `xml:"AccessControlList>Grant,omitempty"`
|
||||
}
|
||||
|
||||
func decodeACL(resp *Response, res *ACLXml) {
|
||||
ItemMap := map[string]string{
|
||||
"ACL": "x-cos-acl",
|
||||
"READ": "x-cos-grant-read",
|
||||
"WRITE": "x-cos-grant-write",
|
||||
"READ_ACP": "x-cos-grant-read-acp",
|
||||
"WRITE_ACP": "x-cos-grant-write-acp",
|
||||
"FULL_CONTROL": "x-cos-grant-full-control",
|
||||
}
|
||||
publicACL := make(map[string]int)
|
||||
resACL := make(map[string][]string)
|
||||
for _, item := range res.AccessControlList {
|
||||
if item.Grantee == nil {
|
||||
continue
|
||||
}
|
||||
if item.Grantee.ID == "qcs::cam::anyone:anyone" || item.Grantee.URI == "http://cam.qcloud.com/groups/global/AllUsers" {
|
||||
publicACL[item.Permission] = 1
|
||||
} else if item.Grantee.ID != res.Owner.ID {
|
||||
resACL[item.Permission] = append(resACL[item.Permission], "id=\""+item.Grantee.ID+"\"")
|
||||
}
|
||||
}
|
||||
if publicACL["FULL_CONTROL"] == 1 || (publicACL["READ"] == 1 && publicACL["WRITE"] == 1) {
|
||||
resACL["ACL"] = []string{"public-read-write"}
|
||||
} else if publicACL["READ"] == 1 {
|
||||
resACL["ACL"] = []string{"public-read"}
|
||||
} else {
|
||||
resACL["ACL"] = []string{"private"}
|
||||
}
|
||||
|
||||
for item, header := range ItemMap {
|
||||
if len(resp.Header.Get(header)) > 0 || len(resACL[item]) == 0 {
|
||||
continue
|
||||
}
|
||||
resp.Header.Set(header, uniqueGrantID(resACL[item]))
|
||||
}
|
||||
}
|
||||
|
||||
func uniqueGrantID(grantIDs []string) string {
|
||||
res := []string{}
|
||||
filter := make(map[string]int)
|
||||
for _, id := range grantIDs {
|
||||
if filter[id] != 0 {
|
||||
continue
|
||||
}
|
||||
filter[id] = 1
|
||||
res = append(res, id)
|
||||
}
|
||||
return strings.Join(res, ",")
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ func setup() {
|
||||
server = httptest.NewServer(mux)
|
||||
|
||||
u, _ := url.Parse(server.URL)
|
||||
client = NewClient(&BaseURL{u, u, u}, nil)
|
||||
client = NewClient(&BaseURL{u, u, u, u}, nil)
|
||||
}
|
||||
|
||||
// teardown closes the test HTTP server.
|
||||
|
||||
@@ -56,14 +56,14 @@ const (
|
||||
kBucket = "cosgosdktest-1259654469"
|
||||
kRegion = "ap-guangzhou"
|
||||
|
||||
// 跨区域复制需要的目标存储桶,地域不能与kBucket存储桶相同。
|
||||
// 跨区域复制需要的目标存储桶,地域不能与kBucket存储桶相同, 目的存储桶需要开启多版本
|
||||
kRepBucket = "cosgosdkreptest"
|
||||
kRepRegion = "ap-chengdu"
|
||||
|
||||
// Batch测试需要的源存储桶和目标存储桶,目前只在成都、重庆地域公测
|
||||
kBatchBucket = "testcd-1259654469"
|
||||
kTargetBatchBucket = "cosgosdkreptest-1259654469" //复用了存储桶
|
||||
kBatchRegion = "ap-chengdu"
|
||||
kBatchBucket = "cosgosdktest-1259654469"
|
||||
kTargetBatchBucket = "cosgosdktest-1259654469" //复用了存储桶
|
||||
kBatchRegion = "ap-guangzhou"
|
||||
)
|
||||
|
||||
func (s *CosTestSuite) SetupSuite() {
|
||||
@@ -85,9 +85,9 @@ func (s *CosTestSuite) SetupSuite() {
|
||||
s.Region = p[2]
|
||||
|
||||
// Bucket name
|
||||
pp := strings.Split(p[0], "-")
|
||||
s.Bucket = pp[0]
|
||||
s.Appid = pp[1]
|
||||
pi := strings.LastIndex(p[0], "-")
|
||||
s.Bucket = p[0][:pi]
|
||||
s.Appid = p[0][pi+1:]
|
||||
|
||||
ib := &cos.BaseURL{BucketURL: bucketurl, BatchURL: batchurl}
|
||||
s.Client = cos.NewClient(ib, &http.Client{
|
||||
@@ -184,6 +184,15 @@ func (s *CosTestSuite) TestGetBucket() {
|
||||
assert.Nil(s.T(), err, "GetBucket Failed")
|
||||
}
|
||||
|
||||
func (s *CosTestSuite) TestGetObjectVersions() {
|
||||
opt := &cos.BucketGetObjectVersionsOptions{
|
||||
Prefix: "中文",
|
||||
MaxKeys: 3,
|
||||
}
|
||||
_, _, err := s.Client.Bucket.GetObjectVersions(context.Background(), opt)
|
||||
assert.Nil(s.T(), err, "GetObjectVersions Failed")
|
||||
}
|
||||
|
||||
func (s *CosTestSuite) TestGetBucketLocation() {
|
||||
v, _, err := s.Client.Bucket.GetLocation(context.Background())
|
||||
assert.Nil(s.T(), err, "GetLocation Failed")
|
||||
@@ -803,7 +812,7 @@ func (s *CosTestSuite) TestBatch() {
|
||||
assert.Equal(s.T(), res3.Priority, 3, "priority not right")
|
||||
|
||||
// 等待状态变成Suspended
|
||||
for i := 0; i < 10; i = i + 1 {
|
||||
for i := 0; i < 50; i = i + 1 {
|
||||
res, _, err := client.Batch.DescribeJob(context.Background(), jobid, headers)
|
||||
assert.Nil(s.T(), err, "describe job Failed")
|
||||
assert.Equal(s.T(), res2.Job.ConfirmationRequired, "true", "ConfirmationRequired not right")
|
||||
|
||||
38
example/bucket/getLogging.go
Normal file
38
example/bucket/getLogging.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://bj-1259654469.cos.ap-beijing.myqcloud.com")
|
||||
b := &cos.BaseURL{
|
||||
BucketURL: u,
|
||||
}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
v, _, err := c.Bucket.GetLogging(context.Background())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("%+v\n", v.LoggingEnabled)
|
||||
}
|
||||
64
example/bucket/getObjectVersion.go
Normal file
64
example/bucket/getObjectVersion.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"net/url"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{
|
||||
BucketURL: u,
|
||||
}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
opt := &cos.BucketGetObjectVersionsOptions{
|
||||
Delimiter: "/",
|
||||
MaxKeys: 1,
|
||||
}
|
||||
v, _, err := c.Bucket.GetObjectVersions(context.Background(), opt)
|
||||
log_status(err)
|
||||
|
||||
for _, c := range v.Version {
|
||||
fmt.Printf("%v, %v, %v\n", c.Key, c.Size, c.IsLatest)
|
||||
}
|
||||
|
||||
}
|
||||
64
example/bucket/intelligenttiering.go
Normal file
64
example/bucket/intelligenttiering.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("Code: %v\n", e.Code)
|
||||
fmt.Printf("Message: %v\n", e.Message)
|
||||
fmt.Printf("Resource: %v\n", e.Resource)
|
||||
fmt.Printf("RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{
|
||||
BucketURL: u,
|
||||
}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: false,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: false,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
opt := &cos.BucketPutIntelligentTieringOptions {
|
||||
Status: "Enabled",
|
||||
Transition: &cos.BucketIntelligentTieringTransition {
|
||||
Days: 30,
|
||||
},
|
||||
}
|
||||
_, err := c.Bucket.PutIntelligentTiering(context.Background(), opt)
|
||||
log_status(err)
|
||||
res, _, err := c.Bucket.GetIntelligentTiering(context.Background())
|
||||
log_status(err)
|
||||
fmt.Printf("%+v\n", res)
|
||||
fmt.Printf("%+v\n", res.Status)
|
||||
fmt.Printf("%+v\n", res.Transition.Days)
|
||||
}
|
||||
92
example/bucket/origin.go
Normal file
92
example/bucket/origin.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("Code: %v\n", e.Code)
|
||||
fmt.Printf("Message: %v\n", e.Message)
|
||||
fmt.Printf("Resource: %v\n", e.Resource)
|
||||
fmt.Printf("RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{
|
||||
BucketURL: u,
|
||||
}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
opt := &cos.BucketPutOriginOptions{
|
||||
Rule: []cos.BucketOriginRule{
|
||||
{
|
||||
OriginType: "Proxy",
|
||||
OriginCondition: &cos.BucketOriginCondition{
|
||||
HTTPStatusCode: "404",
|
||||
Prefix: "",
|
||||
},
|
||||
OriginParameter: &cos.BucketOriginParameter{
|
||||
Protocol: "FOLLOW",
|
||||
FollowQueryString: true,
|
||||
HttpHeader: &cos.BucketOriginHttpHeader{
|
||||
NewHttpHeaders: []cos.OriginHttpHeader{
|
||||
{
|
||||
Key: "x-cos-ContentType",
|
||||
Value: "csv",
|
||||
},
|
||||
},
|
||||
FollowHttpHeaders: []cos.OriginHttpHeader{
|
||||
{
|
||||
Key: "Content-Type",
|
||||
},
|
||||
},
|
||||
},
|
||||
FollowRedirection: true,
|
||||
},
|
||||
OriginInfo: &cos.BucketOriginInfo{
|
||||
HostInfo: "examplebucket-1250000000.cos.ap-shanghai.myqcloud.com",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
_, err := c.Bucket.PutOrigin(context.Background(), opt)
|
||||
log_status(err)
|
||||
res, _, err := c.Bucket.GetOrigin(context.Background())
|
||||
log_status(err)
|
||||
fmt.Printf("%+v\n", res)
|
||||
fmt.Printf("%+v\n", res.Rule)
|
||||
_, err = c.Bucket.DeleteOrigin(context.Background())
|
||||
log_status(err)
|
||||
}
|
||||
@@ -12,6 +12,25 @@ import (
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("http://alanbj-1251668577.cos.ap-beijing.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
@@ -37,8 +56,6 @@ func main() {
|
||||
v, _, err := c.Object.MultiUpload(
|
||||
context.Background(), "test/gomulput1G", "./test1G", opt,
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
fmt.Println(v)
|
||||
}
|
||||
@@ -12,6 +12,25 @@ import (
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
@@ -30,14 +49,10 @@ func main() {
|
||||
|
||||
name := "test_multipart.txt"
|
||||
v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
fmt.Printf("%s\n", v.UploadID)
|
||||
|
||||
resp, err := c.Object.AbortMultipartUpload(context.Background(), name, v.UploadID)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
fmt.Printf("%s\n", resp.Status)
|
||||
}
|
||||
|
||||
63
example/object/ci_image_process.go
Normal file
63
example/object/ci_image_process.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
// Notice when put a large file and set need the request body, might happend out of memory error.
|
||||
RequestBody: false,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
opt := &cos.ImageProcessOptions{
|
||||
IsPicInfo: 1,
|
||||
Rules: []cos.PicOperationsRules{
|
||||
{
|
||||
FileId: "format.jpg",
|
||||
Rule: "imageView2/format/png",
|
||||
},
|
||||
},
|
||||
}
|
||||
name := "test.jpg"
|
||||
res, _, err := c.CI.ImageProcess(context.Background(), name, opt)
|
||||
log_status(err)
|
||||
fmt.Printf("%+v\n", res)
|
||||
}
|
||||
56
example/object/ci_image_recognition.go
Normal file
56
example/object/ci_image_recognition.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
opt := &cos.ImageRecognitionOptions{
|
||||
DetectType: "porn,terrorist,politics",
|
||||
}
|
||||
|
||||
name := "test.jpg"
|
||||
res, _, err := c.CI.ImageRecognition(context.Background(), name, opt)
|
||||
log_status(err)
|
||||
fmt.Printf("%+v\n", res)
|
||||
}
|
||||
@@ -17,15 +17,15 @@ func log_status(err error) {
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("Resource is not existed")
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("Code: %v\n", e.Code)
|
||||
fmt.Printf("Message: %v\n", e.Message)
|
||||
fmt.Printf("Resource: %v\n", e.Resource)
|
||||
fmt.Printf("RequestId: %v\n", e.RequestID)
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
71
example/object/ci_video_auditing_job.go
Normal file
71
example/object/ci_video_auditing_job.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
bu, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
|
||||
cu, _ := url.Parse("https://test-1259654469.ci.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: bu, CIURL: cu}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
opt := &cos.PutVideoAuditingJobOptions{
|
||||
InputObject: "demo.mp4",
|
||||
Conf: &cos.VideoAuditingJobConf{
|
||||
DetectType: "Porn,Terrorism,Politics,Ads",
|
||||
Snapshot: &cos.PutVideoAuditingJobSnapshot{
|
||||
Mode: "Interval",
|
||||
Start: 0.5,
|
||||
TimeInterval: 50.5,
|
||||
Count: 100,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
res, _, err := c.CI.PutVideoAuditingJob(context.Background(), opt)
|
||||
log_status(err)
|
||||
fmt.Printf("%+v\n", res)
|
||||
|
||||
time.Sleep(3 * time.Second)
|
||||
res2, _, err := c.CI.GetVideoAuditingJob(context.Background(), res.JobsDetail.JobId)
|
||||
log_status(err)
|
||||
fmt.Printf("%+v\n", res2)
|
||||
}
|
||||
@@ -14,11 +14,28 @@ import (
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func initUpload(c *cos.Client, name string) *cos.InitiateMultipartUploadResult {
|
||||
v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
fmt.Printf("%#v\n", v)
|
||||
return v
|
||||
}
|
||||
@@ -27,7 +44,7 @@ func uploadPart(c *cos.Client, name string, uploadID string, blockSize, n int) s
|
||||
|
||||
b := make([]byte, blockSize)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
panic(err)
|
||||
log_status(err)
|
||||
}
|
||||
s := fmt.Sprintf("%X", b)
|
||||
f := strings.NewReader(s)
|
||||
@@ -35,15 +52,13 @@ func uploadPart(c *cos.Client, name string, uploadID string, blockSize, n int) s
|
||||
resp, err := c.Object.UploadPart(
|
||||
context.Background(), name, uploadID, n, f, nil,
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
fmt.Printf("%s\n", resp.Status)
|
||||
return resp.Header.Get("Etag")
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
|
||||
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
@@ -86,9 +101,7 @@ func main() {
|
||||
v, resp, err := c.Object.CompleteMultipartUpload(
|
||||
context.Background(), name, uploadID, opt,
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
fmt.Printf("%s\n", resp.Status)
|
||||
fmt.Printf("%#v\n", v)
|
||||
fmt.Printf("%s\n", v.Location)
|
||||
|
||||
@@ -16,8 +16,27 @@ import (
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
|
||||
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
@@ -37,23 +56,18 @@ func main() {
|
||||
f := strings.NewReader(expected)
|
||||
|
||||
_, err := c.Object.Put(context.Background(), source, f, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
|
||||
soruceURL := fmt.Sprintf("%s/%s", u.Host, source)
|
||||
dest := fmt.Sprintf("test/objectMove_%d.go", time.Now().Nanosecond())
|
||||
//opt := &cos.ObjectCopyOptions{}
|
||||
res, _, err := c.Object.Copy(context.Background(), dest, soruceURL, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
fmt.Printf("%+v\n\n", res)
|
||||
|
||||
resp, err := c.Object.Get(context.Background(), dest, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
|
||||
bs, _ := ioutil.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
result := string(bs)
|
||||
|
||||
@@ -13,11 +13,28 @@ import (
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func initUpload(c *cos.Client, name string) *cos.InitiateMultipartUploadResult {
|
||||
v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
fmt.Printf("%#v\n", v)
|
||||
return v
|
||||
}
|
||||
@@ -46,9 +63,7 @@ func main() {
|
||||
opt := &cos.ObjectCopyPartOptions{}
|
||||
res, _, err := c.Object.CopyPart(
|
||||
context.Background(), name, uploadID, 1, sourceUrl, opt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
fmt.Println("ETag:", res.ETag)
|
||||
|
||||
completeOpt := &cos.CompleteMultipartUploadOptions{}
|
||||
@@ -59,9 +74,7 @@ func main() {
|
||||
v, resp, err := c.Object.CompleteMultipartUpload(
|
||||
context.Background(), name, uploadID, completeOpt,
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
fmt.Printf("%s\n", resp.Status)
|
||||
fmt.Printf("%#v\n", v)
|
||||
fmt.Printf("%s\n", v.Location)
|
||||
|
||||
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
@@ -11,6 +12,25 @@ import (
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
@@ -30,7 +50,5 @@ func main() {
|
||||
name := "test/objectPut.go"
|
||||
|
||||
_, err := c.Object.Delete(context.Background(), name, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
}
|
||||
|
||||
@@ -18,10 +18,29 @@ import (
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func genBigData(blockSize int) []byte {
|
||||
b := make([]byte, blockSize)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
panic(err)
|
||||
log_status(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
@@ -46,7 +65,7 @@ func uploadMulti(c *cos.Client) []string {
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
|
||||
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
@@ -89,9 +108,7 @@ func main() {
|
||||
})
|
||||
|
||||
v, _, err := c.Object.DeleteMulti(ctx, opt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
|
||||
for _, x := range v.DeletedObjects {
|
||||
fmt.Printf("deleted %s\n", x.Key)
|
||||
|
||||
@@ -13,6 +13,25 @@ import (
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
@@ -32,31 +51,26 @@ func main() {
|
||||
// Case1 Download object into ReadCloser(). the body needs to be closed
|
||||
name := "test/hello.txt"
|
||||
resp, err := c.Object.Get(context.Background(), name, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
|
||||
bs, _ := ioutil.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
fmt.Printf("%s\n", string(bs))
|
||||
|
||||
// Case2 Download object to local file. the body needs to be closed
|
||||
fd, err := os.OpenFile("hello.txt", os.O_WRONLY|os.O_CREATE, 0660)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
|
||||
defer fd.Close()
|
||||
resp, err = c.Object.Get(context.Background(), name, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
|
||||
io.Copy(fd, resp.Body)
|
||||
resp.Body.Close()
|
||||
|
||||
// Case3 Download object to local file path
|
||||
_, err = c.Object.GetToFile(context.Background(), name, "hello_1.txt", nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
|
||||
// Case4 Download object with range header, can used to concurrent download
|
||||
opt := &cos.ObjectGetOptions{
|
||||
@@ -64,9 +78,7 @@ func main() {
|
||||
Range: "bytes=0-3",
|
||||
}
|
||||
resp, err = c.Object.Get(context.Background(), name, opt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
bs, _ = ioutil.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
fmt.Printf("%s\n", string(bs))
|
||||
|
||||
@@ -12,8 +12,27 @@ import (
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
|
||||
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
@@ -30,9 +49,7 @@ func main() {
|
||||
|
||||
name := "test/hello.txt"
|
||||
v, _, err := c.Object.GetACL(context.Background(), name)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
for _, a := range v.AccessControlList {
|
||||
fmt.Printf("%s, %s, %s\n", a.Grantee.Type, a.Grantee.ID, a.Permission)
|
||||
}
|
||||
|
||||
@@ -11,6 +11,25 @@ import (
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func upload(c *cos.Client, name string) {
|
||||
f := strings.NewReader("test")
|
||||
f = strings.NewReader("test xxx")
|
||||
@@ -35,10 +54,7 @@ func main() {
|
||||
upload(c, name)
|
||||
|
||||
resp, err := c.Object.Get(context.Background(), name, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return
|
||||
}
|
||||
log_status(err)
|
||||
bs, _ := ioutil.ReadAll(resp.Body)
|
||||
defer resp.Body.Close()
|
||||
fmt.Printf("%s\n", string(bs))
|
||||
|
||||
@@ -14,6 +14,25 @@ import (
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
ak := os.Getenv("COS_SECRETID")
|
||||
sk := os.Getenv("COS_SECRETKEY")
|
||||
@@ -38,22 +57,17 @@ func main() {
|
||||
|
||||
// Normal header way to get object
|
||||
resp, err := c.Object.Get(ctx, name, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
bs, _ := ioutil.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
|
||||
// Get presigned
|
||||
presignedURL, err := c.Object.GetPresignedURL(ctx, http.MethodGet, name, ak, sk, time.Hour, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
|
||||
// Get object by presinged url
|
||||
resp2, err := http.Get(presignedURL.String())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
bs2, _ := ioutil.ReadAll(resp2.Body)
|
||||
resp2.Body.Close()
|
||||
fmt.Printf("result2 is : %s\n", string(bs2))
|
||||
|
||||
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
@@ -11,6 +12,25 @@ import (
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
@@ -29,7 +49,5 @@ func main() {
|
||||
|
||||
name := "test/hello.txt"
|
||||
_, err := c.Object.Head(context.Background(), name, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
}
|
||||
|
||||
@@ -13,6 +13,25 @@ import (
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
@@ -31,8 +50,6 @@ func main() {
|
||||
|
||||
name := "test_multipart" + time.Now().Format(time.RFC3339)
|
||||
v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
fmt.Printf("%s\n", v.UploadID)
|
||||
}
|
||||
|
||||
@@ -14,11 +14,28 @@ import (
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func initUpload(c *cos.Client, name string) *cos.InitiateMultipartUploadResult {
|
||||
v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
fmt.Printf("%#v\n", v)
|
||||
return v
|
||||
}
|
||||
@@ -27,7 +44,7 @@ func uploadPart(c *cos.Client, name string, uploadID string, blockSize, n int) s
|
||||
|
||||
b := make([]byte, blockSize)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
panic(err)
|
||||
log_status(err)
|
||||
}
|
||||
s := fmt.Sprintf("%X", b)
|
||||
f := strings.NewReader(s)
|
||||
@@ -35,9 +52,7 @@ func uploadPart(c *cos.Client, name string, uploadID string, blockSize, n int) s
|
||||
resp, err := c.Object.UploadPart(
|
||||
context.Background(), name, uploadID, n, f, nil,
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
fmt.Printf("%s\n", resp.Status)
|
||||
return resp.Header.Get("Etag")
|
||||
}
|
||||
@@ -73,7 +88,7 @@ func main() {
|
||||
// }
|
||||
v, _, err := c.Object.ListParts(ctx, name, uploadID, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log_status(err)
|
||||
return
|
||||
}
|
||||
for _, p := range v.Parts {
|
||||
|
||||
98
example/object/list_uploads.go
Normal file
98
example/object/list_uploads.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func initUpload(c *cos.Client, name string) *cos.InitiateMultipartUploadResult {
|
||||
v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil)
|
||||
log_status(err)
|
||||
fmt.Printf("%#v\n", v)
|
||||
return v
|
||||
}
|
||||
|
||||
func uploadPart(c *cos.Client, name string, uploadID string, blockSize, n int) string {
|
||||
|
||||
b := make([]byte, blockSize)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
log_status(err)
|
||||
}
|
||||
s := fmt.Sprintf("%X", b)
|
||||
f := strings.NewReader(s)
|
||||
|
||||
resp, err := c.Object.UploadPart(
|
||||
context.Background(), name, uploadID, n, f, nil,
|
||||
)
|
||||
log_status(err)
|
||||
fmt.Printf("%s\n", resp.Status)
|
||||
return resp.Header.Get("Etag")
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("http://test-1259654469.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: false,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
name := "test/test_list_parts.go"
|
||||
up := initUpload(c, name)
|
||||
uploadID := up.UploadID
|
||||
blockSize := 1024 * 1024 * 3
|
||||
|
||||
for i := 1; i < 5; i++ {
|
||||
uploadPart(c, name, uploadID, blockSize, i)
|
||||
}
|
||||
opt := &cos.ObjectListUploadsOptions{
|
||||
Prefix: cos.EncodeURIComponent("test/test_list_parts"),
|
||||
MaxUploads: 100,
|
||||
}
|
||||
v, _, err := c.Object.ListUploads(context.Background(), opt)
|
||||
if err != nil {
|
||||
log_status(err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("%+v\n", v)
|
||||
for _, p := range v.Upload {
|
||||
fmt.Printf("%+v\n", p)
|
||||
fmt.Printf("%v, %v, %v\n", p.Key, p.UploadID, p.Initiated)
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
@@ -11,6 +12,25 @@ import (
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
@@ -33,7 +53,5 @@ func main() {
|
||||
AccessControlRequestMethod: "PUT",
|
||||
}
|
||||
_, err := c.Object.Options(context.Background(), name, opt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
}
|
||||
|
||||
44
example/object/presigned_url_with_token.go
Normal file
44
example/object/presigned_url_with_token.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
)
|
||||
|
||||
type URLToken struct {
|
||||
SessionToken string `url:"x-cos-security-token,omitempty" header:"-"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
// 替换成您的临时密钥
|
||||
tak := os.Getenv("COS_SECRETID")
|
||||
tsk := os.Getenv("COS_SECRETKEY")
|
||||
token := &URLToken{
|
||||
SessionToken: "<token>",
|
||||
}
|
||||
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
c := cos.NewClient(b, &http.Client{})
|
||||
|
||||
name := "exampleobject"
|
||||
ctx := context.Background()
|
||||
|
||||
// Get presigned
|
||||
presignedURL, err := c.Object.GetPresignedURL(ctx, http.MethodGet, name, tak, tsk, time.Hour, token)
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %v\n", err)
|
||||
return
|
||||
}
|
||||
// Get object by presinged url
|
||||
_, err = http.Get(presignedURL.String())
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %v\n", err)
|
||||
}
|
||||
fmt.Println(presignedURL.String())
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
@@ -12,8 +13,27 @@ import (
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
|
||||
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
@@ -34,9 +54,7 @@ func main() {
|
||||
f := strings.NewReader("test")
|
||||
|
||||
_, err := c.Object.Put(context.Background(), name, f, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
|
||||
// Case2 put object with the options
|
||||
name = "test/put_option.go"
|
||||
@@ -51,14 +69,9 @@ func main() {
|
||||
},
|
||||
}
|
||||
_, err = c.Object.Put(context.Background(), name, f, opt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
|
||||
// Case3 put object by local file path
|
||||
_, err = c.Object.PutFromFile(context.Background(), name, "./test", nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
log_status(err)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
@@ -11,6 +12,25 @@ import (
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
@@ -34,9 +54,7 @@ func main() {
|
||||
}
|
||||
name := "test/hello.txt"
|
||||
_, err := c.Object.PutACL(context.Background(), name, opt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
|
||||
// with body
|
||||
opt = &cos.ObjectPutACLOptions{
|
||||
@@ -58,7 +76,5 @@ func main() {
|
||||
}
|
||||
|
||||
_, err = c.Object.PutACL(context.Background(), name, opt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
@@ -10,6 +11,25 @@ import (
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
@@ -35,7 +55,5 @@ func main() {
|
||||
}
|
||||
name := "archivetest"
|
||||
_, err := c.Object.PostRestore(context.Background(), name, opt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
}
|
||||
|
||||
66
example/object/select.go
Normal file
66
example/object/select.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
// Notice when put a large file and set need the request body, might happend out of memory error.
|
||||
RequestBody: false,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: false,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
opt := &cos.ObjectSelectOptions{
|
||||
Expression: "Select * from COSObject",
|
||||
ExpressionType: "SQL",
|
||||
InputSerialization: &cos.SelectInputSerialization{
|
||||
JSON: &cos.JSONInputSerialization{
|
||||
Type: "DOCUMENT",
|
||||
},
|
||||
},
|
||||
OutputSerialization: &cos.SelectOutputSerialization{
|
||||
JSON: &cos.JSONOutputSerialization{
|
||||
RecordDelimiter: "\n",
|
||||
},
|
||||
},
|
||||
RequestProgress: "TRUE",
|
||||
}
|
||||
res, err := c.Object.Select(context.Background(), "test.json", opt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer res.Close()
|
||||
data, err := ioutil.ReadAll(res)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("data: %v\n", string(data))
|
||||
resp, _ := res.(*cos.ObjectSelectResponse)
|
||||
fmt.Printf("data: %+v\n", resp.Frame)
|
||||
|
||||
// Select to File
|
||||
_, err = c.Object.SelectToFile(context.Background(), "test.json", "./test.json", opt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
66
example/object/select_csv.go
Normal file
66
example/object/select_csv.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
// Notice when put a large file and set need the request body, might happend out of memory error.
|
||||
RequestBody: false,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: false,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
opt := &cos.ObjectSelectOptions{
|
||||
Expression: "Select * from COSObject",
|
||||
ExpressionType: "SQL",
|
||||
InputSerialization: &cos.SelectInputSerialization{
|
||||
CSV: &cos.CSVInputSerialization{
|
||||
FileHeaderInfo: "IGNORE",
|
||||
},
|
||||
},
|
||||
OutputSerialization: &cos.SelectOutputSerialization{
|
||||
CSV: &cos.CSVOutputSerialization{
|
||||
RecordDelimiter: "\n",
|
||||
},
|
||||
},
|
||||
RequestProgress: "TRUE",
|
||||
}
|
||||
res, err := c.Object.Select(context.Background(), "test.csv", opt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer res.Close()
|
||||
data, err := ioutil.ReadAll(res)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("data: %v\n", string(data))
|
||||
resp, _ := res.(*cos.ObjectSelectResponse)
|
||||
fmt.Printf("data: %+v\n", resp.Frame)
|
||||
|
||||
// Select To File
|
||||
_, err = c.Object.SelectToFile(context.Background(), "test.csv", "./test.csv", opt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
81
example/object/sse_c.go
Normal file
81
example/object/sse_c.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://testcd-1259654469.cos.ap-chengdu.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
// Notice when put a large file and set need the request body, might happend out of memory error.
|
||||
RequestBody: false,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
opt := &cos.ObjectPutOptions{
|
||||
ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
|
||||
ContentType: "text/html",
|
||||
XCosSSECustomerAglo: "AES256",
|
||||
XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
|
||||
XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
|
||||
},
|
||||
ACLHeaderOptions: &cos.ACLHeaderOptions{},
|
||||
}
|
||||
name := "PutFromGoWithSSE-C"
|
||||
content := "Put Object From Go With SSE-C"
|
||||
f := strings.NewReader(content)
|
||||
_, err := c.Object.Put(context.Background(), name, f, opt)
|
||||
log_status(err)
|
||||
|
||||
getopt := &cos.ObjectGetOptions{
|
||||
XCosSSECustomerAglo: "AES256",
|
||||
XCosSSECustomerKey: "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=",
|
||||
XCosSSECustomerKeyMD5: "U5L61r7jcwdNvT7frmUG8g==",
|
||||
}
|
||||
var resp *cos.Response
|
||||
resp, err = c.Object.Get(context.Background(), name, getopt)
|
||||
log_status(err)
|
||||
|
||||
bodyBytes, _ := ioutil.ReadAll(resp.Body)
|
||||
bodyContent := string(bodyBytes)
|
||||
if bodyContent != content {
|
||||
log_status(errors.New("Content inconsistency"))
|
||||
}
|
||||
}
|
||||
75
example/object/sse_cos.go
Normal file
75
example/object/sse_cos.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
// Notice when put a large file and set need the request body, might happend out of memory error.
|
||||
RequestBody: false,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
opt := &cos.ObjectPutOptions{
|
||||
ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
|
||||
ContentType: "text/html",
|
||||
XCosServerSideEncryption: "AES256",
|
||||
},
|
||||
ACLHeaderOptions: &cos.ACLHeaderOptions{},
|
||||
}
|
||||
name := "PutFromGoWithSSE-COS"
|
||||
content := "Put Object From Go With SSE-COS"
|
||||
f := strings.NewReader(content)
|
||||
_, err := c.Object.Put(context.Background(), name, f, opt)
|
||||
log_status(err)
|
||||
|
||||
getopt := &cos.ObjectGetOptions{}
|
||||
var resp *cos.Response
|
||||
resp, err = c.Object.Get(context.Background(), name, getopt)
|
||||
log_status(err)
|
||||
|
||||
bodyBytes, _ := ioutil.ReadAll(resp.Body)
|
||||
bodyContent := string(bodyBytes)
|
||||
if bodyContent != content {
|
||||
log_status(errors.New("Content inconsistency"))
|
||||
}
|
||||
}
|
||||
75
example/object/tagging.go
Normal file
75
example/object/tagging.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{
|
||||
BucketURL: u,
|
||||
}
|
||||
c := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
SecretID: os.Getenv("COS_SECRETID"),
|
||||
SecretKey: os.Getenv("COS_SECRETKEY"),
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
name := "test"
|
||||
|
||||
opt := &cos.ObjectPutTaggingOptions{
|
||||
TagSet: []cos.ObjectTaggingTag{
|
||||
{
|
||||
Key: "test_k2",
|
||||
Value: "test_v2",
|
||||
},
|
||||
{
|
||||
Key: "test_k3",
|
||||
Value: "test_v3",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
_, err := c.Object.PutTagging(context.Background(), name, opt)
|
||||
log_status(err)
|
||||
|
||||
res, _, err := c.Object.GetTagging(context.Background(), name)
|
||||
log_status(err)
|
||||
fmt.Printf("%v\n", res.TagSet)
|
||||
|
||||
_, err = c.Object.DeleteTagging(context.Background(), name)
|
||||
log_status(err)
|
||||
}
|
||||
@@ -12,6 +12,25 @@ import (
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1259654469.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
@@ -33,8 +52,6 @@ func main() {
|
||||
v, _, err := c.Object.Upload(
|
||||
context.Background(), "gomulput1G", "./test1G", nil,
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
fmt.Println(v)
|
||||
}
|
||||
|
||||
@@ -13,6 +13,25 @@ import (
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
u, _ := url.Parse("https://test-1253846586.cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
@@ -32,11 +51,13 @@ func main() {
|
||||
name := "test/uploadFile.go"
|
||||
f, err := os.Open(os.Args[0])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log_status(err)
|
||||
return
|
||||
}
|
||||
s, err := f.Stat()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log_status(err)
|
||||
return
|
||||
}
|
||||
fmt.Println(s.Size())
|
||||
opt := &cos.ObjectPutOptions{
|
||||
@@ -47,7 +68,5 @@ func main() {
|
||||
//opt.ContentLength = int(s.Size())
|
||||
|
||||
_, err = c.Object.Put(context.Background(), name, f, opt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
}
|
||||
|
||||
@@ -14,11 +14,28 @@ import (
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
)
|
||||
|
||||
func log_status(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if cos.IsNotFoundError(err) {
|
||||
// WARN
|
||||
fmt.Println("WARN: Resource is not existed")
|
||||
} else if e, ok := cos.IsCOSError(err); ok {
|
||||
fmt.Printf("ERROR: Code: %v\n", e.Code)
|
||||
fmt.Printf("ERROR: Message: %v\n", e.Message)
|
||||
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
|
||||
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
|
||||
// ERROR
|
||||
} else {
|
||||
fmt.Printf("ERROR: %v\n", err)
|
||||
// ERROR
|
||||
}
|
||||
}
|
||||
|
||||
func initUpload(c *cos.Client, name string) *cos.InitiateMultipartUploadResult {
|
||||
v, _, err := c.Object.InitiateMultipartUpload(context.Background(), name, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
fmt.Printf("%#v\n", v)
|
||||
return v
|
||||
}
|
||||
@@ -47,7 +64,5 @@ func main() {
|
||||
_, err := c.Object.UploadPart(
|
||||
context.Background(), name, uploadID, 1, f, nil,
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
log_status(err)
|
||||
}
|
||||
|
||||
96
example/sts/sts_v3.go
Normal file
96
example/sts/sts_v3.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/tencentyun/cos-go-sdk-v5"
|
||||
"github.com/tencentyun/cos-go-sdk-v5/debug"
|
||||
"github.com/tencentyun/qcloud-cos-sts-sdk/go"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
appid := "1259654469"
|
||||
bucket := "test-1259654469"
|
||||
c := sts.NewClient(
|
||||
os.Getenv("COS_SECRETID"),
|
||||
os.Getenv("COS_SECRETKEY"),
|
||||
nil,
|
||||
)
|
||||
opt := &sts.CredentialOptions{
|
||||
DurationSeconds: int64(time.Hour.Seconds()),
|
||||
Region: "ap-guangzhou",
|
||||
Policy: &sts.CredentialPolicy{
|
||||
Statement: []sts.CredentialPolicyStatement{
|
||||
{
|
||||
Action: []string{
|
||||
"name/cos:PostObject",
|
||||
"name/cos:PutObject",
|
||||
"name/cos:GetObject",
|
||||
},
|
||||
Effect: "allow",
|
||||
Resource: []string{
|
||||
//这里改成允许的路径前缀,可以根据自己网站的用户登录态判断允许上传的具体路径,例子: a.jpg 或者 a/* 或者 * (使用通配符*存在重大安全风险, 请谨慎评估使用)
|
||||
"qcs::cos:ap-guangzhou:uid/" + appid + ":" + bucket + "/exampleobject",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
res, err := c.GetCredential(opt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("%+v\n", res.Credentials)
|
||||
|
||||
//获取临时ak、sk、token
|
||||
tAk := res.Credentials.TmpSecretID
|
||||
tSk := res.Credentials.TmpSecretKey
|
||||
token := res.Credentials.SessionToken
|
||||
|
||||
u, _ := url.Parse("https://" + bucket + ".cos.ap-guangzhou.myqcloud.com")
|
||||
b := &cos.BaseURL{BucketURL: u}
|
||||
client := cos.NewClient(b, &http.Client{
|
||||
Transport: &cos.AuthorizationTransport{
|
||||
// 使用临时密钥
|
||||
SecretID: tAk,
|
||||
SecretKey: tSk,
|
||||
SessionToken: token,
|
||||
Transport: &debug.DebugRequestTransport{
|
||||
RequestHeader: true,
|
||||
RequestBody: true,
|
||||
ResponseHeader: true,
|
||||
ResponseBody: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
name := "exampleobject"
|
||||
f := strings.NewReader("test")
|
||||
|
||||
_, err = client.Object.Put(context.Background(), name, f, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
name = "exampleobject"
|
||||
f = strings.NewReader("test xxx")
|
||||
optc := &cos.ObjectPutOptions{
|
||||
ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
|
||||
ContentType: "text/html",
|
||||
},
|
||||
ACLHeaderOptions: &cos.ACLHeaderOptions{
|
||||
//XCosACL: "public-read",
|
||||
XCosACL: "private",
|
||||
},
|
||||
}
|
||||
_, err = client.Object.Put(context.Background(), name, f, optc)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
}
|
||||
31
helper.go
31
helper.go
@@ -6,6 +6,7 @@ import (
|
||||
"crypto/sha1"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// 计算 md5 或 sha1 时的分块大小
|
||||
@@ -44,7 +45,7 @@ func cloneRequest(r *http.Request) *http.Request {
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
|
||||
//
|
||||
// http://www.ecma-international.org/ecma-262/6.0/#sec-uri-syntax-and-semantics
|
||||
func encodeURIComponent(s string) string {
|
||||
func encodeURIComponent(s string, excluded ...[]byte) string {
|
||||
var b bytes.Buffer
|
||||
written := 0
|
||||
|
||||
@@ -70,6 +71,18 @@ func encodeURIComponent(s string) string {
|
||||
|
||||
continue
|
||||
}
|
||||
if len(excluded) > 0 {
|
||||
conti := false
|
||||
for _, ch := range excluded[0] {
|
||||
if ch == c {
|
||||
conti = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if conti {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
b.WriteString(s[written:i])
|
||||
@@ -83,3 +96,19 @@ func encodeURIComponent(s string) string {
|
||||
b.WriteString(s[written:])
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func decodeURIComponent(s string) (string, error) {
|
||||
decodeStr, err := url.QueryUnescape(s)
|
||||
if err != nil {
|
||||
return s, err
|
||||
}
|
||||
return decodeStr, err
|
||||
}
|
||||
|
||||
func DecodeURIComponent(s string) (string, error) {
|
||||
return DecodeURIComponent(s)
|
||||
}
|
||||
|
||||
func EncodeURIComponent(s string) string {
|
||||
return encodeURIComponent(s)
|
||||
}
|
||||
|
||||
245
object.go
245
object.go
@@ -2,14 +2,17 @@ package cos
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -30,6 +33,8 @@ type ObjectGetOptions struct {
|
||||
XCosSSECustomerAglo string `header:"x-cos-server-side-encryption-customer-algorithm,omitempty" url:"-" xml:"-"`
|
||||
XCosSSECustomerKey string `header:"x-cos-server-side-encryption-customer-key,omitempty" url:"-" xml:"-"`
|
||||
XCosSSECustomerKeyMD5 string `header:"x-cos-server-side-encryption-customer-key-MD5,omitempty" url:"-" xml:"-"`
|
||||
|
||||
XCosTrafficLimit int `header:"x-cos-traffic-limit,omitempty" url:"-" xml:"-"`
|
||||
}
|
||||
|
||||
// presignedURLTestingOptions is the opt of presigned url
|
||||
@@ -110,12 +115,12 @@ func (s *ObjectService) GetPresignedURL(ctx context.Context, httpMethod, name, a
|
||||
authTime = NewAuthTime(expired)
|
||||
}
|
||||
authorization := newAuthorization(ak, sk, req, authTime)
|
||||
sign := encodeURIComponent(authorization)
|
||||
sign := encodeURIComponent(authorization, []byte{'&','='})
|
||||
|
||||
if req.URL.RawQuery == "" {
|
||||
req.URL.RawQuery = fmt.Sprintf("sign=%s", sign)
|
||||
req.URL.RawQuery = fmt.Sprintf("%s", sign)
|
||||
} else {
|
||||
req.URL.RawQuery = fmt.Sprintf("%s&sign=%s", req.URL.RawQuery, sign)
|
||||
req.URL.RawQuery = fmt.Sprintf("%s&%s", req.URL.RawQuery, sign)
|
||||
}
|
||||
return req.URL, nil
|
||||
|
||||
@@ -146,6 +151,7 @@ type ObjectPutHeaderOptions struct {
|
||||
XCosSSECustomerKeyMD5 string `header:"x-cos-server-side-encryption-customer-key-MD5,omitempty" url:"-" xml:"-"`
|
||||
//兼容其他自定义头部
|
||||
XOptionHeader *http.Header `header:"-,omitempty" url:"-" xml:"-"`
|
||||
XCosTrafficLimit int `header:"x-cos-traffic-limit,omitempty" url:"-" xml:"-"`
|
||||
}
|
||||
|
||||
// ObjectPutOptions the options of put object
|
||||
@@ -236,30 +242,40 @@ type ObjectCopyResult struct {
|
||||
//
|
||||
// https://cloud.tencent.com/document/product/436/10881
|
||||
func (s *ObjectService) Copy(ctx context.Context, name, sourceURL string, opt *ObjectCopyOptions, id ...string) (*ObjectCopyResult, *Response, error) {
|
||||
surl := strings.SplitN(sourceURL, "/", 2)
|
||||
if len(surl) < 2 {
|
||||
return nil, nil, errors.New(fmt.Sprintf("x-cos-copy-source format error: %s", sourceURL))
|
||||
}
|
||||
var u string
|
||||
if len(id) == 1 {
|
||||
u = fmt.Sprintf("%s?versionId=%s", encodeURIComponent(sourceURL), id[0])
|
||||
u = fmt.Sprintf("%s/%s?versionId=%s", surl[0], encodeURIComponent(surl[1]), id[0])
|
||||
} else if len(id) == 0 {
|
||||
u = encodeURIComponent(sourceURL)
|
||||
u = fmt.Sprintf("%s/%s", surl[0], encodeURIComponent(surl[1]))
|
||||
} else {
|
||||
return nil, nil, errors.New("wrong params")
|
||||
}
|
||||
|
||||
var res ObjectCopyResult
|
||||
if opt == nil {
|
||||
opt = new(ObjectCopyOptions)
|
||||
copyOpt := &ObjectCopyOptions{
|
||||
&ObjectCopyHeaderOptions{},
|
||||
&ACLHeaderOptions{},
|
||||
}
|
||||
if opt.ObjectCopyHeaderOptions == nil {
|
||||
opt.ObjectCopyHeaderOptions = new(ObjectCopyHeaderOptions)
|
||||
if opt != nil {
|
||||
if opt.ObjectCopyHeaderOptions != nil {
|
||||
*copyOpt.ObjectCopyHeaderOptions = *opt.ObjectCopyHeaderOptions
|
||||
}
|
||||
opt.XCosCopySource = u
|
||||
if opt.ACLHeaderOptions != nil {
|
||||
*copyOpt.ACLHeaderOptions = *opt.ACLHeaderOptions
|
||||
}
|
||||
}
|
||||
copyOpt.XCosCopySource = u
|
||||
|
||||
sendOpt := sendOptions{
|
||||
baseURL: s.client.BaseURL.BucketURL,
|
||||
uri: "/" + encodeURIComponent(name),
|
||||
method: http.MethodPut,
|
||||
body: nil,
|
||||
optHeader: opt,
|
||||
optHeader: copyOpt,
|
||||
result: &res,
|
||||
}
|
||||
resp, err := s.client.send(ctx, &sendOpt)
|
||||
@@ -279,7 +295,9 @@ type ObjectDeleteOptions struct {
|
||||
XCosSSECustomerKeyMD5 string `header:"x-cos-server-side-encryption-customer-key-MD5,omitempty" url:"-" xml:"-"`
|
||||
//兼容其他自定义头部
|
||||
XOptionHeader *http.Header `header:"-,omitempty" url:"-" xml:"-"`
|
||||
VersionId string `header:"-" url:"VersionId,omitempty" xml:"-"`
|
||||
}
|
||||
|
||||
// Delete Object请求可以将一个文件(Object)删除。
|
||||
//
|
||||
// https://www.qcloud.com/document/product/436/7743
|
||||
@@ -298,6 +316,7 @@ func (s *ObjectService) Delete(ctx context.Context, name string, opt ...*ObjectD
|
||||
uri: "/" + encodeURIComponent(name),
|
||||
method: http.MethodDelete,
|
||||
optHeader: optHeader,
|
||||
optQuery: optHeader,
|
||||
}
|
||||
resp, err := s.client.send(ctx, &sendOpt)
|
||||
return resp, err
|
||||
@@ -434,9 +453,10 @@ type ObjectDeleteMultiResult struct {
|
||||
XMLName xml.Name `xml:"DeleteResult"`
|
||||
DeletedObjects []Object `xml:"Deleted,omitempty"`
|
||||
Errors []struct {
|
||||
Key string
|
||||
Code string
|
||||
Message string
|
||||
Key string `xml:",omitempty"`
|
||||
Code string `xml:",omitempty"`
|
||||
Message string `xml:",omitempty"`
|
||||
VersionId string `xml:",omitempty"`
|
||||
} `xml:"Error,omitempty"`
|
||||
}
|
||||
|
||||
@@ -466,6 +486,7 @@ type Object struct {
|
||||
LastModified string `xml:",omitempty"`
|
||||
StorageClass string `xml:",omitempty"`
|
||||
Owner *Owner `xml:",omitempty"`
|
||||
VersionId string `xml:",omitempty"`
|
||||
}
|
||||
|
||||
// MultiUploadOptions is the option of the multiupload,
|
||||
@@ -474,12 +495,15 @@ type MultiUploadOptions struct {
|
||||
OptIni *InitiateMultipartUploadOptions
|
||||
PartSize int64
|
||||
ThreadPoolSize int
|
||||
CheckPoint bool
|
||||
}
|
||||
|
||||
type Chunk struct {
|
||||
Number int
|
||||
OffSet int64
|
||||
Size int64
|
||||
Done bool
|
||||
ETag string
|
||||
}
|
||||
|
||||
// jobs
|
||||
@@ -496,6 +520,7 @@ type Jobs struct {
|
||||
type Results struct {
|
||||
PartNumber int
|
||||
Resp *Response
|
||||
err error
|
||||
}
|
||||
|
||||
func worker(s *ObjectService, jobs <-chan *Jobs, results chan<- *Results) {
|
||||
@@ -503,6 +528,7 @@ func worker(s *ObjectService, jobs <-chan *Jobs, results chan<- *Results) {
|
||||
fd, err := os.Open(j.FilePath)
|
||||
var res Results
|
||||
if err != nil {
|
||||
res.err = err
|
||||
res.PartNumber = j.Chunk.Number
|
||||
res.Resp = nil
|
||||
results <- &res
|
||||
@@ -518,6 +544,7 @@ func worker(s *ObjectService, jobs <-chan *Jobs, results chan<- *Results) {
|
||||
&io.LimitedReader{R: fd, N: j.Chunk.Size}, j.Opt)
|
||||
res.PartNumber = j.Chunk.Number
|
||||
res.Resp = resp
|
||||
res.err = err
|
||||
if err != nil {
|
||||
rt--
|
||||
if rt == 0 {
|
||||
@@ -591,6 +618,80 @@ func SplitFileIntoChunks(filePath string, partSize int64) ([]Chunk, int, error)
|
||||
|
||||
}
|
||||
|
||||
func (s *ObjectService) getResumableUploadID(ctx context.Context, name string) (string, error) {
|
||||
opt := &ObjectListUploadsOptions{
|
||||
Prefix: name,
|
||||
EncodingType: "url",
|
||||
}
|
||||
res, _, err := s.ListUploads(ctx, opt)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(res.Upload) == 0 {
|
||||
return "", nil
|
||||
}
|
||||
last := len(res.Upload) - 1
|
||||
for last >= 0 {
|
||||
decodeKey, _ := decodeURIComponent(res.Upload[last].Key)
|
||||
if decodeKey == name {
|
||||
return decodeURIComponent(res.Upload[last].UploadID)
|
||||
}
|
||||
last = last - 1
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (s *ObjectService) checkUploadedParts(ctx context.Context, name, UploadID, filepath string, chunks []Chunk, partNum int) error {
|
||||
var uploadedParts []Object
|
||||
isTruncated := true
|
||||
opt := &ObjectListPartsOptions{
|
||||
EncodingType: "url",
|
||||
}
|
||||
for isTruncated {
|
||||
res, _, err := s.ListParts(ctx, name, UploadID, opt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(res.Parts) > 0 {
|
||||
uploadedParts = append(uploadedParts, res.Parts...)
|
||||
}
|
||||
isTruncated = res.IsTruncated
|
||||
opt.PartNumberMarker = res.NextPartNumberMarker
|
||||
}
|
||||
fd, err := os.Open(filepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fd.Close()
|
||||
// 某个分块出错, 重置chunks
|
||||
ret := func(e error) error {
|
||||
for i, _ := range chunks {
|
||||
chunks[i].Done = false
|
||||
chunks[i].ETag = ""
|
||||
}
|
||||
return e
|
||||
}
|
||||
for _, part := range uploadedParts {
|
||||
partNumber := part.PartNumber
|
||||
if partNumber > partNum {
|
||||
return ret(errors.New("Part Number is not consistent"))
|
||||
}
|
||||
partNumber = partNumber - 1
|
||||
fd.Seek(chunks[partNumber].OffSet, os.SEEK_SET)
|
||||
bs, err := ioutil.ReadAll(io.LimitReader(fd, chunks[partNumber].Size))
|
||||
if err != nil {
|
||||
return ret(err)
|
||||
}
|
||||
localMD5 := fmt.Sprintf("\"%x\"", md5.Sum(bs))
|
||||
if localMD5 != part.ETag {
|
||||
return ret(errors.New(fmt.Sprintf("CheckSum Failed in Part[%d]", part.PartNumber)))
|
||||
}
|
||||
chunks[partNumber].Done = true
|
||||
chunks[partNumber].ETag = part.ETag
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MultiUpload/Upload 为高级upload接口,并发分块上传
|
||||
// 注意该接口目前只供参考
|
||||
//
|
||||
@@ -610,14 +711,46 @@ func (s *ObjectService) Upload(ctx context.Context, name string, filepath string
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
// filesize=0 , use simple upload
|
||||
if partNum == 0 {
|
||||
var opt0 *ObjectPutOptions
|
||||
if opt.OptIni != nil {
|
||||
opt0 = &ObjectPutOptions{
|
||||
opt.OptIni.ACLHeaderOptions,
|
||||
opt.OptIni.ObjectPutHeaderOptions,
|
||||
}
|
||||
}
|
||||
rsp, err := s.PutFromFile(ctx, name, filepath, opt0)
|
||||
if err != nil {
|
||||
return nil, rsp, err
|
||||
}
|
||||
result := &CompleteMultipartUploadResult{
|
||||
Key: name,
|
||||
ETag: rsp.Header.Get("ETag"),
|
||||
}
|
||||
return result, rsp, nil
|
||||
}
|
||||
|
||||
var uploadID string
|
||||
resumableFlag := false
|
||||
if opt.CheckPoint {
|
||||
var err error
|
||||
uploadID, err = s.getResumableUploadID(ctx, name)
|
||||
if err == nil && uploadID != "" {
|
||||
err = s.checkUploadedParts(ctx, name, uploadID, filepath, chunks, partNum)
|
||||
resumableFlag = (err == nil)
|
||||
}
|
||||
}
|
||||
|
||||
// 2.Init
|
||||
optini := opt.OptIni
|
||||
if !resumableFlag {
|
||||
res, _, err := s.InitiateMultipartUpload(ctx, name, optini)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
uploadID := res.UploadID
|
||||
uploadID = res.UploadID
|
||||
}
|
||||
var poolSize int
|
||||
if opt.ThreadPoolSize > 0 {
|
||||
poolSize = opt.ThreadPoolSize
|
||||
@@ -637,11 +770,15 @@ func (s *ObjectService) Upload(ctx context.Context, name string, filepath string
|
||||
|
||||
// 4.Push jobs
|
||||
for _, chunk := range chunks {
|
||||
if chunk.Done {
|
||||
continue
|
||||
}
|
||||
partOpt := &ObjectUploadPartOptions{}
|
||||
if optini != nil && optini.ObjectPutHeaderOptions != nil {
|
||||
partOpt.XCosSSECustomerAglo = optini.XCosSSECustomerAglo
|
||||
partOpt.XCosSSECustomerKey = optini.XCosSSECustomerKey
|
||||
partOpt.XCosSSECustomerKeyMD5 = optini.XCosSSECustomerKeyMD5
|
||||
partOpt.XCosTrafficLimit = optini.XCosTrafficLimit
|
||||
}
|
||||
job := &Jobs{
|
||||
Name: name,
|
||||
@@ -656,12 +793,18 @@ func (s *ObjectService) Upload(ctx context.Context, name string, filepath string
|
||||
close(chjobs)
|
||||
|
||||
// 5.Recv the resp etag to complete
|
||||
for i := 1; i <= partNum; i++ {
|
||||
for i := 0; i < partNum; i++ {
|
||||
if chunks[i].Done {
|
||||
optcom.Parts = append(optcom.Parts, Object{
|
||||
PartNumber: chunks[i].Number, ETag: chunks[i].ETag},
|
||||
)
|
||||
continue
|
||||
}
|
||||
res := <-chresults
|
||||
// Notice one part fail can not get the etag according.
|
||||
if res.Resp == nil {
|
||||
if res.Resp == nil || res.err != nil {
|
||||
// Some part already fail, can not to get the header inside.
|
||||
return nil, nil, fmt.Errorf("UploadID %s, part %d failed to get resp content.", uploadID, res.PartNumber)
|
||||
return nil, nil, fmt.Errorf("UploadID %s, part %d failed to get resp content. error: %s", uploadID, res.PartNumber, res.err.Error())
|
||||
}
|
||||
// Notice one part fail can not get the etag according.
|
||||
etag := res.Resp.Header.Get("ETag")
|
||||
@@ -675,3 +818,69 @@ func (s *ObjectService) Upload(ctx context.Context, name string, filepath string
|
||||
|
||||
return v, resp, err
|
||||
}
|
||||
|
||||
type ObjectPutTaggingOptions struct {
|
||||
XMLName xml.Name `xml:"Tagging"`
|
||||
TagSet []ObjectTaggingTag `xml:"TagSet>Tag,omitempty"`
|
||||
}
|
||||
type ObjectTaggingTag BucketTaggingTag
|
||||
type ObjectGetTaggingResult ObjectPutTaggingOptions
|
||||
|
||||
func (s *ObjectService) PutTagging(ctx context.Context, name string, opt *ObjectPutTaggingOptions, id ...string) (*Response, error) {
|
||||
var u string
|
||||
if len(id) == 1 {
|
||||
u = fmt.Sprintf("/%s?tagging&versionId=%s", encodeURIComponent(name), id[0])
|
||||
} else if len(id) == 0 {
|
||||
u = fmt.Sprintf("/%s?tagging", encodeURIComponent(name))
|
||||
} else {
|
||||
return nil, errors.New("wrong params")
|
||||
}
|
||||
sendOpt := &sendOptions{
|
||||
baseURL: s.client.BaseURL.BucketURL,
|
||||
uri: u,
|
||||
method: http.MethodPut,
|
||||
body: opt,
|
||||
}
|
||||
resp, err := s.client.send(ctx, sendOpt)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (s *ObjectService) GetTagging(ctx context.Context, name string, id ...string) (*ObjectGetTaggingResult, *Response, error) {
|
||||
var u string
|
||||
if len(id) == 1 {
|
||||
u = fmt.Sprintf("/%s?tagging&versionId=%s", encodeURIComponent(name), id[0])
|
||||
} else if len(id) == 0 {
|
||||
u = fmt.Sprintf("/%s?tagging", encodeURIComponent(name))
|
||||
} else {
|
||||
return nil, nil, errors.New("wrong params")
|
||||
}
|
||||
|
||||
var res ObjectGetTaggingResult
|
||||
sendOpt := &sendOptions{
|
||||
baseURL: s.client.BaseURL.BucketURL,
|
||||
uri: u,
|
||||
method: http.MethodGet,
|
||||
result: &res,
|
||||
}
|
||||
resp, err := s.client.send(ctx, sendOpt)
|
||||
return &res, resp, err
|
||||
}
|
||||
|
||||
func (s *ObjectService) DeleteTagging(ctx context.Context, name string, id ...string) (*Response, error) {
|
||||
var u string
|
||||
if len(id) == 1 {
|
||||
u = fmt.Sprintf("/%s?tagging&versionId=%s", encodeURIComponent(name), id[0])
|
||||
} else if len(id) == 0 {
|
||||
u = fmt.Sprintf("/%s?tagging", encodeURIComponent(name))
|
||||
} else {
|
||||
return nil, errors.New("wrong params")
|
||||
}
|
||||
|
||||
sendOpt := &sendOptions{
|
||||
baseURL: s.client.BaseURL.BucketURL,
|
||||
uri: u,
|
||||
method: http.MethodDelete,
|
||||
}
|
||||
resp, err := s.client.send(ctx, sendOpt)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
// ObjectGetACLResult is the result of GetObjectACL
|
||||
type ObjectGetACLResult ACLXml
|
||||
type ObjectGetACLResult = ACLXml
|
||||
|
||||
// GetACL Get Object ACL接口实现使用API读取Object的ACL表,只有所有者有权操作。
|
||||
//
|
||||
@@ -20,6 +20,9 @@ func (s *ObjectService) GetACL(ctx context.Context, name string) (*ObjectGetACLR
|
||||
result: &res,
|
||||
}
|
||||
resp, err := s.client.send(ctx, &sendOpt)
|
||||
if err == nil {
|
||||
decodeACL(resp, &res)
|
||||
}
|
||||
return &res, resp, err
|
||||
}
|
||||
|
||||
|
||||
@@ -42,12 +42,14 @@ func (s *ObjectService) InitiateMultipartUpload(ctx context.Context, name string
|
||||
// ObjectUploadPartOptions is the options of upload-part
|
||||
type ObjectUploadPartOptions struct {
|
||||
Expect string `header:"Expect,omitempty" url:"-"`
|
||||
XCosContentSHA1 string `header:"x-cos-content-sha1" url:"-"`
|
||||
XCosContentSHA1 string `header:"x-cos-content-sha1,omitempty" url:"-"`
|
||||
ContentLength int `header:"Content-Length,omitempty" url:"-"`
|
||||
|
||||
XCosSSECustomerAglo string `header:"x-cos-server-side-encryption-customer-algorithm,omitempty" url:"-" xml:"-"`
|
||||
XCosSSECustomerKey string `header:"x-cos-server-side-encryption-customer-key,omitempty" url:"-" xml:"-"`
|
||||
XCosSSECustomerKeyMD5 string `header:"x-cos-server-side-encryption-customer-key-MD5,omitempty" url:"-" xml:"-"`
|
||||
|
||||
XCosTrafficLimit int `header:"x-cos-traffic-limit,omitempty" url:"-" xml:"-"`
|
||||
}
|
||||
|
||||
// UploadPart 请求实现在初始化以后的分块上传,支持的块的数量为1到10000,块的大小为1 MB 到5 GB。
|
||||
@@ -244,3 +246,50 @@ func (s *ObjectService) CopyPart(ctx context.Context, name, uploadID string, par
|
||||
}
|
||||
return &res, resp, err
|
||||
}
|
||||
|
||||
type ObjectListUploadsOptions struct {
|
||||
Delimiter string `url:"Delimiter,omitempty"`
|
||||
EncodingType string `url:"EncodingType,omitempty"`
|
||||
Prefix string `url:"Prefix"`
|
||||
MaxUploads int `url:"MaxUploads"`
|
||||
KeyMarker string `url:"KeyMarker"`
|
||||
UploadIdMarker string `url:"UploadIDMarker"`
|
||||
}
|
||||
|
||||
type ObjectListUploadsResult struct {
|
||||
XMLName xml.Name `xml:"ListMultipartUploadsResult"`
|
||||
Bucket string `xml:"Bucket,omitempty"`
|
||||
EncodingType string `xml:"Encoding-Type,omitempty"`
|
||||
KeyMarker string `xml:"KeyMarker,omitempty"`
|
||||
UploadIdMarker string `xml:"UploadIdMarker,omitempty"`
|
||||
NextKeyMarker string `xml:"NextKeyMarker,omitempty"`
|
||||
NextUploadIdMarker string `xml:"NextUploadIdMarker,omitempty"`
|
||||
MaxUploads string `xml:"MaxUploads,omitempty"`
|
||||
IsTruncated bool `xml:"IsTruncated,omitempty"`
|
||||
Prefix string `xml:"Prefix,omitempty"`
|
||||
Delimiter string `xml:"Delimiter,omitempty"`
|
||||
Upload []ListUploadsResultUpload `xml:"Upload,omitempty"`
|
||||
CommonPrefixes []string `xml:"CommonPrefixes>Prefix,omitempty"`
|
||||
}
|
||||
|
||||
type ListUploadsResultUpload struct {
|
||||
Key string `xml:"Key,omitempty"`
|
||||
UploadID string `xml:"UploadId,omitempty"`
|
||||
StorageClass string `xml:"StorageClass,omitempty"`
|
||||
Initiator *Initiator `xml:"Initiator,omitempty"`
|
||||
Owner *Owner `xml:"Owner,omitempty"`
|
||||
Initiated string `xml:"Initiated,omitempty"`
|
||||
}
|
||||
|
||||
func (s *ObjectService) ListUploads(ctx context.Context, opt *ObjectListUploadsOptions) (*ObjectListUploadsResult, *Response, error) {
|
||||
var res ObjectListUploadsResult
|
||||
sendOpt := &sendOptions{
|
||||
baseURL: s.client.BaseURL.BucketURL,
|
||||
uri: "/?uploads",
|
||||
method: http.MethodGet,
|
||||
optQuery: opt,
|
||||
result: &res,
|
||||
}
|
||||
resp, err := s.client.send(ctx, sendOpt)
|
||||
return &res, resp, err
|
||||
}
|
||||
|
||||
444
object_select.go
Normal file
444
object_select.go
Normal file
@@ -0,0 +1,444 @@
|
||||
package cos
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"hash/crc32"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
type JSONInputSerialization struct {
|
||||
Type string `xml:"Type"`
|
||||
}
|
||||
|
||||
type CSVInputSerialization struct {
|
||||
RecordDelimiter string `xml:"RecordDelimiter,omitempty"`
|
||||
FieldDelimiter string `xml:"FieldDelimiter,omitempty"`
|
||||
QuoteCharacter string `xml:"QuoteCharacter,omitempty"`
|
||||
QuoteEscapeCharacter string `xml:"QuoteEscapeCharacter,omitempty"`
|
||||
AllowQuotedRecordDelimiter string `xml:"AllowQuotedRecordDelimiter,omitempty"`
|
||||
FileHeaderInfo string `xml:"FileHeaderInfo,omitempty"`
|
||||
Comments string `xml:"Comments,omitempty"`
|
||||
}
|
||||
|
||||
type SelectInputSerialization struct {
|
||||
CompressionType string `xml:"CompressionType,omitempty"`
|
||||
CSV *CSVInputSerialization `xml:"CSV,omitempty"`
|
||||
JSON *JSONInputSerialization `xml:"JSON,omitempty"`
|
||||
}
|
||||
|
||||
type JSONOutputSerialization struct {
|
||||
RecordDelimiter string `xml:"RecordDelimiter,omitempty"`
|
||||
}
|
||||
|
||||
type CSVOutputSerialization struct {
|
||||
QuoteFileds string `xml:"QuoteFileds,omitempty"`
|
||||
RecordDelimiter string `xml:"RecordDelimiter,omitempty"`
|
||||
FieldDelimiter string `xml:"FieldDelimiter,omitempty"`
|
||||
QuoteCharacter string `xml:"QuoteCharacter,omitempty"`
|
||||
QuoteEscapeCharacter string `xml:"QuoteEscapeCharacter,omitempty"`
|
||||
}
|
||||
|
||||
type SelectOutputSerialization struct {
|
||||
CSV *CSVOutputSerialization `xml:"CSV,omitempty"`
|
||||
JSON *JSONOutputSerialization `xml:"JSON,omitempty"`
|
||||
}
|
||||
|
||||
type ObjectSelectOptions struct {
|
||||
XMLName xml.Name `xml:"SelectRequest"`
|
||||
Expression string `xml:"Expression"`
|
||||
ExpressionType string `xml:"ExpressionType"`
|
||||
InputSerialization *SelectInputSerialization `xml:"InputSerialization"`
|
||||
OutputSerialization *SelectOutputSerialization `xml:"OutputSerialization"`
|
||||
RequestProgress string `xml:"RequestProgress>Enabled,omitempty"`
|
||||
}
|
||||
|
||||
func (s *ObjectService) Select(ctx context.Context, name string, opt *ObjectSelectOptions) (io.ReadCloser, error) {
|
||||
u := fmt.Sprintf("/%s?select&select-type=2", encodeURIComponent(name))
|
||||
sendOpt := sendOptions{
|
||||
baseURL: s.client.BaseURL.BucketURL,
|
||||
uri: u,
|
||||
method: http.MethodPost,
|
||||
body: opt,
|
||||
disableCloseBody: true,
|
||||
}
|
||||
resp, err := s.client.send(ctx, &sendOpt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := &ObjectSelectResponse{
|
||||
Headers: resp.Header,
|
||||
Body: resp.Body,
|
||||
StatusCode: resp.StatusCode,
|
||||
Frame: &ObjectSelectResult{
|
||||
NextFrame: true,
|
||||
Payload: []byte{},
|
||||
},
|
||||
Finish: false,
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *ObjectService) SelectToFile(ctx context.Context, name, file string, opt *ObjectSelectOptions) (*ObjectSelectResponse, error) {
|
||||
resp, err := s.Select(ctx, name, opt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res, _ := resp.(*ObjectSelectResponse)
|
||||
defer func() {
|
||||
io.Copy(ioutil.Discard, resp)
|
||||
resp.Close()
|
||||
}()
|
||||
|
||||
fd, err := os.OpenFile(file, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.FileMode(0664))
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
_, err = io.Copy(fd, resp)
|
||||
fd.Close()
|
||||
res.Finish = true
|
||||
return res, err
|
||||
}
|
||||
|
||||
const (
|
||||
kReadTimeout = 3
|
||||
kMessageType = ":message-type"
|
||||
kEventType = ":event-type"
|
||||
kContentType = ":content-type"
|
||||
|
||||
kRecordsFrameType = iota
|
||||
kContinuationFrameType
|
||||
kProgressFrameType
|
||||
kStatsFrameType
|
||||
kEndFrameType
|
||||
kErrorFrameType
|
||||
)
|
||||
|
||||
type ProgressFrame struct {
|
||||
XMLName xml.Name `xml:"Progress"`
|
||||
BytesScanned int `xml:"BytesScanned"`
|
||||
BytesProcessed int `xml:"BytesProcessed"`
|
||||
BytesReturned int `xml:"BytesReturned"`
|
||||
}
|
||||
|
||||
type StatsFrame struct {
|
||||
XMLName xml.Name `xml:"Stats"`
|
||||
BytesScanned int `xml:"BytesScanned"`
|
||||
BytesProcessed int `xml:"BytesProcessed"`
|
||||
BytesReturned int `xml:"BytesReturned"`
|
||||
}
|
||||
|
||||
type DataFrame struct {
|
||||
ContentType string
|
||||
ConsumedBytesLength int32
|
||||
LeftBytesLength int32
|
||||
}
|
||||
|
||||
type ErrorFrame struct {
|
||||
Code string
|
||||
Message string
|
||||
}
|
||||
|
||||
func (e *ErrorFrame) Error() string {
|
||||
return fmt.Sprintf("Error Code: %s, Error Message: %s", e.Code, e.Message)
|
||||
}
|
||||
|
||||
type ObjectSelectResult struct {
|
||||
TotalFrameLength int32
|
||||
TotalHeaderLength int32
|
||||
NextFrame bool
|
||||
FrameType int
|
||||
Payload []byte
|
||||
DataFrame DataFrame
|
||||
ProgressFrame ProgressFrame
|
||||
StatsFrame StatsFrame
|
||||
ErrorFrame *ErrorFrame
|
||||
}
|
||||
|
||||
type ObjectSelectResponse struct {
|
||||
StatusCode int
|
||||
Headers http.Header
|
||||
Body io.ReadCloser
|
||||
Frame *ObjectSelectResult
|
||||
Finish bool
|
||||
}
|
||||
|
||||
func (osr *ObjectSelectResponse) Read(p []byte) (n int, err error) {
|
||||
n, err = osr.readFrames(p)
|
||||
return
|
||||
}
|
||||
func (osr *ObjectSelectResponse) Close() error {
|
||||
return osr.Body.Close()
|
||||
}
|
||||
|
||||
func (osr *ObjectSelectResponse) readFrames(p []byte) (int, error) {
|
||||
if osr.Finish {
|
||||
return 0, io.EOF
|
||||
}
|
||||
if osr.Frame.ErrorFrame != nil {
|
||||
return 0, osr.Frame.ErrorFrame
|
||||
}
|
||||
|
||||
var err error
|
||||
var nlen int
|
||||
dlen := len(p)
|
||||
|
||||
for nlen < dlen {
|
||||
if osr.Frame.NextFrame == true {
|
||||
osr.Frame.NextFrame = false
|
||||
err := osr.analysisPrelude()
|
||||
if err != nil {
|
||||
return nlen, err
|
||||
}
|
||||
err = osr.analysisHeader()
|
||||
if err != nil {
|
||||
return nlen, err
|
||||
}
|
||||
}
|
||||
switch osr.Frame.FrameType {
|
||||
case kRecordsFrameType:
|
||||
n, err := osr.analysisRecords(p[nlen:])
|
||||
if err != nil {
|
||||
return nlen, err
|
||||
}
|
||||
nlen += n
|
||||
case kContinuationFrameType:
|
||||
err = osr.payloadChecksum("ContinuationFrame")
|
||||
if err != nil {
|
||||
return nlen, err
|
||||
}
|
||||
case kProgressFrameType:
|
||||
err := osr.analysisXml(&osr.Frame.ProgressFrame)
|
||||
if err != nil {
|
||||
return nlen, err
|
||||
}
|
||||
case kStatsFrameType:
|
||||
err := osr.analysisXml(&osr.Frame.StatsFrame)
|
||||
if err != nil {
|
||||
return nlen, err
|
||||
}
|
||||
case kEndFrameType:
|
||||
err = osr.payloadChecksum("EndFrame")
|
||||
if err != nil {
|
||||
return nlen, err
|
||||
}
|
||||
osr.Finish = true
|
||||
return nlen, io.EOF
|
||||
case kErrorFrameType:
|
||||
return nlen, osr.Frame.ErrorFrame
|
||||
}
|
||||
}
|
||||
return nlen, err
|
||||
}
|
||||
|
||||
func (osr *ObjectSelectResponse) analysisPrelude() error {
|
||||
frame := make([]byte, 12)
|
||||
_, err := osr.fixedLengthRead(frame, kReadTimeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var preludeCRC uint32
|
||||
bytesToInt(frame[0:4], &osr.Frame.TotalFrameLength)
|
||||
bytesToInt(frame[4:8], &osr.Frame.TotalHeaderLength)
|
||||
bytesToInt(frame[8:12], &preludeCRC)
|
||||
osr.Frame.Payload = append(osr.Frame.Payload, frame...)
|
||||
|
||||
return checksum(frame[0:8], preludeCRC, "Prelude")
|
||||
}
|
||||
|
||||
func (osr *ObjectSelectResponse) analysisHeader() error {
|
||||
var nlen int32
|
||||
headers := make(map[string]string)
|
||||
for nlen < osr.Frame.TotalHeaderLength {
|
||||
var headerNameLen int8
|
||||
var headerValueLen int16
|
||||
bHeaderNameLen := make([]byte, 1)
|
||||
_, err := osr.fixedLengthRead(bHeaderNameLen, kReadTimeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nlen += 1
|
||||
bytesToInt(bHeaderNameLen, &headerNameLen)
|
||||
osr.Frame.Payload = append(osr.Frame.Payload, bHeaderNameLen...)
|
||||
|
||||
bHeaderName := make([]byte, headerNameLen)
|
||||
_, err = osr.fixedLengthRead(bHeaderName, kReadTimeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nlen += int32(headerNameLen)
|
||||
headerName := string(bHeaderName)
|
||||
osr.Frame.Payload = append(osr.Frame.Payload, bHeaderName...)
|
||||
|
||||
bValueTypeLen := make([]byte, 3)
|
||||
_, err = osr.fixedLengthRead(bValueTypeLen, kReadTimeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nlen += 3
|
||||
bytesToInt(bValueTypeLen[1:], &headerValueLen)
|
||||
osr.Frame.Payload = append(osr.Frame.Payload, bValueTypeLen...)
|
||||
|
||||
bHeaderValue := make([]byte, headerValueLen)
|
||||
_, err = osr.fixedLengthRead(bHeaderValue, kReadTimeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nlen += int32(headerValueLen)
|
||||
headers[headerName] = string(bHeaderValue)
|
||||
osr.Frame.Payload = append(osr.Frame.Payload, bHeaderValue...)
|
||||
}
|
||||
htype, ok := headers[kMessageType]
|
||||
if !ok {
|
||||
return fmt.Errorf("header parse failed, no message-type, headers: %+v\n", headers)
|
||||
}
|
||||
switch {
|
||||
case htype == "error":
|
||||
osr.Frame.FrameType = kErrorFrameType
|
||||
osr.Frame.ErrorFrame = &ErrorFrame{}
|
||||
osr.Frame.ErrorFrame.Code, _ = headers[":error-code"]
|
||||
osr.Frame.ErrorFrame.Message, _ = headers[":error-message"]
|
||||
case htype == "event":
|
||||
hevent, ok := headers[kEventType]
|
||||
if !ok {
|
||||
return fmt.Errorf("header parse failed, no event-type, headers: %+v\n", headers)
|
||||
}
|
||||
switch {
|
||||
case hevent == "Records":
|
||||
hContentType, ok := headers[kContentType]
|
||||
if ok {
|
||||
osr.Frame.DataFrame.ContentType = hContentType
|
||||
}
|
||||
osr.Frame.FrameType = kRecordsFrameType
|
||||
case hevent == "Cont":
|
||||
osr.Frame.FrameType = kContinuationFrameType
|
||||
case hevent == "Progress":
|
||||
osr.Frame.FrameType = kProgressFrameType
|
||||
case hevent == "Stats":
|
||||
osr.Frame.FrameType = kStatsFrameType
|
||||
case hevent == "End":
|
||||
osr.Frame.FrameType = kEndFrameType
|
||||
default:
|
||||
return fmt.Errorf("header parse failed, invalid event-type, headers: %+v\n", headers)
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("header parse failed, invalid message-type: headers: %+v\n", headers)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (osr *ObjectSelectResponse) analysisRecords(data []byte) (int, error) {
|
||||
var needReadLength int32
|
||||
dlen := int32(len(data))
|
||||
restLen := osr.Frame.TotalFrameLength - 16 - osr.Frame.TotalHeaderLength - osr.Frame.DataFrame.ConsumedBytesLength
|
||||
if dlen <= restLen {
|
||||
needReadLength = dlen
|
||||
} else {
|
||||
needReadLength = restLen
|
||||
}
|
||||
n, err := osr.fixedLengthRead(data[:needReadLength], kReadTimeout)
|
||||
if err != nil {
|
||||
return n, fmt.Errorf("read data frame error: %s", err.Error())
|
||||
}
|
||||
osr.Frame.DataFrame.ConsumedBytesLength += int32(n)
|
||||
osr.Frame.Payload = append(osr.Frame.Payload, data[:needReadLength]...)
|
||||
// 读完了一帧数据并填充到data中了
|
||||
if osr.Frame.DataFrame.ConsumedBytesLength == osr.Frame.TotalFrameLength-16-osr.Frame.TotalHeaderLength {
|
||||
osr.Frame.DataFrame.ConsumedBytesLength = 0
|
||||
err = osr.payloadChecksum("RecordFrame")
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (osr *ObjectSelectResponse) analysisXml(frame interface{}) error {
|
||||
payloadLength := osr.Frame.TotalFrameLength - 16 - osr.Frame.TotalHeaderLength
|
||||
bFrame := make([]byte, payloadLength)
|
||||
_, err := osr.fixedLengthRead(bFrame, kReadTimeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = xml.Unmarshal(bFrame, frame)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
osr.Frame.Payload = append(osr.Frame.Payload, bFrame...)
|
||||
return osr.payloadChecksum("XmlFrame")
|
||||
}
|
||||
|
||||
// 调用payloadChecksum时,表示该帧已读完,开始读取下一帧内容
|
||||
func (osr *ObjectSelectResponse) payloadChecksum(ftype string) error {
|
||||
bcrc := make([]byte, 4)
|
||||
_, err := osr.fixedLengthRead(bcrc, kReadTimeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var res uint32
|
||||
bytesToInt(bcrc, &res)
|
||||
err = checksum(osr.Frame.Payload, res, ftype)
|
||||
|
||||
osr.Frame.NextFrame = true
|
||||
osr.Frame.Payload = []byte{}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
type chanReadIO struct {
|
||||
readLen int
|
||||
err error
|
||||
}
|
||||
|
||||
func (osr *ObjectSelectResponse) fixedLengthRead(p []byte, read_timeout int64) (int, error) {
|
||||
timeout := time.Duration(read_timeout)
|
||||
r := osr.Body
|
||||
ch := make(chan chanReadIO, 1)
|
||||
defer close(ch)
|
||||
go func(p []byte) {
|
||||
var needLen int
|
||||
readChan := chanReadIO{}
|
||||
needLen = len(p)
|
||||
for {
|
||||
n, err := r.Read(p[readChan.readLen:needLen])
|
||||
readChan.readLen += n
|
||||
if err != nil {
|
||||
readChan.err = err
|
||||
ch <- readChan
|
||||
return
|
||||
}
|
||||
|
||||
if readChan.readLen == needLen {
|
||||
break
|
||||
}
|
||||
}
|
||||
ch <- readChan
|
||||
}(p)
|
||||
|
||||
select {
|
||||
case <-time.After(time.Second * timeout):
|
||||
return 0, fmt.Errorf("requestId: %s, readLen timeout, timeout is %d(second),need read:%d", "sr.Headers.Get(HTTPHeaderOssRequestID)", timeout, len(p))
|
||||
case result := <-ch:
|
||||
return result.readLen, result.err
|
||||
}
|
||||
}
|
||||
|
||||
func bytesToInt(b []byte, ret interface{}) {
|
||||
binBuf := bytes.NewBuffer(b)
|
||||
binary.Read(binBuf, binary.BigEndian, ret)
|
||||
}
|
||||
|
||||
func checksum(b []byte, rec uint32, ftype string) error {
|
||||
c := crc32.ChecksumIEEE(b)
|
||||
if c != rec {
|
||||
return fmt.Errorf("parse type: %v, checksum failed, cal: %v, rec: %v\n", ftype, c, rec)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user