Skip to content

Commit

Permalink
add repsonse header api (#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzong18 authored and huiguangjun committed Oct 30, 2023
1 parent 50c3b3f commit 368023e
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 5 deletions.
79 changes: 79 additions & 0 deletions oss/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2537,6 +2537,85 @@ 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
}
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
48 changes: 48 additions & 0 deletions oss/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5805,3 +5805,51 @@ 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: []ResponseHeaderRule{
{
Name: "name1",
Filters: ResponseHeaderRuleFilters{
[]string{
"Put", "GetObject",
},
},
HideHeaders: ResponseHeaderRuleHeaders{
[]string{
"Last-Modified",
},
},
},
{
Name: "name2",
Filters: ResponseHeaderRuleFilters{
[]string{
"*",
},
},
HideHeaders: ResponseHeaderRuleHeaders{
[]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.Operation[0], Equals, "Put")
c.Assert(rule.Rule[0].Filters.Operation[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
25 changes: 25 additions & 0 deletions oss/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -1663,3 +1663,28 @@ type RegionInfoList struct {
XMLName xml.Name `xml:"RegionInfoList"`
Regions []RegionInfo `xml:"RegionInfo"`
}

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

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

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

type ResponseHeaderRule struct {
Name string `xml:"Name"` // rule name
Filters ResponseHeaderRuleFilters `xml:"Filters,omitempty"` // rule filters Operation
HideHeaders ResponseHeaderRuleHeaders `xml:"HideHeaders,omitempty"` // rule hide header
}

type ResponseHeaderRuleFilters struct {
Operation []string `xml:"Operation,omitempty"`
}

type ResponseHeaderRuleHeaders struct {
Header []string `xml:"Header,omitempty"`
}
104 changes: 104 additions & 0 deletions oss/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1868,3 +1868,107 @@ 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) TestPutResponseHeader(c *C) {
reqHeader := PutBucketResponseHeader{
Rule: []ResponseHeaderRule{
{
Name: "name1",
Filters: ResponseHeaderRuleFilters{
[]string{
"Put", "GetObject",
},
},
HideHeaders: ResponseHeaderRuleHeaders{
[]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: []ResponseHeaderRule{
{
Name: "name1",
Filters: ResponseHeaderRuleFilters{
[]string{
"Put", "GetObject",
},
},
HideHeaders: ResponseHeaderRuleHeaders{
[]string{
"Last-Modified",
},
},
},
{
Name: "name2",
Filters: ResponseHeaderRuleFilters{
[]string{
"*",
},
},
HideHeaders: ResponseHeaderRuleHeaders{
[]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>")

reqHeader = PutBucketResponseHeader{
Rule: []ResponseHeaderRule{
{
Name: "name1",
},
},
}
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) TestGetResponseHeaderResult(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.Operation[0], Equals, "Put")
c.Assert(repResult.Rule[0].HideHeaders.Header[0], Equals, "Last-Modified")

c.Assert(repResult.Rule[1].Name, Equals, "rule2")
c.Assert(repResult.Rule[1].Filters.Operation[0], Equals, "*")
c.Assert(repResult.Rule[1].HideHeaders.Header[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
Loading

0 comments on commit 368023e

Please sign in to comment.