Skip to content

Commit

Permalink
add CreateBucketXml
Browse files Browse the repository at this point in the history
  • Loading branch information
taowei.wtw authored and kkuai committed Nov 18, 2021
1 parent bb443e8 commit 2273d4f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
18 changes: 18 additions & 0 deletions oss/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,24 @@ func (client Client) CreateBucket(bucketName string, options ...Option) error {
return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
}

// create bucket xml
func (client Client) CreateBucketXml(bucketName string, xmlBody string, options ...Option) error {
buffer := new(bytes.Buffer)
buffer.Write([]byte(xmlBody))
contentType := http.DetectContentType(buffer.Bytes())
headers := map[string]string{}
headers[HTTPHeaderContentType] = contentType

params := map[string]interface{}{}
resp, err := client.do("PUT", bucketName, params, headers, buffer, options...)
if err != nil {
return err
}

defer resp.Body.Close()
return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
}

// ListBuckets lists buckets of the current account under the given endpoint, with optional filters.
//
// options specifies the filters such as Prefix, Marker and MaxKeys. Prefix is the bucket name's prefix filter.
Expand Down
23 changes: 23 additions & 0 deletions oss/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4483,3 +4483,26 @@ func (s *OssClientSuite) TestGetBucketCName(c *C) {
err = client.DeleteBucket(bucketName)
c.Assert(err, IsNil)
}

func (s *OssClientSuite) TestCreateBucketXml(c *C) {
client, err := New(endpoint, accessID, accessKey)
c.Assert(err, IsNil)

bucketName := bucketNamePrefix + RandLowStr(5)
xmlBody := `
<?xml version="1.0" encoding="UTF-8"?>
<CreateBucketConfiguration>
<StorageClass>IA</StorageClass>
</CreateBucketConfiguration>
`
err = client.CreateBucketXml(bucketName,xmlBody)
c.Assert(err, IsNil)

//check
bucketInfo,_:= client.GetBucketInfo(bucketName)
c.Assert(bucketInfo.BucketInfo.StorageClass, Equals, "IA")
err = client.DeleteBucket(bucketName)
c.Assert(err, IsNil)
}


2 changes: 1 addition & 1 deletion oss/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ const (

NullVersion = "null"

Version = "v2.1.11" // Go SDK version
Version = "v2.2.0" // Go SDK version
)

// FrameType
Expand Down

0 comments on commit 2273d4f

Please sign in to comment.