diff --git a/oss/client.go b/oss/client.go index dc12d23..6d6ecd0 100644 --- a/oss/client.go +++ b/oss/client.go @@ -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. diff --git a/oss/client_test.go b/oss/client_test.go index 1b8b898..08ce12e 100644 --- a/oss/client_test.go +++ b/oss/client_test.go @@ -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) +} diff --git a/oss/conn.go b/oss/conn.go index 3538538..7d40475 100644 --- a/oss/conn.go +++ b/oss/conn.go @@ -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 diff --git a/oss/const.go b/oss/const.go index c675270..b0d5ce6 100644 --- a/oss/const.go +++ b/oss/const.go @@ -81,7 +81,7 @@ const ( StorageDeepColdArchive StorageClassType = "DeepColdArchive" ) -// RedundancyType bucket data Redundancy type +//RedundancyType bucket data Redundancy type type DataRedundancyType string const ( @@ -92,7 +92,7 @@ const ( RedundancyZRS DataRedundancyType = "ZRS" ) -// ObjecthashFuncType +//ObjecthashFuncType type ObjecthashFuncType string const ( @@ -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 ( @@ -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 diff --git a/oss/type.go b/oss/type.go index 5e53e6a..009005e 100644 --- a/oss/type.go +++ b/oss/type.go @@ -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 +} diff --git a/oss/type_test.go b/oss/type_test.go index 55f413f..7244677 100644 --- a/oss/type_test.go +++ b/oss/type_test.go @@ -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, "name1PutGetObject
Last-Modified
") + + 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, "name1PutGetObject
Last-Modified
name2*
Last-Modified
") +} + +func (s *OssTypeSuite) TestGetBucketResponseHeaderResult(c *C) { + xmlData := ` + + rule1 + + Put + GetObject + + +
Last-Modified
+
x-oss-request-id
+
+
+ + rule2 + + * + + +
Last-Modified
+
x-oss-request-id
+
+
+
` + 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") +} diff --git a/sample.go b/sample.go index dd110fb..519a987 100644 --- a/sample.go +++ b/sample.go @@ -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, diff --git a/sample/bucket_responseheader.go b/sample/bucket_responseheader.go new file mode 100644 index 0000000..f178794 --- /dev/null +++ b/sample/bucket_responseheader.go @@ -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") +}