Skip to content

Commit

Permalink
add bucket style api (#361)
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzong18 authored and huiguangjun committed Mar 23, 2023
1 parent b416f9d commit 8e7b954
Show file tree
Hide file tree
Showing 6 changed files with 310 additions and 1 deletion.
108 changes: 108 additions & 0 deletions oss/client.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -2349,6 +2349,114 @@ func (client Client) GetBucketResourceGroupXml(bucketName string, options ...Opt
return out, err
}

// PutBucketStyle set bucket's style
// bucketName the bucket name.
// styleContent the style content.
// error it's nil if no error, otherwise it's an error object.
func (client Client) PutBucketStyle(bucketName, styleName string, styleContent string, options ...Option) error {
bs := fmt.Sprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Style><Content>%s</Content></Style>", styleContent)
err := client.PutBucketStyleXml(bucketName, styleName, bs, options...)
return err
}

// PutBucketStyleXml set bucket's style
// bucketName the bucket name.
// styleName the style name.
// xmlData the style in xml format
// error it's nil if no error, otherwise it's an error object.
func (client Client) PutBucketStyleXml(bucketName, styleName, xmlData string, options ...Option) error {
buffer := new(bytes.Buffer)
buffer.Write([]byte(xmlData))
contentType := http.DetectContentType(buffer.Bytes())
headers := map[string]string{}
headers[HTTPHeaderContentType] = contentType
params := map[string]interface{}{}
params["style"] = nil
params["styleName"] = styleName
resp, err := client.do("PUT", bucketName, params, nil, buffer, options...)
if err != nil {
return err
}
defer resp.Body.Close()
return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
}

// GetBucketStyle get bucket's style
// bucketName the bucket name.
// styleName the bucket style name.
// GetBucketStyleResult the style result of bucket.
// error it's nil if no error, otherwise it's an error object.
func (client Client) GetBucketStyle(bucketName, styleName string, options ...Option) (GetBucketStyleResult, error) {
var out GetBucketStyleResult
body, err := client.GetBucketStyleXml(bucketName, styleName, options...)
err = xmlUnmarshal(strings.NewReader(body), &out)
return out, err
}

// GetBucketStyleXml get bucket's style
// bucketName the bucket name.
// styleName the bucket style name.
// string the style result of bucket in xml format.
// error it's nil if no error, otherwise it's an error object.
func (client Client) GetBucketStyleXml(bucketName, styleName string, options ...Option) (string, error) {
params := map[string]interface{}{}
params["style"] = nil
params["styleName"] = styleName
resp, err := client.do("GET", bucketName, params, nil, nil, options...)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
out := string(body)
return out, err
}

// ListBucketStyle get bucket's styles
// bucketName the bucket name.
// GetBucketListStyleResult the list style result of bucket.
// error it's nil if no error, otherwise it's an error object.
func (client Client) ListBucketStyle(bucketName string, options ...Option) (GetBucketListStyleResult, error) {
var out GetBucketListStyleResult
body, err := client.ListBucketStyleXml(bucketName, options...)
err = xmlUnmarshal(strings.NewReader(body), &out)
return out, err
}

// ListBucketStyleXml get bucket's list style
// bucketName the bucket name.
// string the style result of bucket in xml format.
// error it's nil if no error, otherwise it's an error object.
func (client Client) ListBucketStyleXml(bucketName string, options ...Option) (string, error) {
params := map[string]interface{}{}
params["style"] = nil
resp, err := client.do("GET", bucketName, params, nil, nil, options...)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
out := string(body)
return out, err
}

// DeleteBucketStyle delete bucket's style
// bucketName the bucket name.
// styleName the bucket style name.
// string the style result of bucket in xml format.
// error it's nil if no error, otherwise it's an error object.
func (client Client) DeleteBucketStyle(bucketName, styleName string, options ...Option) error {
params := map[string]interface{}{}
params["style"] = bucketName
params["styleName"] = styleName
resp, err := client.do("DELETE", bucketName, params, nil, nil, options...)
if err != nil {
return err
}
defer resp.Body.Close()
return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
}

// LimitUploadSpeed set upload bandwidth limit speed,default is 0,unlimited
// upSpeed KB/s, 0 is unlimited,default is 0
// error it's nil if success, otherwise failure
Expand Down
63 changes: 63 additions & 0 deletions oss/client_test.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -5262,3 +5262,66 @@ func (s *OssClientSuite) TestBucketResourceGroup(c *C) {
c.Assert(err, IsNil)
c.Assert(res.ResourceGroupId, Equals, "rg-acfmy7mo47b3adq")
}

// TestBucketStyle
func (s *OssClientSuite) TestBucketStyle(c *C) {
var bucketNameTest = bucketNamePrefix + "-acc-" + RandLowStr(6)
client, err := New(endpoint, accessID, accessKey)
c.Assert(err, IsNil)

err = client.CreateBucket(bucketNameTest)
c.Assert(err, IsNil)
time.Sleep(3 * time.Second)

// Put Bucket Style
style := "image/resize,p_50"
styleName := "image-" + RandLowStr(6)
err = client.PutBucketStyle(bucketNameTest, styleName, style)
c.Assert(err, IsNil)
time.Sleep(1 * time.Second)

// get bucket style
res, err := client.GetBucketStyle(bucketNameTest, styleName)
c.Assert(err, IsNil)
c.Assert(res.Name, Equals, styleName)
c.Assert(res.Content, Equals, "image/resize,p_50")
c.Assert(res.CreateTime != "", Equals, true)
c.Assert(res.LastModifyTime != "", Equals, true)

style1 := "image/resize,w_200"
styleName1 := "image-" + RandLowStr(6)
err = client.PutBucketStyle(bucketNameTest, styleName1, style1)
c.Assert(err, IsNil)
time.Sleep(1 * time.Second)

style2 := "image/resize,w_300"
styleName2 := "image-" + RandLowStr(6)
err = client.PutBucketStyle(bucketNameTest, styleName2, style2)
c.Assert(err, IsNil)
time.Sleep(1 * time.Second)

// list bucket style
list, err := client.ListBucketStyle(bucketNameTest)
c.Assert(err, IsNil)
c.Assert(len(list.Style), Equals, 3)

c.Assert(list.Style[1].Name, Equals, styleName1)
c.Assert(list.Style[1].Content, Equals, "image/resize,w_200")
c.Assert(list.Style[1].CreateTime != "", Equals, true)
c.Assert(list.Style[1].LastModifyTime != "", Equals, true)
c.Assert(list.Style[2].Name, Equals, styleName2)
c.Assert(list.Style[2].Content, Equals, "image/resize,w_300")
c.Assert(list.Style[2].CreateTime != "", Equals, true)
c.Assert(list.Style[2].LastModifyTime != "", Equals, true)

// delete bucket style
err = client.DeleteBucketStyle(bucketNameTest, styleName)
c.Assert(err, IsNil)

err = client.DeleteBucketStyle(bucketNameTest, styleName1)
c.Assert(err, IsNil)

err = client.DeleteBucketStyle(bucketNameTest, styleName2)
c.Assert(err, IsNil)

}
21 changes: 21 additions & 0 deletions oss/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -1531,3 +1531,24 @@ type BucketResourceGroupXml struct {
XMLName xml.Name `xml:"BucketResourceGroupConfiguration"`
ResourceGroupId string `xml:"ResourceGroupId"` // resource groupId
}

// GetBucketStyleResult define style for the bucket
type GetBucketStyleResult BucketStyleXml

// GetBucketListStyleResult define the list style for the bucket
type GetBucketListStyleResult BucketListStyleXml

// BucketListStyleXml define the list style of the bucket
type BucketListStyleXml struct {
XMLName xml.Name `xml:"StyleList"`
Style []BucketStyleXml `xml:"Style,omitempty"` // style
}

// BucketStyleXml define the information of the bucket's style
type BucketStyleXml struct {
XMLName xml.Name `xml:"Style"`
Name string `xml:"Name,omitempty"` // style name
Content string `xml:"Content"` // style content
CreateTime string `xml:"CreateTime,omitempty"` // style create time
LastModifyTime string `xml:"LastModifyTime,omitempty"` // style last modify time
}
56 changes: 55 additions & 1 deletion oss/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1136,4 +1136,58 @@ func (s *OssTypeSuite) TestPutBucketCname(c *C) {
bs, err = xml.Marshal(putCnameConfig2)
c.Assert(err, IsNil)
c.Assert(string(bs), Equals, "<BucketCnameConfiguration><Cname><Domain>www.aliyun.com</Domain><CertificateConfiguration><DeleteCertificate>true</DeleteCertificate></CertificateConfiguration></Cname></BucketCnameConfiguration>")
}
}

