Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6a2da87a06 | ||
|
|
c07e49771c | ||
|
|
6da3d4094c | ||
|
|
72e7751604 | ||
|
|
5057561906 | ||
|
|
3b45c0ae0e | ||
|
|
ba2e64b62e | ||
|
|
b8afb3f850 |
25
CHANGELOG.md
25
CHANGELOG.md
@@ -7,6 +7,31 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
|
||||
|
||||
## [v0.7.15](https://github.com/tencentyun/cos-go-sdk-v5/compare/v0.7.14...v0.7.15) - 2020-12-11
|
||||
|
||||
update ci & ci document
|
||||
|
||||
### Merged
|
||||
|
||||
- update ci [`#96`](https://github.com/tencentyun/cos-go-sdk-v5/pull/96)
|
||||
|
||||
### Commits
|
||||
|
||||
- Updated CHANGELOG.md [`b8afb3f`](https://github.com/tencentyun/cos-go-sdk-v5/commit/b8afb3f85050cee4884a46c8ed49d26bf76d10a4)
|
||||
|
||||
## [v0.7.14](https://github.com/tencentyun/cos-go-sdk-v5/compare/v0.7.13...v0.7.14) - 2020-12-09
|
||||
|
||||
fix bucket lifecycle
|
||||
|
||||
### Merged
|
||||
|
||||
- update version [`#95`](https://github.com/tencentyun/cos-go-sdk-v5/pull/95)
|
||||
- fix bucket lifecycle [`#94`](https://github.com/tencentyun/cos-go-sdk-v5/pull/94)
|
||||
|
||||
### Commits
|
||||
|
||||
- Updated CHANGELOG.md [`e3a89ee`](https://github.com/tencentyun/cos-go-sdk-v5/commit/e3a89ee58b4f524c7ad5f2b1f0bdc688a2e39c32)
|
||||
|
||||
## [v0.7.13](https://github.com/tencentyun/cos-go-sdk-v5/compare/v0.7.12...v0.7.13) - 2020-12-08
|
||||
|
||||
add ci document && add progress
|
||||
|
||||
37
bucket_accelerate.go
Normal file
37
bucket_accelerate.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package cos
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type BucketPutAccelerateOptions struct {
|
||||
XMLName xml.Name `xml:"AccelerateConfiguration"`
|
||||
Status string `xml:"Status,omitempty"`
|
||||
Type string `xml:"Type,omitempty"`
|
||||
}
|
||||
type BucketGetAccelerateResult BucketPutAccelerateOptions
|
||||
|
||||
func (s *BucketService) PutAccelerate(ctx context.Context, opt *BucketPutAccelerateOptions) (*Response, error) {
|
||||
sendOpt := &sendOptions{
|
||||
baseURL: s.client.BaseURL.BucketURL,
|
||||
uri: "/?accelerate",
|
||||
method: http.MethodPut,
|
||||
body: opt,
|
||||
}
|
||||
resp, err := s.client.send(ctx, sendOpt)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (s *BucketService) GetAccelerate(ctx context.Context) (*BucketGetAccelerateResult, *Response, error) {
|
||||
var res BucketGetAccelerateResult
|
||||
sendOpt := &sendOptions{
|
||||
baseURL: s.client.BaseURL.BucketURL,
|
||||
uri: "/?accelerate",
|
||||
method: http.MethodGet,
|
||||
result: &res,
|
||||
}
|
||||
resp, err := s.client.send(ctx, sendOpt)
|
||||
return &res, resp, err
|
||||
}
|
||||
74
bucket_accelerate_test.go
Normal file
74
bucket_accelerate_test.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package cos
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBucketService_GetAccelerate(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
testMethod(t, r, "GET")
|
||||
vs := values{
|
||||
"accelerate": "",
|
||||
}
|
||||
testFormValues(t, r, vs)
|
||||
fmt.Fprint(w, `<AccelerateConfiguration>
|
||||
<Status>Enabled</Status>
|
||||
<Type>COS</Type>
|
||||
</AccelerateConfiguration>`)
|
||||
})
|
||||
|
||||
res, _, err := client.Bucket.GetAccelerate(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("Bucket.GetAccelerate returned error %v", err)
|
||||
}
|
||||
|
||||
want := &BucketGetAccelerateResult{
|
||||
XMLName: xml.Name{Local: "AccelerateConfiguration"},
|
||||
Status: "Enabled",
|
||||
Type: "COS",
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(res, want) {
|
||||
t.Errorf("Bucket.GetAccelerate returned %+v, want %+v", res, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBucketService_PutAccelerate(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
opt := &BucketPutAccelerateOptions{
|
||||
XMLName: xml.Name{Local: "AccelerateConfiguration"},
|
||||
Status: "Enabled",
|
||||
Type: "COS",
|
||||
}
|
||||
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
testMethod(t, r, "PUT")
|
||||
vs := values{
|
||||
"accelerate": "",
|
||||
}
|
||||
testFormValues(t, r, vs)
|
||||
|
||||
body := new(BucketPutAccelerateOptions)
|
||||
xml.NewDecoder(r.Body).Decode(body)
|
||||
want := opt
|
||||
want.XMLName = xml.Name{Local: "AccelerateConfiguration"}
|
||||
if !reflect.DeepEqual(body, want) {
|
||||
t.Errorf("Bucket.PutAccelerate request\n body: %+v\n, want %+v\n", body, want)
|
||||
}
|
||||
})
|
||||
|
||||
_, err := client.Bucket.PutAccelerate(context.Background(), opt)
|
||||
if err != nil {
|
||||
t.Fatalf("Bucket.PutAccelerate returned error: %v", err)
|
||||
}
|
||||
}
|
||||
7
ci.go
7
ci.go
@@ -33,19 +33,21 @@ func EncodePicOperations(pic *PicOperations) string {
|
||||
type ImageProcessResult struct {
|
||||
XMLName xml.Name `xml:"UploadResult"`
|
||||
OriginalInfo *PicOriginalInfo `xml:"OriginalInfo,omitempty"`
|
||||
ProcessObject *PicProcessObject `xml:"ProcessResults>Object,omitempty"`
|
||||
ProcessResults *PicProcessObject `xml:"ProcessResults>Object,omitempty"`
|
||||
}
|
||||
type PicOriginalInfo struct {
|
||||
Key string `xml:"Key,omitempty"`
|
||||
Location string `xml:"Location,omitempty"`
|
||||
ImageInfo *PicImageInfo `xml:"ImageInfo,omitempty"`
|
||||
ETag string `xml:"ETag,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"`
|
||||
Ave string `xml:"Ave,omitempty"`
|
||||
Orientation int `xml:"Orientation,omitempty"`
|
||||
}
|
||||
type PicProcessObject struct {
|
||||
Key string `xml:"Key,omitempty"`
|
||||
@@ -55,6 +57,7 @@ type PicProcessObject struct {
|
||||
Height int `xml:"Height,omitempty"`
|
||||
Size int `xml:"Size,omitempty"`
|
||||
Quality int `xml:"Quality,omitempty"`
|
||||
ETag string `xml:"ETag,omitempty"`
|
||||
WatermarkStatus int `xml:"WatermarkStatus,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
@@ -265,6 +265,7 @@ func (s *CIService) DocPreview(ctx context.Context, name string, opt *DocPreview
|
||||
uri: "/" + encodeURIComponent(name) + "?ci-process=doc-preview",
|
||||
optQuery: opt,
|
||||
method: http.MethodGet,
|
||||
disableCloseBody: true,
|
||||
}
|
||||
resp, err := s.client.send(ctx, &sendOpt)
|
||||
return resp, err
|
||||
|
||||
2
cos.go
2
cos.go
@@ -22,7 +22,7 @@ import (
|
||||
|
||||
const (
|
||||
// Version current go sdk version
|
||||
Version = "0.7.14"
|
||||
Version = "0.7.16"
|
||||
userAgent = "cos-go-sdk-v5/" + Version
|
||||
contentTypeXML = "application/xml"
|
||||
defaultServiceBaseURL = "http://service.cos.myqcloud.com"
|
||||
|
||||
@@ -882,6 +882,31 @@ func (s *CosTestSuite) TestReferer() {
|
||||
assert.Equal(s.T(), opt.EmptyReferConfiguration, res.EmptyReferConfiguration, "GetReferer Failed")
|
||||
}
|
||||
|
||||
func (s *CosTestSuite) TestAccelerate() {
|
||||
opt := &cos.BucketPutAccelerateOptions{
|
||||
Status: "Enabled",
|
||||
Type: "COS",
|
||||
}
|
||||
_, err := s.Client.Bucket.PutAccelerate(context.Background(), opt)
|
||||
assert.Nil(s.T(), err, "PutAccelerate Failed")
|
||||
|
||||
time.Sleep(time.Second)
|
||||
res, _, err := s.Client.Bucket.GetAccelerate(context.Background())
|
||||
assert.Nil(s.T(), err, "GetAccelerate Failed")
|
||||
assert.Equal(s.T(), opt.Status, res.Status, "GetAccelerate Failed")
|
||||
assert.Equal(s.T(), opt.Type, res.Type, "GetAccelerate Failed")
|
||||
|
||||
opt.Status = "Suspended"
|
||||
_, err = s.Client.Bucket.PutAccelerate(context.Background(), opt)
|
||||
assert.Nil(s.T(), err, "PutAccelerate Failed")
|
||||
|
||||
time.Sleep(time.Second)
|
||||
res, _, err = s.Client.Bucket.GetAccelerate(context.Background())
|
||||
assert.Nil(s.T(), err, "GetAccelerate Failed")
|
||||
assert.Equal(s.T(), opt.Status, res.Status, "GetAccelerate Failed")
|
||||
assert.Equal(s.T(), opt.Type, res.Type, "GetAccelerate Failed")
|
||||
}
|
||||
|
||||
// End of api test
|
||||
|
||||
// All methods that begin with "Test" are run as tests within a
|
||||
|
||||
72
example/bucket/accelerate.go
Normal file
72
example/bucket/accelerate.go
Normal file
@@ -0,0 +1,72 @@
|
||||
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,
|
||||
},
|
||||
},
|
||||
})
|
||||
res, _, err := c.Bucket.GetAccelerate(context.Background())
|
||||
log_status(err)
|
||||
fmt.Printf("%+v\n", res)
|
||||
|
||||
opt := &cos.BucketPutAccelerateOptions{
|
||||
Status: "Enabled",
|
||||
Type: "COS",
|
||||
}
|
||||
_, err = c.Bucket.PutAccelerate(context.Background(), opt)
|
||||
log_status(err)
|
||||
|
||||
res, _, err = c.Bucket.GetAccelerate(context.Background())
|
||||
log_status(err)
|
||||
fmt.Printf("%+v\n", res)
|
||||
|
||||
opt.Status = "Suspended"
|
||||
_, err = c.Bucket.PutAccelerate(context.Background(), opt)
|
||||
log_status(err)
|
||||
|
||||
res, _, err = c.Bucket.GetAccelerate(context.Background())
|
||||
log_status(err)
|
||||
fmt.Printf("%+v\n", res)
|
||||
}
|
||||
@@ -79,10 +79,10 @@ func main() {
|
||||
resp, err := c.Object.UploadPart(
|
||||
context.Background(), name, uploadID, 1, fd, opt,
|
||||
)
|
||||
log_status(err)
|
||||
optcom.Parts = append(optcom.Parts, cos.Object{
|
||||
PartNumber: 1, ETag: resp.Header.Get("ETag"),
|
||||
})
|
||||
log_status(err)
|
||||
|
||||
f := strings.NewReader("test heoo")
|
||||
resp, err = c.Object.UploadPart(
|
||||
|
||||
12
helper.go
12
helper.go
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"crypto/sha1"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -12,6 +13,9 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 单次上传文件最大为5GB
|
||||
const singleUploadMaxLength = 5 * 1024 * 1024 * 1024
|
||||
|
||||
// 计算 md5 或 sha1 时的分块大小
|
||||
const calDigestBlockSize = 1024 * 1024 * 10
|
||||
|
||||
@@ -140,3 +144,11 @@ func GetReaderLen(reader io.Reader) (length int64, err error) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func CheckReaderLen(reader io.Reader) error {
|
||||
nlen, err := GetReaderLen(reader)
|
||||
if err != nil || nlen < singleUploadMaxLength {
|
||||
return nil
|
||||
}
|
||||
return errors.New("The single object size you upload can not be larger than 5GB")
|
||||
}
|
||||
|
||||
@@ -181,6 +181,9 @@ type ObjectPutOptions struct {
|
||||
//
|
||||
// https://www.qcloud.com/document/product/436/7749
|
||||
func (s *ObjectService) Put(ctx context.Context, name string, r io.Reader, opt *ObjectPutOptions) (*Response, error) {
|
||||
if err := CheckReaderLen(r); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if opt != nil && opt.Listener != nil {
|
||||
totalBytes, err := GetReaderLen(r)
|
||||
if err != nil {
|
||||
@@ -802,6 +805,7 @@ func (s *ObjectService) Upload(ctx context.Context, name string, filepath string
|
||||
progressCallback(listener, event)
|
||||
|
||||
// 4.Push jobs
|
||||
go func() {
|
||||
for _, chunk := range chunks {
|
||||
if chunk.Done {
|
||||
continue
|
||||
@@ -824,6 +828,7 @@ func (s *ObjectService) Upload(ctx context.Context, name string, filepath string
|
||||
chjobs <- job
|
||||
}
|
||||
close(chjobs)
|
||||
}()
|
||||
|
||||
// 5.Recv the resp etag to complete
|
||||
for i := 0; i < partNum; i++ {
|
||||
@@ -854,6 +859,7 @@ func (s *ObjectService) Upload(ctx context.Context, name string, filepath string
|
||||
event = newProgressEvent(ProgressDataEvent, chunks[res.PartNumber-1].Size, consumedBytes, totalBytes)
|
||||
progressCallback(listener, event)
|
||||
}
|
||||
close(chresults)
|
||||
sort.Sort(ObjectList(optcom.Parts))
|
||||
|
||||
event = newProgressEvent(ProgressCompletedEvent, 0, consumedBytes, totalBytes)
|
||||
|
||||
@@ -44,13 +44,14 @@ type ObjectUploadPartOptions struct {
|
||||
Expect string `header:"Expect,omitempty" url:"-"`
|
||||
XCosContentSHA1 string `header:"x-cos-content-sha1,omitempty" url:"-"`
|
||||
ContentLength int `header:"Content-Length,omitempty" url:"-"`
|
||||
|
||||
ContentMD5 string `header:"Content-MD5,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:"-"`
|
||||
|
||||
XOptionHeader *http.Header `header:"-,omitempty" url:"-" xml:"-"`
|
||||
// 上传进度, ProgressCompleteEvent不能表示对应API调用成功,API是否调用成功的判断标准为返回err==nil
|
||||
Listener ProgressListener `header:"-" url:"-" xml:"-"`
|
||||
}
|
||||
@@ -64,6 +65,9 @@ type ObjectUploadPartOptions struct {
|
||||
//
|
||||
// https://www.qcloud.com/document/product/436/7750
|
||||
func (s *ObjectService) UploadPart(ctx context.Context, name, uploadID string, partNumber int, r io.Reader, opt *ObjectUploadPartOptions) (*Response, error) {
|
||||
if err := CheckReaderLen(r); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if opt != nil && opt.Listener != nil {
|
||||
totalBytes, err := GetReaderLen(r)
|
||||
if err != nil {
|
||||
|
||||
@@ -97,6 +97,10 @@ func (r *teeReader) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *teeReader) Size() int64 {
|
||||
return r.totalBytes
|
||||
}
|
||||
|
||||
func TeeReader(reader io.Reader, writer io.Writer, total int64, listener ProgressListener) *teeReader {
|
||||
return &teeReader{
|
||||
reader: reader,
|
||||
|
||||
Reference in New Issue
Block a user