鸣镝 9 years ago
parent
commit
bf61848e2b
10 changed files with 48 additions and 71 deletions
  1. 13 19
      oss/bucket.go
  2. 1 1
      oss/client_test.go
  3. 2 2
      oss/crc_test.go
  4. 6 1
      oss/download.go
  5. 0 4
      oss/model.go
  6. 2 5
      oss/multipart.go
  7. 10 11
      oss/option.go
  8. 2 2
      oss/option_test.go
  9. 3 7
      oss/progress_test.go
  10. 9 19
      oss/upload.go

+ 13 - 19
oss/bucket.go

@@ -94,11 +94,9 @@ func (bucket Bucket) DoPutObject(request *PutObjectRequest, options []Option) (*
 		options = addContentType(options, request.ObjectKey)
 		options = addContentType(options, request.ObjectKey)
 	}
 	}
 
 
-	if request.Listener == nil {
-		request.Listener = getProgressListener(options)
-	}
+	listener := getProgressListener(options)
 
 
-	resp, err := bucket.do("PUT", request.ObjectKey, "", "", options, request.Reader, request.Listener)
+	resp, err := bucket.do("PUT", request.ObjectKey, "", "", options, request.Reader, listener)
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
@@ -127,7 +125,7 @@ func (bucket Bucket) DoPutObject(request *PutObjectRequest, options []Option) (*
 // error  操作无错误为nil,非nil为错误信息。
 // error  操作无错误为nil,非nil为错误信息。
 //
 //
 func (bucket Bucket) GetObject(objectKey string, options ...Option) (io.ReadCloser, error) {
 func (bucket Bucket) GetObject(objectKey string, options ...Option) (io.ReadCloser, error) {
-	result, err := bucket.DoGetObject(&GetObjectRequest{objectKey, nil}, options)
+	result, err := bucket.DoGetObject(&GetObjectRequest{objectKey}, options)
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
@@ -147,7 +145,7 @@ func (bucket Bucket) GetObjectToFile(objectKey, filePath string, options ...Opti
 	tempFilePath := filePath + TempFileSuffix
 	tempFilePath := filePath + TempFileSuffix
 
 
 	// 读取Object内容
 	// 读取Object内容
-	result, err := bucket.DoGetObject(&GetObjectRequest{objectKey, nil}, options)
+	result, err := bucket.DoGetObject(&GetObjectRequest{objectKey}, options)
 	if err != nil {
 	if err != nil {
 		return err
 		return err
 	}
 	}
@@ -209,11 +207,10 @@ func (bucket Bucket) DoGetObject(request *GetObjectRequest, options []Option) (*
 	}
 	}
 
 
 	// progress
 	// progress
-	if request.Listener == nil {
-		request.Listener = getProgressListener(options)
-	}
+	listener := getProgressListener(options)
+
 	contentLen, _ := strconv.ParseInt(resp.Headers.Get(HTTPHeaderContentLength), 10, 64)
 	contentLen, _ := strconv.ParseInt(resp.Headers.Get(HTTPHeaderContentLength), 10, 64)
-	resp.Body = ioutil.NopCloser(TeeReader(resp.Body, crcCalc, contentLen, request.Listener, nil))
+	resp.Body = ioutil.NopCloser(TeeReader(resp.Body, crcCalc, contentLen, listener, nil))
 
 
 	return result, nil
 	return result, nil
 }
 }
@@ -343,18 +340,16 @@ func (bucket Bucket) DoAppendObject(request *AppendObjectRequest, options []Opti
 	handleOptions(headers, opts)
 	handleOptions(headers, opts)
 
 
 	var initCRC uint64
 	var initCRC uint64
-	isCRCSet, initCRCStr, _ := isOptionSet(options, initCRC64)
+	isCRCSet, initCRCOpt, _ := isOptionSet(options, initCRC64)
 	if isCRCSet {
 	if isCRCSet {
-		initCRC, _ = strconv.ParseUint(initCRCStr.(string), 10, 64)
+		initCRC = initCRCOpt.(uint64)
 	}
 	}
 
 
-	if request.Listener == nil {
-		request.Listener = getProgressListener(options)
-	}
+	listener := getProgressListener(options)
 
 
 	handleOptions(headers, opts)
 	handleOptions(headers, opts)
 	resp, err := bucket.Client.Conn.Do("POST", bucket.BucketName, request.ObjectKey, params, params, headers,
 	resp, err := bucket.Client.Conn.Do("POST", bucket.BucketName, request.ObjectKey, params, params, headers,
-		request.Reader, initCRC, request.Listener)
+		request.Reader, initCRC, listener)
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
@@ -407,9 +402,8 @@ func (bucket Bucket) DeleteObjects(objectKeys []string, options ...Option) (Dele
 	for _, key := range objectKeys {
 	for _, key := range objectKeys {
 		dxml.Objects = append(dxml.Objects, DeleteObject{Key: key})
 		dxml.Objects = append(dxml.Objects, DeleteObject{Key: key})
 	}
 	}
-	isQuietStr, _ := findOption(options, deleteObjectsQuiet, "FALSE")
-	isQuiet, _ := strconv.ParseBool(isQuietStr.(string))
-	dxml.Quiet = isQuiet
+	isQuiet, _ := findOption(options, deleteObjectsQuiet, false)
+	dxml.Quiet = isQuiet.(bool)
 	encode := "&encoding-type=url"
 	encode := "&encoding-type=url"
 
 
 	bs, err := xml.Marshal(dxml)
 	bs, err := xml.Marshal(dxml)

+ 1 - 1
oss/client_test.go

@@ -47,7 +47,7 @@ const (
 	bucketName = "go-sdk-test-bucket-xyza-for-object"
 	bucketName = "go-sdk-test-bucket-xyza-for-object"
 	// object name for object ops test
 	// object name for object ops test
 	objectNamePrefix = "go-sdk-test-object-xyza-"
 	objectNamePrefix = "go-sdk-test-object-xyza-"
-
+	// sts region is one and only hangzhou
 	stsRegion = "cn-hangzhou"
 	stsRegion = "cn-hangzhou"
 )
 )
 
 

+ 2 - 2
oss/crc_test.go

@@ -150,7 +150,7 @@ func (s *OssCrcSuite) TestEnableCRCAndMD5(c *C) {
 	body.Close()
 	body.Close()
 
 
 	// GetObjectWithCRC
 	// GetObjectWithCRC
-	getResult, err := bucket.DoGetObject(&GetObjectRequest{objectName, nil}, nil)
+	getResult, err := bucket.DoGetObject(&GetObjectRequest{objectName}, nil)
 	c.Assert(err, IsNil)
 	c.Assert(err, IsNil)
 	str, err := readBody(getResult.Response.Body)
 	str, err := readBody(getResult.Response.Body)
 	c.Assert(err, IsNil)
 	c.Assert(err, IsNil)
@@ -247,7 +247,7 @@ func (s *OssCrcSuite) TestDisableCRCAndMD5(c *C) {
 	body.Close()
 	body.Close()
 
 
 	// GetObjectWithCRC
 	// GetObjectWithCRC
-	getResult, err := bucket.DoGetObject(&GetObjectRequest{objectName, nil}, nil)
+	getResult, err := bucket.DoGetObject(&GetObjectRequest{objectName}, nil)
 	c.Assert(err, IsNil)
 	c.Assert(err, IsNil)
 	str, err := readBody(getResult.Response.Body)
 	str, err := readBody(getResult.Response.Body)
 	c.Assert(err, IsNil)
 	c.Assert(err, IsNil)

+ 6 - 1
oss/download.go

@@ -76,9 +76,14 @@ func downloadWorker(id int, arg downloadWorkerArg, jobs <-chan downloadPart, res
 			break
 			break
 		}
 		}
 
 
+		// resolve options
 		r := Range(part.Start, part.End)
 		r := Range(part.Start, part.End)
 		p := Progress(&defaultDownloadProgressListener{})
 		p := Progress(&defaultDownloadProgressListener{})
-		opts := append(arg.options, r, p)
+		opts := make([]Option, len(arg.options)+2)
+		// append orderly, can not be reversed!
+		opts = append(opts, arg.options...)
+		opts = append(opts, r, p)
+
 		rd, err := arg.bucket.GetObject(arg.key, opts...)
 		rd, err := arg.bucket.GetObject(arg.key, opts...)
 		if err != nil {
 		if err != nil {
 			failed <- err
 			failed <- err

+ 0 - 4
oss/model.go

@@ -19,13 +19,11 @@ type Response struct {
 type PutObjectRequest struct {
 type PutObjectRequest struct {
 	ObjectKey string
 	ObjectKey string
 	Reader    io.Reader
 	Reader    io.Reader
-	Listener  ProgressListener
 }
 }
 
 
 // GetObjectRequest The request of DoGetObject
 // GetObjectRequest The request of DoGetObject
 type GetObjectRequest struct {
 type GetObjectRequest struct {
 	ObjectKey string
 	ObjectKey string
-	Listener  ProgressListener
 }
 }
 
 
 // GetObjectResult The result of DoGetObject
 // GetObjectResult The result of DoGetObject
@@ -40,7 +38,6 @@ type AppendObjectRequest struct {
 	ObjectKey string
 	ObjectKey string
 	Reader    io.Reader
 	Reader    io.Reader
 	Position  int64
 	Position  int64
-	Listener  ProgressListener
 }
 }
 
 
 // AppendObjectResult The result of DoAppendObject
 // AppendObjectResult The result of DoAppendObject
@@ -55,7 +52,6 @@ type UploadPartRequest struct {
 	Reader     io.Reader
 	Reader     io.Reader
 	PartSize   int64
 	PartSize   int64
 	PartNumber int
 	PartNumber int
-	Listener   ProgressListener
 }
 }
 
 
 // UploadPartResult The result of DoUploadPart
 // UploadPartResult The result of DoUploadPart

+ 2 - 5
oss/multipart.go

@@ -110,14 +110,11 @@ func (bucket Bucket) UploadPartFromFile(imur InitiateMultipartUploadResult, file
 // error  操作无错误为nil,非nil为错误信息。
 // error  操作无错误为nil,非nil为错误信息。
 //
 //
 func (bucket Bucket) DoUploadPart(request *UploadPartRequest, options []Option) (*UploadPartResult, error) {
 func (bucket Bucket) DoUploadPart(request *UploadPartRequest, options []Option) (*UploadPartResult, error) {
-	if request.Listener == nil {
-		request.Listener = getProgressListener(options)
-	}
-
+	listener := getProgressListener(options)
 	params := "partNumber=" + strconv.Itoa(request.PartNumber) + "&uploadId=" + request.InitResult.UploadID
 	params := "partNumber=" + strconv.Itoa(request.PartNumber) + "&uploadId=" + request.InitResult.UploadID
 	opts := []Option{ContentLength(request.PartSize)}
 	opts := []Option{ContentLength(request.PartSize)}
 	resp, err := bucket.do("PUT", request.InitResult.Key, params, params, opts,
 	resp, err := bucket.do("PUT", request.InitResult.Key, params, params, opts,
-		&io.LimitedReader{R: request.Reader, N: request.PartSize}, request.Listener)
+		&io.LimitedReader{R: request.Reader, N: request.PartSize}, listener)
 	if err != nil {
 	if err != nil {
 		return &UploadPartResult{}, err
 		return &UploadPartResult{}, err
 	}
 	}

+ 10 - 11
oss/option.go

@@ -2,7 +2,6 @@ package oss
 
 
 import (
 import (
 	"bytes"
 	"bytes"
-	"encoding/json"
 	"fmt"
 	"fmt"
 	"net/http"
 	"net/http"
 	"net/url"
 	"net/url"
@@ -206,9 +205,10 @@ func UploadIDMarker(value string) Option {
 
 
 // DeleteObjectsQuiet DeleteObjects详细(verbose)模式或简单(quiet)模式,默认详细模式。
 // DeleteObjectsQuiet DeleteObjects详细(verbose)模式或简单(quiet)模式,默认详细模式。
 func DeleteObjectsQuiet(isQuiet bool) Option {
 func DeleteObjectsQuiet(isQuiet bool) Option {
-	return addArg(deleteObjectsQuiet, strconv.FormatBool(isQuiet))
+	return addArg(deleteObjectsQuiet, isQuiet)
 }
 }
 
 
+// 断点续传配置,包括是否启用、cp文件
 type cpConfig struct {
 type cpConfig struct {
 	IsEnable bool
 	IsEnable bool
 	FilePath string
 	FilePath string
@@ -216,18 +216,17 @@ type cpConfig struct {
 
 
 // Checkpoint DownloadFile/UploadFile是否开启checkpoint及checkpoint文件路径
 // Checkpoint DownloadFile/UploadFile是否开启checkpoint及checkpoint文件路径
 func Checkpoint(isEnable bool, filePath string) Option {
 func Checkpoint(isEnable bool, filePath string) Option {
-	res, _ := json.Marshal(cpConfig{isEnable, filePath})
-	return addArg(checkpointConfig, string(res))
+	return addArg(checkpointConfig, &cpConfig{isEnable, filePath})
 }
 }
 
 
 // Routines DownloadFile/UploadFile并发数
 // Routines DownloadFile/UploadFile并发数
 func Routines(n int) Option {
 func Routines(n int) Option {
-	return addArg(routineNum, strconv.Itoa(n))
+	return addArg(routineNum, n)
 }
 }
 
 
 // InitCRC AppendObject CRC的校验的初始值
 // InitCRC AppendObject CRC的校验的初始值
 func InitCRC(initCRC uint64) Option {
 func InitCRC(initCRC uint64) Option {
-	return addArg(initCRC64, strconv.FormatUint(initCRC, 10))
+	return addArg(initCRC64, initCRC)
 }
 }
 
 
 // Progress set progress listener
 // Progress set progress listener
@@ -237,7 +236,7 @@ func Progress(listener ProgressListener) Option {
 
 
 func setHeader(key string, value interface{}) Option {
 func setHeader(key string, value interface{}) Option {
 	return func(params map[string]optionValue) error {
 	return func(params map[string]optionValue) error {
-		if value == "" {
+		if value == nil {
 			return nil
 			return nil
 		}
 		}
 		params[key] = optionValue{value, optionHTTP}
 		params[key] = optionValue{value, optionHTTP}
@@ -247,7 +246,7 @@ func setHeader(key string, value interface{}) Option {
 
 
 func addParam(key string, value interface{}) Option {
 func addParam(key string, value interface{}) Option {
 	return func(params map[string]optionValue) error {
 	return func(params map[string]optionValue) error {
-		if value == "" {
+		if value == nil {
 			return nil
 			return nil
 		}
 		}
 		params[key] = optionValue{value, optionParam}
 		params[key] = optionValue{value, optionParam}
@@ -257,7 +256,7 @@ func addParam(key string, value interface{}) Option {
 
 
 func addArg(key string, value interface{}) Option {
 func addArg(key string, value interface{}) Option {
 	return func(params map[string]optionValue) error {
 	return func(params map[string]optionValue) error {
-		if value == "" {
+		if value == nil {
 			return nil
 			return nil
 		}
 		}
 		params[key] = optionValue{value, optionArg}
 		params[key] = optionValue{value, optionArg}
@@ -324,7 +323,7 @@ func findOption(options []Option, param string, defaultVal interface{}) (interfa
 	for _, option := range options {
 	for _, option := range options {
 		if option != nil {
 		if option != nil {
 			if err := option(params); err != nil {
 			if err := option(params); err != nil {
-				return "", err
+				return nil, err
 			}
 			}
 		}
 		}
 	}
 	}
@@ -340,7 +339,7 @@ func isOptionSet(options []Option, option string) (bool, interface{}, error) {
 	for _, option := range options {
 	for _, option := range options {
 		if option != nil {
 		if option != nil {
 			if err := option(params); err != nil {
 			if err := option(params); err != nil {
-				return false, "", err
+				return false, nil, err
 			}
 			}
 		}
 		}
 	}
 	}

+ 2 - 2
oss/option_test.go

@@ -214,7 +214,7 @@ func (s *OssOptionSuite) TestHandleOptions(c *C) {
 	headers = map[string]string{}
 	headers = map[string]string{}
 	err = handleOptions(headers, options)
 	err = handleOptions(headers, options)
 	c.Assert(err, IsNil)
 	c.Assert(err, IsNil)
-	c.Assert(len(headers), Equals, 0)
+	c.Assert(len(headers), Equals, 1)
 }
 }
 
 
 func (s *OssOptionSuite) TestHandleParams(c *C) {
 func (s *OssOptionSuite) TestHandleParams(c *C) {
@@ -230,7 +230,7 @@ func (s *OssOptionSuite) TestHandleParams(c *C) {
 
 
 	options = []Option{KeyMarker(""), nil}
 	options = []Option{KeyMarker(""), nil}
 	out, err = handleParams(options)
 	out, err = handleParams(options)
-	c.Assert(out, Equals, "")
+	c.Assert(out, Equals, "key-marker=")
 	c.Assert(err, IsNil)
 	c.Assert(err, IsNil)
 }
 }
 
 

+ 3 - 7
oss/progress_test.go

@@ -132,10 +132,9 @@ func (s *OssProgressSuite) TestPutObject(c *C) {
 	request := &PutObjectRequest{
 	request := &PutObjectRequest{
 		ObjectKey: objectName,
 		ObjectKey: objectName,
 		Reader:    fd,
 		Reader:    fd,
-		Listener:  &OssProgressListener{},
 	}
 	}
 
 
-	options := []Option{}
+	options := []Option{Progress(&OssProgressListener{})}
 	_, err = s.bucket.DoPutObject(request, options)
 	_, err = s.bucket.DoPutObject(request, options)
 	c.Assert(err, IsNil)
 	c.Assert(err, IsNil)
 
 
@@ -259,11 +258,8 @@ func (s *OssProgressSuite) TestGetObject(c *C) {
 	c.Assert(err, IsNil)
 	c.Assert(err, IsNil)
 
 
 	// DoGetObject
 	// DoGetObject
-	request := &GetObjectRequest{
-		objectName,
-		&OssProgressListener{},
-	}
-	options := []Option{}
+	request := &GetObjectRequest{objectName}
+	options := []Option{Progress(&OssProgressListener{})}
 	result, err := s.bucket.DoGetObject(request, options)
 	result, err := s.bucket.DoGetObject(request, options)
 	c.Assert(err, IsNil)
 	c.Assert(err, IsNil)
 	_, err = ioutil.ReadAll(result.Response.Body)
 	_, err = ioutil.ReadAll(result.Response.Body)

+ 9 - 19
oss/upload.go

@@ -7,7 +7,6 @@ import (
 	"errors"
 	"errors"
 	"io/ioutil"
 	"io/ioutil"
 	"os"
 	"os"
-	"strconv"
 	"time"
 	"time"
 )
 )
 
 
@@ -44,37 +43,28 @@ func (bucket Bucket) UploadFile(objectKey, filePath string, partSize int64, opti
 
 
 // 获取Checkpoint配置
 // 获取Checkpoint配置
 func getCpConfig(options []Option, filePath string) (*cpConfig, error) {
 func getCpConfig(options []Option, filePath string) (*cpConfig, error) {
-	cpc := cpConfig{}
-	cpStr, err := findOption(options, checkpointConfig, "")
-	if err != nil {
-		return nil, err
-	}
-
-	if cpStr != "" {
-		if err = json.Unmarshal([]byte(cpStr.(string)), &cpc); err != nil {
-			return nil, err
-		}
+	cpc := &cpConfig{}
+	cpcOpt, err := findOption(options, checkpointConfig, nil)
+	if err != nil || cpcOpt == nil {
+		return cpc, err
 	}
 	}
 
 
+	cpc = cpcOpt.(*cpConfig)
 	if cpc.IsEnable && cpc.FilePath == "" {
 	if cpc.IsEnable && cpc.FilePath == "" {
 		cpc.FilePath = filePath + CheckpointFileSuffix
 		cpc.FilePath = filePath + CheckpointFileSuffix
 	}
 	}
 
 
-	return &cpc, nil
+	return cpc, nil
 }
 }
 
 
 // 获取并发数,默认并发数1
 // 获取并发数,默认并发数1
 func getRoutines(options []Option) int {
 func getRoutines(options []Option) int {
-	rStr, err := findOption(options, routineNum, "")
-	if err != nil || rStr == "" {
-		return 1
-	}
-
-	rs, err := strconv.Atoi(rStr.(string))
-	if err != nil {
+	rtnOpt, err := findOption(options, routineNum, nil)
+	if err != nil || rtnOpt == nil {
 		return 1
 		return 1
 	}
 	}
 
 
+	rs := rtnOpt.(int)
 	if rs < 1 {
 	if rs < 1 {
 		rs = 1
 		rs = 1
 	} else if rs > 100 {
 	} else if rs > 100 {