// Test Bucket Style
func (s *OssTypeSuite) TestBucketStyle(c *C) {
var res GetBucketStyleResult
xmlData := []byte(`<?xml version="1.0" encoding="UTF-8"?>
<Style>
<Name>imageStyle</Name>
<Content>image/resize,p_50</Content>
<CreateTime>Wed, 20 May 2020 12:07:15 GMT</CreateTime>
<LastModifyTime>Wed, 21 May 2020 12:07:15 GMT</LastModifyTime>
</Style>`)
err := xml.Unmarshal(xmlData, &res)
c.Assert(err, IsNil)
c.Assert(res.Name, Equals, "imageStyle")
c.Assert(res.Content, Equals, "image/resize,p_50")
c.Assert(res.CreateTime, Equals, "Wed, 20 May 2020 12:07:15 GMT")
c.Assert(res.LastModifyTime, Equals, "Wed, 21 May 2020 12:07:15 GMT")

var list GetBucketListStyleResult
xmlData = []byte(`<?xml version="1.0" encoding="UTF-8"?>
<StyleList>
<Style>
<Name>imageStyle</Name>
<Content>image/resize,p_50</Content>
<CreateTime>Wed, 20 May 2020 12:07:15 GMT</CreateTime>
<LastModifyTime>Wed, 21 May 2020 12:07:15 GMT</LastModifyTime>
</Style>
<Style>
<Name>imageStyle1</Name>
<Content>image/resize,w_200</Content>
<CreateTime>Wed, 20 May 2020 12:08:04 GMT</CreateTime>
<LastModifyTime>Wed, 21 May 2020 12:08:04 GMT</LastModifyTime>
</Style>
<Style>
<Name>imageStyle3</Name>
<Content>image/resize,w_300</Content>
<CreateTime>Fri, 12 Mar 2021 06:19:13 GMT</CreateTime>
<LastModifyTime>Fri, 13 Mar 2021 06:27:21 GMT</LastModifyTime>
</Style>
</StyleList>`)
err = xml.Unmarshal(xmlData, &list)
c.Assert(err, IsNil)
c.Assert(list.Style[0].Name, Equals, "imageStyle")
c.Assert(list.Style[0].Content, Equals, "image/resize,p_50")
c.Assert(list.Style[0].CreateTime, Equals, "Wed, 20 May 2020 12:07:15 GMT")
c.Assert(list.Style[0].LastModifyTime, Equals, "Wed, 21 May 2020 12:07:15 GMT")

c.Assert(err, IsNil)
c.Assert(list.Style[1].Name, Equals, "imageStyle1")
c.Assert(list.Style[2].Content, Equals, "image/resize,w_300")
c.Assert(list.Style[1].CreateTime, Equals, "Wed, 20 May 2020 12:08:04 GMT")
c.Assert(list.Style[2].LastModifyTime, Equals, "Fri, 13 Mar 2021 06:27:21 GMT")

}
1 change: 1 addition & 0 deletions sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var sampleMap = map[string]interface{}{
"BucketAccessMonitorSample": sample.BucketAccessMonitorSample,
"BucketResourceGroupSample": sample.BucketResourceGroupSample,
"BucketCnameSample": sample.BucketCnameSample,
"BucketStyleSample": sample.BucketStyleSample,
"ObjectACLSample": sample.ObjectACLSample,
"ObjectMetaSample": sample.ObjectMetaSample,
"ListObjectsSample": sample.ListObjectsSample,
Expand Down
62 changes: 62 additions & 0 deletions sample/bucket_style.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package sample

import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

// BucketStyleSample shows how to set,get list and delete the bucket's style.
func BucketStyleSample() {
// New client
client, err := oss.New(endpoint, accessID, accessKey)
if err != nil {
HandleError(err)
}

// Create the bucket with default parameters
err = client.CreateBucket(bucketName)
if err != nil {
HandleError(err)
}

// Get bucket's style.
styleName := "image-style"
style, err := client.GetBucketStyle(bucketName, styleName)
if err != nil {
HandleError(err)
}
fmt.Printf("Style Name:%s\n", style.Name)
fmt.Printf("Style Name:%s\n", style.Content)
fmt.Printf("Style Create Time:%s\n", style.CreateTime)
fmt.Printf("Style Last Modify Time:%s\n", style.LastModifyTime)

// Set bucket's style.
styleContent := "image/resize,p_50"
err = client.PutBucketStyle(bucketName, styleName, styleContent)
if err != nil {
HandleError(err)
}
fmt.Println("Put Bucket Style Success!")

// List bucket's style
list, err := client.ListBucketStyle(bucketName)
if err != nil {
HandleError(err)
}

for _, styleInfo := range list.Style {
fmt.Printf("Style Name:%s\n", styleInfo.Name)
fmt.Printf("Style Name:%s\n", styleInfo.Content)
fmt.Printf("Style Create Time:%s\n", styleInfo.CreateTime)
fmt.Printf("Style Last Modify Time:%s\n", styleInfo.LastModifyTime)
}

// Delete bucket's style
err = client.DeleteBucketStyle(bucketName, styleName)
if err != nil {
HandleError(err)
}
fmt.Println("Bucket Style Delete Success!")

fmt.Println("BucketStyleSample completed")
}

0 comments on commit 8e7b954

Please sign in to comment.