Skip to content

Commit

Permalink
remove DoCreateBucket
Browse files Browse the repository at this point in the history
  • Loading branch information
dengwu12 committed Apr 25, 2017
1 parent 455811b commit 82da2e8
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 46 deletions.
4 changes: 2 additions & 2 deletions oss/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,12 +640,12 @@ func (bucket Bucket) GetSymlink(objectKey string) (http.Header, error) {
}
defer resp.Body.Close()

targetObjectKey := resp.Headers.Get(HTTPHeaderOSSSymlinkTarget)
targetObjectKey := resp.Headers.Get(HTTPHeaderOssSymlinkTarget)
targetObjectKey, err = url.QueryUnescape(targetObjectKey)
if err != nil {
return resp.Headers, err
}
resp.Headers.Set(HTTPHeaderOSSSymlinkTarget, targetObjectKey)
resp.Headers.Set(HTTPHeaderOssSymlinkTarget, targetObjectKey)
return resp.Headers, err
}

Expand Down
7 changes: 3 additions & 4 deletions oss/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func (s *OssBucketSuite) SetUpSuite(c *C) {

s.client.CreateBucket(bucketName)

cbConfig := CreateBucketConfiguration{StorageClass: StorageArchive}
err = s.client.DoCreateBucket(archiveBucketName, cbConfig)
err = s.client.CreateBucket(archiveBucketName, StorageClass(StorageArchive))
c.Assert(err, IsNil)

time.Sleep(5 * time.Second)
Expand Down Expand Up @@ -1549,7 +1548,7 @@ func (s *OssBucketSuite) TestSymlink(c *C) {

meta, err = s.bucket.GetSymlink(objectName)
c.Assert(err, IsNil)
c.Assert(meta.Get(HTTPHeaderOSSSymlinkTarget), Equals, targetObjectName)
c.Assert(meta.Get(HTTPHeaderOssSymlinkTarget), Equals, targetObjectName)

// List object
lor, err := s.bucket.ListObjects()
Expand Down Expand Up @@ -1594,7 +1593,7 @@ func (s *OssBucketSuite) TestSymlink(c *C) {

meta, err = s.bucket.GetSymlink(objectName)
c.Assert(err, IsNil)
c.Assert(meta.Get(HTTPHeaderOSSSymlinkTarget), Equals, targetObjectName)
c.Assert(meta.Get(HTTPHeaderOssSymlinkTarget), Equals, targetObjectName)

body, err = s.bucket.GetObject(objectName)
c.Assert(err, IsNil)
Expand Down
48 changes: 18 additions & 30 deletions oss/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,47 +89,35 @@ func (client Client) Bucket(bucketName string) (*Bucket, error) {
// bucketName bucket名称,在整个OSS中具有全局唯一性,且不能修改。bucket名称的只能包括小写字母,数字和短横线-,
// 必须以小写字母或者数字开头,长度必须在3-255字节之间。
// options 创建bucket的选项。您可以使用选项ACL,指定bucket的访问权限。Bucket有以下三种访问权限,私有读写(ACLPrivate)、
// 公共读私有写(ACLPublicRead),公共读公共写(ACLPublicReadWrite),默认访问权限是私有读写。
// 公共读私有写(ACLPublicRead),公共读公共写(ACLPublicReadWrite),默认访问权限是私有读写。可以使用StorageClass选项设置bucket的存储方式。
//
// error 操作无错误时返回nil,非nil为错误信息。
//
func (client Client) CreateBucket(bucketName string, options ...Option) error {
headers := make(map[string]string)
handleOptions(headers, options)

resp, err := client.do("PUT", bucketName, "", "", headers, nil)
if err != nil {
return err
}
var resp *Response
var err error

defer resp.Body.Close()
return checkRespCode(resp.StatusCode, []int{http.StatusOK})
}
isOptSet, val, _ := isOptionSet(options, storageClass)
if isOptSet {
cbConfig := createBucketConfiguration{StorageClass: val.(StorageClassType)}
bs, err := xml.Marshal(cbConfig)
if err != nil {
return err
}
buffer := new(bytes.Buffer)
buffer.Write(bs)

//
// DoCreateBucket 创建Bucket。
//
// bucketName bucket名称,在整个OSS中具有全局唯一性,且不能修改。bucket名称的只能包括小写字母,数字和短横线-,
// 必须以小写字母或者数字开头,长度必须在3-255字节之间。
// options 创建bucket的选项。您可以使用选项ACL,指定bucket的访问权限。Bucket有以下三种访问权限,私有读写(ACLPrivate)、
// 公共读私有写(ACLPublicRead),公共读公共写(ACLPublicReadWrite),默认访问权限是私有读写。
//
// error 操作无错误时返回nil,非nil为错误信息。
//
func (client Client) DoCreateBucket(bucketName string, cbConfig CreateBucketConfiguration, options ...Option) error {
bs, err := xml.Marshal(cbConfig)
if err != nil {
return err
}
buffer := new(bytes.Buffer)
buffer.Write(bs)
contentType := http.DetectContentType(buffer.Bytes())
headers[HTTPHeaderContentType] = contentType

contentType := http.DetectContentType(buffer.Bytes())
headers := make(map[string]string)
handleOptions(headers, options)
headers[HTTPHeaderContentType] = contentType
resp, err = client.do("PUT", bucketName, "", "", headers, buffer)
} else {
resp, err = client.do("PUT", bucketName, "", "", headers, nil)
}

resp, err := client.do("PUT", bucketName, "", "", headers, buffer)
if err != nil {
return err
}
Expand Down
9 changes: 3 additions & 6 deletions oss/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,7 @@ func (s *OssClientSuite) TestCreateBucket(c *C) {
// create bucket with config and test get bucket info
for _, storage := range []StorageClassType{StorageStandard, StorageIA, StorageArchive} {
bucketNameTest := bucketNamePrefix + randLowStr(5)
cbConfig := CreateBucketConfiguration{StorageClass: storage}
err = client.DoCreateBucket(bucketNameTest, cbConfig, ACL(ACLPublicRead))
err = client.CreateBucket(bucketNameTest, StorageClass(storage), ACL(ACLPublicRead))
c.Assert(err, IsNil)

res, err := client.GetBucketInfo(bucketNameTest)
Expand All @@ -223,15 +222,13 @@ func (s *OssClientSuite) TestCreateBucket(c *C) {
}

// error put bucket with config
cbConfig := CreateBucketConfiguration{StorageClass: StorageArchive}
err = client.DoCreateBucket("ERRORBUCKETNAME", cbConfig)
err = client.CreateBucket("ERRORBUCKETNAME", StorageClass(StorageArchive))
c.Assert(err, NotNil)

// create bucket with config and test list bucket
for _, storage := range []StorageClassType{StorageStandard, StorageIA, StorageArchive} {
bucketNameTest := bucketNamePrefix + randLowStr(5)
cbConfig := CreateBucketConfiguration{StorageClass: storage}
err = client.DoCreateBucket(bucketNameTest, cbConfig)
err = client.CreateBucket(bucketNameTest, StorageClass(storage))
c.Assert(err, IsNil)

res, err := client.ListBuckets()
Expand Down
2 changes: 1 addition & 1 deletion oss/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const (
HTTPHeaderOssNextAppendPosition = "X-Oss-Next-Append-Position"
HTTPHeaderOssRequestID = "X-Oss-Request-Id"
HTTPHeaderOssCRC64 = "X-Oss-Hash-Crc64ecma"
HTTPHeaderOSSSymlinkTarget = "X-Oss-Symlink-Target"
HTTPHeaderOssSymlinkTarget = "X-Oss-Symlink-Target"
)

// 其它常量
Expand Down
8 changes: 7 additions & 1 deletion oss/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
checkpointConfig = "x-cp-config"
initCRC64 = "init-crc64"
progressListener = "x-progress-listener"
storageClass = "storage-class"
)

type (
Expand Down Expand Up @@ -166,7 +167,7 @@ func ObjectACL(acl ACLType) Option {

// symlinkTarget is an option to set X-Oss-Symlink-Target
func symlinkTarget(targetObjectKey string) Option {
return setHeader(HTTPHeaderOSSSymlinkTarget, targetObjectKey)
return setHeader(HTTPHeaderOssSymlinkTarget, targetObjectKey)
}

// Origin is an option to set Origin header
Expand Down Expand Up @@ -219,6 +220,11 @@ func DeleteObjectsQuiet(isQuiet bool) Option {
return addArg(deleteObjectsQuiet, isQuiet)
}

// StorageClass bucket的存储方式
func StorageClass(value StorageClassType) Option {
return addArg(storageClass, value)
}

// 断点续传配置,包括是否启用、cp文件
type cpConfig struct {
IsEnable bool
Expand Down
4 changes: 2 additions & 2 deletions oss/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,8 @@ func decodeListMultipartUploadResult(result *ListMultipartUploadResult) error {
return nil
}

// CreateBucketConfiguration 规则的过期属性
type CreateBucketConfiguration struct {
// createBucketConfiguration 规则的过期属性
type createBucketConfiguration struct {
XMLName xml.Name `xml:"CreateBucketConfiguration"`
StorageClass StorageClassType `xml:"StorageClass,omitempty"`
}

0 comments on commit 82da2e8

Please sign in to comment.