Skip to content

Commit

Permalink
add repsonse header api
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzong18 committed Oct 19, 2023
1 parent dec10ed commit ca15fd2
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 5 deletions.
80 changes: 80 additions & 0 deletions oss/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2537,6 +2537,86 @@ func (client Client) DeleteBucketStyle(bucketName, styleName string, options ...
return CheckRespCode(resp.StatusCode, []int{http.StatusNoContent})
}

// PutBucketResponseHeader set bucket response header
// bucketName the bucket name.
// xmlData the resource group in xml format
// error it's nil if no error, otherwise it's an error object.
func (client Client) PutBucketResponseHeader(bucketName string, responseHeader PutBucketResponseHeader, options ...Option) error {
bs, err := xml.Marshal(responseHeader)
if err != nil {
return err
}
err = client.PutBucketResponseHeaderXml(bucketName, string(bs), options...)
return err
}

// PutBucketResponseHeaderXml set bucket response header
// bucketName the bucket name.
// xmlData the bucket response header in xml format
// error it's nil if no error, otherwise it's an error object.
func (client Client) PutBucketResponseHeaderXml(bucketName, 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["responseHeader"] = nil
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})
}

// GetBucketResponseHeader get bucket's response header.
// bucketName the bucket name.
// GetBucketResponseHeaderResult the response header result of bucket.
// error it's nil if no error, otherwise it's an error object.
func (client Client) GetBucketResponseHeader(bucketName string, options ...Option) (GetBucketResponseHeaderResult, error) {
var out GetBucketResponseHeaderResult
body, err := client.GetBucketResponseHeaderXml(bucketName, options...)
if err != nil {
return out, err
}
fmt.Printf("body:%s\n", body)
err = xmlUnmarshal(strings.NewReader(body), &out)
return out, err
}

// GetBucketResponseHeaderXml get bucket's resource group
// bucketName the bucket name.
// string the response header result of bucket xml format.
// error it's nil if no error, otherwise it's an error object.
func (client Client) GetBucketResponseHeaderXml(bucketName string, options ...Option) (string, error) {
params := map[string]interface{}{}
params["responseHeader"] = 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
}

// DeleteBucketResponseHeader delete response header from a bucket.
// bucketName the bucket name.
// error it's nil if no error, otherwise it's an error object.
func (client Client) DeleteBucketResponseHeader(bucketName string, options ...Option) error {
params := map[string]interface{}{}
params["responseHeader"] = nil
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})
}

// DescribeRegions get describe regions
// GetDescribeRegionsResult the result of bucket in xml format.
// error it's nil if no error, otherwise it's an error object.
Expand Down
37 changes: 37 additions & 0 deletions oss/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5805,3 +5805,40 @@ func (s *OssClientSuite) TestDescribeRegions(c *C) {
c.Assert(info.Regions[0].InternalEndpoint, Equals, "oss-cn-hangzhou-internal.aliyuncs.com")
c.Assert(info.Regions[0].AccelerateEndpoint, Equals, "oss-accelerate.aliyuncs.com")
}

// TestDescribeRegions
func (s *OssClientSuite) TestBucketResponseHeader(c *C) {
client, err := New(endpoint, accessID, accessKey)
c.Assert(err, IsNil)
reqHeader := PutBucketResponseHeader{
Rule: []BucketResponseHeaderRule{
{
Name: "name1",
Filters: []string{
"Put", "GetObject",
},
HideHeaders: []string{"Last-Modified"},
},
{
Name: "name2",
Filters: []string{
"*",
},
HideHeaders: []string{"Last-Modified"},
},
},
}

err = client.PutBucketResponseHeader(bucketName, reqHeader)
c.Assert(err, IsNil)

rule, err := client.GetBucketResponseHeader(bucketName)
c.Assert(err, IsNil)
c.Assert(len(rule.Rule), Equals, 2)
c.Assert(rule.Rule[0].Name, Equals, "name1")
c.Assert(rule.Rule[1].Name, Equals, "name2")
c.Assert(rule.Rule[0].Filters[0], Equals, "Put")
c.Assert(rule.Rule[0].Filters[1], Equals, "GetObject")
err = client.DeleteBucketResponseHeader(bucketName)
c.Assert(err, IsNil)
}
2 changes: 1 addition & 1 deletion oss/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var signKeyList = []string{"acl", "uploads", "location", "cors",
"x-oss-enable-md5", "x-oss-enable-sha1", "x-oss-enable-sha256",
"x-oss-hash-ctx", "x-oss-md5-ctx", "transferAcceleration",
"regionList", "cloudboxes", "x-oss-ac-source-ip", "x-oss-ac-subnet-mask", "x-oss-ac-vpc-id", "x-oss-ac-forward-allow",
"metaQuery", "resourceGroup", "rtc", "x-oss-async-process",
"metaQuery", "resourceGroup", "rtc", "x-oss-async-process", "responseHeader",
}

