update upload progress && single object length
This commit is contained in:
@@ -79,10 +79,10 @@ func main() {
|
|||||||
resp, err := c.Object.UploadPart(
|
resp, err := c.Object.UploadPart(
|
||||||
context.Background(), name, uploadID, 1, fd, opt,
|
context.Background(), name, uploadID, 1, fd, opt,
|
||||||
)
|
)
|
||||||
|
log_status(err)
|
||||||
optcom.Parts = append(optcom.Parts, cos.Object{
|
optcom.Parts = append(optcom.Parts, cos.Object{
|
||||||
PartNumber: 1, ETag: resp.Header.Get("ETag"),
|
PartNumber: 1, ETag: resp.Header.Get("ETag"),
|
||||||
})
|
})
|
||||||
log_status(err)
|
|
||||||
|
|
||||||
f := strings.NewReader("test heoo")
|
f := strings.NewReader("test heoo")
|
||||||
resp, err = c.Object.UploadPart(
|
resp, err = c.Object.UploadPart(
|
||||||
|
|||||||
12
helper.go
12
helper.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -12,6 +13,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 单次上传文件最大为5GB
|
||||||
|
const singleUploadMaxLength = 5 * 1024 * 1024 * 1024
|
||||||
|
|
||||||
// 计算 md5 或 sha1 时的分块大小
|
// 计算 md5 或 sha1 时的分块大小
|
||||||
const calDigestBlockSize = 1024 * 1024 * 10
|
const calDigestBlockSize = 1024 * 1024 * 10
|
||||||
|
|
||||||
@@ -140,3 +144,11 @@ func GetReaderLen(reader io.Reader) (length int64, err error) {
|
|||||||
}
|
}
|
||||||
return
|
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")
|
||||||
|
}
|
||||||
|
|||||||
50
object.go
50
object.go
@@ -127,7 +127,7 @@ func (s *ObjectService) GetPresignedURL(ctx context.Context, httpMethod, name, a
|
|||||||
authTime = NewAuthTime(expired)
|
authTime = NewAuthTime(expired)
|
||||||
}
|
}
|
||||||
authorization := newAuthorization(ak, sk, req, authTime)
|
authorization := newAuthorization(ak, sk, req, authTime)
|
||||||
sign := encodeURIComponent(authorization, []byte{'&','='})
|
sign := encodeURIComponent(authorization, []byte{'&', '='})
|
||||||
|
|
||||||
if req.URL.RawQuery == "" {
|
if req.URL.RawQuery == "" {
|
||||||
req.URL.RawQuery = fmt.Sprintf("%s", sign)
|
req.URL.RawQuery = fmt.Sprintf("%s", sign)
|
||||||
@@ -181,6 +181,9 @@ type ObjectPutOptions struct {
|
|||||||
//
|
//
|
||||||
// https://www.qcloud.com/document/product/436/7749
|
// https://www.qcloud.com/document/product/436/7749
|
||||||
func (s *ObjectService) Put(ctx context.Context, name string, r io.Reader, opt *ObjectPutOptions) (*Response, error) {
|
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 {
|
if opt != nil && opt.Listener != nil {
|
||||||
totalBytes, err := GetReaderLen(r)
|
totalBytes, err := GetReaderLen(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -802,28 +805,30 @@ func (s *ObjectService) Upload(ctx context.Context, name string, filepath string
|
|||||||
progressCallback(listener, event)
|
progressCallback(listener, event)
|
||||||
|
|
||||||
// 4.Push jobs
|
// 4.Push jobs
|
||||||
for _, chunk := range chunks {
|
go func() {
|
||||||
if chunk.Done {
|
for _, chunk := range chunks {
|
||||||
continue
|
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,
|
||||||
|
RetryTimes: 3,
|
||||||
|
FilePath: filepath,
|
||||||
|
UploadId: uploadID,
|
||||||
|
Chunk: chunk,
|
||||||
|
Opt: partOpt,
|
||||||
|
}
|
||||||
|
chjobs <- job
|
||||||
}
|
}
|
||||||
partOpt := &ObjectUploadPartOptions{}
|
close(chjobs)
|
||||||
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,
|
|
||||||
RetryTimes: 3,
|
|
||||||
FilePath: filepath,
|
|
||||||
UploadId: uploadID,
|
|
||||||
Chunk: chunk,
|
|
||||||
Opt: partOpt,
|
|
||||||
}
|
|
||||||
chjobs <- job
|
|
||||||
}
|
|
||||||
close(chjobs)
|
|
||||||
|
|
||||||
// 5.Recv the resp etag to complete
|
// 5.Recv the resp etag to complete
|
||||||
for i := 0; i < partNum; i++ {
|
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)
|
event = newProgressEvent(ProgressDataEvent, chunks[res.PartNumber-1].Size, consumedBytes, totalBytes)
|
||||||
progressCallback(listener, event)
|
progressCallback(listener, event)
|
||||||
}
|
}
|
||||||
|
close(chresults)
|
||||||
sort.Sort(ObjectList(optcom.Parts))
|
sort.Sort(ObjectList(optcom.Parts))
|
||||||
|
|
||||||
event = newProgressEvent(ProgressCompletedEvent, 0, consumedBytes, totalBytes)
|
event = newProgressEvent(ProgressCompletedEvent, 0, consumedBytes, totalBytes)
|
||||||
|
|||||||
@@ -41,16 +41,17 @@ func (s *ObjectService) InitiateMultipartUpload(ctx context.Context, name string
|
|||||||
|
|
||||||
// ObjectUploadPartOptions is the options of upload-part
|
// ObjectUploadPartOptions is the options of upload-part
|
||||||
type ObjectUploadPartOptions struct {
|
type ObjectUploadPartOptions struct {
|
||||||
Expect string `header:"Expect,omitempty" url:"-"`
|
Expect string `header:"Expect,omitempty" url:"-"`
|
||||||
XCosContentSHA1 string `header:"x-cos-content-sha1,omitempty" url:"-"`
|
XCosContentSHA1 string `header:"x-cos-content-sha1,omitempty" url:"-"`
|
||||||
ContentLength int `header:"Content-Length,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:"-"`
|
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:"-"`
|
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:"-"`
|
XCosSSECustomerKeyMD5 string `header:"x-cos-server-side-encryption-customer-key-MD5,omitempty" url:"-" xml:"-"`
|
||||||
|
|
||||||
XCosTrafficLimit int `header:"x-cos-traffic-limit,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
|
// 上传进度, ProgressCompleteEvent不能表示对应API调用成功,API是否调用成功的判断标准为返回err==nil
|
||||||
Listener ProgressListener `header:"-" url:"-" xml:"-"`
|
Listener ProgressListener `header:"-" url:"-" xml:"-"`
|
||||||
}
|
}
|
||||||
@@ -64,6 +65,9 @@ type ObjectUploadPartOptions struct {
|
|||||||
//
|
//
|
||||||
// https://www.qcloud.com/document/product/436/7750
|
// 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) {
|
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 {
|
if opt != nil && opt.Listener != nil {
|
||||||
totalBytes, err := GetReaderLen(r)
|
totalBytes, err := GetReaderLen(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -97,6 +97,10 @@ func (r *teeReader) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *teeReader) Size() int64 {
|
||||||
|
return r.totalBytes
|
||||||
|
}
|
||||||
|
|
||||||
func TeeReader(reader io.Reader, writer io.Writer, total int64, listener ProgressListener) *teeReader {
|
func TeeReader(reader io.Reader, writer io.Writer, total int64, listener ProgressListener) *teeReader {
|
||||||
return &teeReader{
|
return &teeReader{
|
||||||
reader: reader,
|
reader: reader,
|
||||||
|
|||||||
Reference in New Issue
Block a user