// init initializes Conn
Expand Down
8 changes: 4 additions & 4 deletions oss/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const (
StorageDeepColdArchive StorageClassType = "DeepColdArchive"
)

// RedundancyType bucket data Redundancy type
//RedundancyType bucket data Redundancy type
type DataRedundancyType string

const (
Expand All @@ -92,7 +92,7 @@ const (
RedundancyZRS DataRedundancyType = "ZRS"
)

// ObjecthashFuncType
//ObjecthashFuncType
type ObjecthashFuncType string

const (
Expand All @@ -111,7 +111,7 @@ const (
BucketOwner PayerType = "BucketOwner"
)

// RestoreMode the restore mode for coldArchive object
//RestoreMode the restore mode for coldArchive object
type RestoreMode string

const (
Expand Down Expand Up @@ -245,7 +245,7 @@ const (

DefaultContentSha256 = "UNSIGNED-PAYLOAD" // for v4 signature

Version = "v2.2.9" // Go SDK version
Version = "v2.2.8" // Go SDK version
)

// FrameType
Expand Down
17 changes: 17 additions & 0 deletions oss/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -1663,3 +1663,20 @@ type RegionInfoList struct {
XMLName xml.Name `xml:"RegionInfoList"`
Regions []RegionInfo `xml:"RegionInfo"`
}

//PutBucketResponseHeader define the xml of bucket's response header config
type PutBucketResponseHeader BucketResponseHeaderXml

//GetBucketResponseHeaderResult define the xml of bucket's response header result
type GetBucketResponseHeaderResult BucketResponseHeaderXml

type BucketResponseHeaderXml struct {
XMLName xml.Name `xml:"ResponseHeaderConfiguration"`
Rule []BucketResponseHeaderRule `xml:Rule,omitempty"` // rule
}

type BucketResponseHeaderRule struct {
Name string `xml:"Name"` // rule name
Filters []string `xml:"Filters>Operation,omitempty"` // rule filters Operation
HideHeaders []string `xml:"HideHeaders>Header"` // rule hide header
}
75 changes: 75 additions & 0 deletions oss/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1868,3 +1868,78 @@ func (s *OssTypeSuite) TestAsyncProcessResult(c *C) {
c.Assert(repResult.RequestId, Equals, "B8AD6942-BBDE-571D-A9A9-525A4C34B2B3")
c.Assert(repResult.TaskId, Equals, "MediaConvert-58a8f19f-697f-4f8d-ae2c-0d7b15bef68d")
}

func (s *OssTypeSuite) TestPutBucketResponseHeader(c *C) {
reqHeader := PutBucketResponseHeader{
Rule: []BucketResponseHeaderRule{
{
Name: "name1",
Filters: []string{
"Put", "GetObject",
},
HideHeaders: []string{"Last-Modified"},
},
},
}
xmlData, err := xml.Marshal(reqHeader)
c.Assert(err, IsNil)
c.Assert(string(xmlData), Equals, "<ResponseHeaderConfiguration><Rule><Name>name1</Name><Filters><Operation>Put</Operation><Operation>GetObject</Operation></Filters><HideHeaders><Header>Last-Modified</Header></HideHeaders></Rule></ResponseHeaderConfiguration>")

reqHeader = PutBucketResponseHeader{
Rule: []BucketResponseHeaderRule{
{
Name: "name1",
Filters: []string{
"Put", "GetObject",
},
HideHeaders: []string{"Last-Modified"},
},
{
Name: "name2",
Filters: []string{
"*",
},
HideHeaders: []string{"Last-Modified"},
},
},
}
xmlData, err = xml.Marshal(reqHeader)
c.Assert(err, IsNil)
c.Assert(string(xmlData), Equals, "<ResponseHeaderConfiguration><Rule><Name>name1</Name><Filters><Operation>Put</Operation><Operation>GetObject</Operation></Filters><HideHeaders><Header>Last-Modified</Header></HideHeaders></Rule><Rule><Name>name2</Name><Filters><Operation>*</Operation></Filters><HideHeaders><Header>Last-Modified</Header></HideHeaders></Rule></ResponseHeaderConfiguration>")
}

func (s *OssTypeSuite) TestGetBucketResponseHeaderResult(c *C) {
xmlData := `<ResponseHeaderConfiguration>
<Rule>
<Name>rule1</Name>
<Filters>
<Operation>Put</Operation>
<Operation>GetObject</Operation>
</Filters>
<HideHeaders>
<Header>Last-Modified</Header>
<Header>x-oss-request-id</Header>
</HideHeaders>
</Rule>
<Rule>
<Name>rule2</Name>
<Filters>
<Operation>*</Operation>
</Filters>
<HideHeaders>
<Header>Last-Modified</Header>
<Header>x-oss-request-id</Header>
</HideHeaders>
</Rule>
</ResponseHeaderConfiguration>`
var repResult GetBucketResponseHeaderResult
err := xmlUnmarshal(strings.NewReader(xmlData), &repResult)
c.Assert(err, IsNil)
c.Assert(repResult.Rule[0].Name, Equals, "rule1")
c.Assert(repResult.Rule[0].Filters[0], Equals, "Put")
c.Assert(repResult.Rule[0].HideHeaders[0], Equals, "Last-Modified")

c.Assert(repResult.Rule[1].Name, Equals, "rule2")
c.Assert(repResult.Rule[1].Filters[0], Equals, "*")
c.Assert(repResult.Rule[1].HideHeaders[0], Equals, "Last-Modified")
}
1 change: 1 addition & 0 deletions sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var sampleMap = map[string]interface{}{
"BucketCnameSample": sample.BucketCnameSample,
"BucketStyleSample": sample.BucketStyleSample,
"BucketReplicationSample": sample.BucketReplicationSample,
"BucketResponseHeaderSample": sample.BucketResponseHeaderSample,
"ObjectACLSample": sample.ObjectACLSample,
"ObjectMetaSample": sample.ObjectMetaSample,
"ListObjectsSample": sample.ListObjectsSample,
Expand Down
76 changes: 76 additions & 0 deletions sample/bucket_responseheader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package sample

import (
"fmt"

"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

// BucketResponseHeaderSample shows how to set, get and delete the bucket's response header.
func BucketResponseHeaderSample() {
// 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)
}

// Set bucket's response header.
reqHeader := oss.PutBucketResponseHeader{
Rule: []oss.BucketResponseHeaderRule{
{
Name: "name1",
Filters: []string{
"Put", "GetObject",
},
HideHeaders: []string{"Last-Modified"},
},
{
Name: "name2",
Filters: []string{
"*",
},
HideHeaders: []string{"Last-Modified"},
},
},
}
err = client.PutBucketResponseHeader(bucketName, reqHeader)
if err != nil {
HandleError(err)
}

fmt.Println("Bucket Response Header Set Success!")

// Get bucket's response header.
header, err := client.GetBucketResponseHeader(bucketName)
if err != nil {
HandleError(err)
}
for _, rule := range header.Rule {
fmt.Printf("Rule Name:%#v\n", rule.Name)
if len(rule.Filters) > 0 {
for _, filter := range rule.Filters {
fmt.Printf("Rule Filter:%#v\n", filter)
}
}
if len(rule.HideHeaders) > 0 {
for _, hide := range rule.HideHeaders {
fmt.Printf("Rule Hide Headers:%#v\n", hide)
}
}
}

// Delete bucket's response header.
err = client.DeleteBucketResponseHeader(bucketName)
if err != nil {
HandleError(err)
}
fmt.Println("Bucket Response Header Delete Success!")

fmt.Println("BucketResponseHeaderSample completed")
}

0 comments on commit ca15fd2

Please sign in to comment.