Skip to content

Commit

Permalink
modify bucket cros api (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
yangzong18 authored and huiguangjun committed Oct 30, 2023
1 parent 368023e commit 9b58296
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 6 deletions.
16 changes: 16 additions & 0 deletions oss/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,22 @@ func (client Client) SetBucketCORS(bucketName string, corsRules []CORSRule, opti
return CheckRespCode(resp.StatusCode, []int{http.StatusOK})
}

// SetBucketCORSV2 sets the bucket's CORS rules
//
// bucketName the bucket name
// putBucketCORS the CORS rules to set.
//
// error it's nil if no error, otherwise it's an error object.
//
func (client Client) SetBucketCORSV2(bucketName string, putBucketCORS PutBucketCORS, options ...Option) error {
bs, err := xml.Marshal(putBucketCORS)
if err != nil {
return err
}
err = client.SetBucketCORSXml(bucketName, string(bs), options...)
return err
}

func (client Client) SetBucketCORSXml(bucketName string, xmlBody string, options ...Option) error {
buffer := new(bytes.Buffer)
buffer.Write([]byte(xmlBody))
Expand Down
13 changes: 13 additions & 0 deletions oss/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2325,6 +2325,19 @@ func (s *OssClientSuite) TestSetBucketCORS(c *C) {
c.Assert(err, IsNil)
c.Assert(len(gbcr.CORSRules), Equals, 2)

isTrue := true
put := PutBucketCORS{}
put.CORSRules = []CORSRule{rule1, rule2}
put.ResponseVary = &isTrue
err = client.SetBucketCORSV2(bucketNameTest, put)
c.Assert(err, IsNil)

time.Sleep(timeoutInOperation)
gbcr, err = client.GetBucketCORS(bucketNameTest)
c.Assert(err, IsNil)
c.Assert(len(gbcr.CORSRules), Equals, 2)
c.Assert(*gbcr.ResponseVary, Equals, isTrue)

err = client.DeleteBucketCORS(bucketNameTest)
c.Assert(err, IsNil)

Expand Down
8 changes: 6 additions & 2 deletions oss/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,9 @@ type GetBucketWebsiteResult WebsiteXML

// CORSXML defines CORS configuration
type CORSXML struct {
XMLName xml.Name `xml:"CORSConfiguration"`
CORSRules []CORSRule `xml:"CORSRule"` // CORS rules
XMLName xml.Name `xml:"CORSConfiguration"`
CORSRules []CORSRule `xml:"CORSRule"` // CORS rules
ResponseVary *bool `xml:"ResponseVary,omitempty"` // return Vary or not
}

// CORSRule defines CORS rules
Expand All @@ -341,6 +342,9 @@ type CORSRule struct {
// GetBucketCORSResult defines the result from GetBucketCORS request.
type GetBucketCORSResult CORSXML

// PutBucketCORS defines the PutBucketCORS config xml.
type PutBucketCORS CORSXML

// GetBucketInfoResult defines the result from GetBucketInfo request.
type GetBucketInfoResult struct {
XMLName xml.Name `xml:"BucketInfo"`
Expand Down
65 changes: 65 additions & 0 deletions oss/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1972,3 +1972,68 @@ func (s *OssTypeSuite) TestGetResponseHeaderResult(c *C) {
c.Assert(repResult.Rule[1].Filters.Operation[0], Equals, "*")
c.Assert(repResult.Rule[1].HideHeaders.Header[0], Equals, "Last-Modified")
}

func (s *OssTypeSuite) TestGetBucketCORSResult(c *C) {
xmlData := `<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration>
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>GET</AllowedMethod>
<AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
<CORSRule>
<AllowedOrigin>http://example.com</AllowedOrigin>
<AllowedOrigin>http://example.net</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedHeader>Authorization</AllowedHeader>
<ExposeHeader>x-oss-test</ExposeHeader>
<ExposeHeader>x-oss-test1</ExposeHeader>
<MaxAgeSeconds>100</MaxAgeSeconds>
</CORSRule>
<ResponseVary>false</ResponseVary>
</CORSConfiguration>`
var repResult GetBucketCORSResult
err := xmlUnmarshal(strings.NewReader(xmlData), &repResult)
c.Assert(err, IsNil)
c.Assert(repResult.CORSRules[0].AllowedOrigin[0], Equals, "*")
c.Assert(repResult.CORSRules[0].AllowedMethod[0], Equals, "PUT")
c.Assert(repResult.CORSRules[0].AllowedMethod[1], Equals, "GET")
c.Assert(repResult.CORSRules[0].AllowedHeader[0], Equals, "Authorization")

c.Assert(repResult.CORSRules[1].AllowedOrigin[0], Equals, "http://example.com")
c.Assert(repResult.CORSRules[1].AllowedOrigin[1], Equals, "http://example.net")
c.Assert(repResult.CORSRules[1].AllowedMethod[0], Equals, "GET")
c.Assert(repResult.CORSRules[1].AllowedHeader[0], Equals, "Authorization")
c.Assert(repResult.CORSRules[1].ExposeHeader[0], Equals, "x-oss-test")
c.Assert(repResult.CORSRules[1].ExposeHeader[1], Equals, "x-oss-test1")
c.Assert(repResult.CORSRules[1].MaxAgeSeconds, Equals, int(100))
c.Assert(*repResult.ResponseVary, Equals, false)
}

func (s *OssTypeSuite) TestPutBucketCORS(c *C) {
isTrue := true
rule1 := CORSRule{
AllowedOrigin: []string{"*"},
AllowedMethod: []string{"PUT", "GET", "POST"},
AllowedHeader: []string{},
ExposeHeader: []string{},
MaxAgeSeconds: 100,
}

rule2 := CORSRule{
AllowedOrigin: []string{"http://www.a.com", "http://www.b.com"},
AllowedMethod: []string{"GET"},
AllowedHeader: []string{"Authorization"},
ExposeHeader: []string{"x-oss-test", "x-oss-test1"},
MaxAgeSeconds: 100,
}

put := PutBucketCORS{}
put.CORSRules = []CORSRule{rule1, rule2}
put.ResponseVary = &isTrue

bs, err := xml.Marshal(put)
c.Assert(err, IsNil)
c.Assert(string(bs), Equals, "<CORSConfiguration><CORSRule><AllowedOrigin>*</AllowedOrigin><AllowedMethod>PUT</AllowedMethod><AllowedMethod>GET</AllowedMethod><AllowedMethod>POST</AllowedMethod><MaxAgeSeconds>100</MaxAgeSeconds></CORSRule><CORSRule><AllowedOrigin>http://www.a.com</AllowedOrigin><AllowedOrigin>http://www.b.com</AllowedOrigin><AllowedMethod>GET</AllowedMethod><AllowedHeader>Authorization</AllowedHeader><ExposeHeader>x-oss-test</ExposeHeader><ExposeHeader>x-oss-test1</ExposeHeader><MaxAgeSeconds>100</MaxAgeSeconds></CORSRule><ResponseVary>true</ResponseVary></CORSConfiguration>")
}
26 changes: 22 additions & 4 deletions sample/bucket_cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func BucketCORSSample() {
ExposeHeader: []string{},
MaxAgeSeconds: 100,
}

rule2 := oss.CORSRule{
AllowedOrigin: []string{"http://www.a.com", "http://www.b.com"},
AllowedMethod: []string{"GET"},
Expand All @@ -48,12 +47,31 @@ func BucketCORSSample() {
HandleError(err)
}

// Case 3: Set the bucket CORS rules. if CORS rules exist, they will be overwritten.
isTrue := true
put := oss.PutBucketCORS{}
put.CORSRules = []oss.CORSRule{rule1, rule2}
put.ResponseVary = &isTrue
err = client.SetBucketCORSV2(bucketName, put)
if err != nil {
HandleError(err)
}

// Get the bucket's CORS
gbl, err := client.GetBucketCORS(bucketName)
corsRes, err := client.GetBucketCORS(bucketName)
if err != nil {
HandleError(err)
}
fmt.Println("Bucket CORS:", gbl.CORSRules)
for _, rule := range corsRes.CORSRules {
fmt.Printf("Cors Rules Allowed Origin:%s\n", rule.AllowedOrigin)
fmt.Printf("Cors Rules Allowed Method:%s\n", rule.AllowedMethod)
fmt.Printf("Cors Rules Allowed Header:%s\n", rule.AllowedHeader)
fmt.Printf("Cors Rules Expose Header:%s\n", rule.ExposeHeader)
fmt.Printf("Cors Rules Max Age Seconds:%d\n", rule.MaxAgeSeconds)
}
if corsRes.ResponseVary != nil {
fmt.Printf("Cors Rules Response Vary:%t\n", *corsRes.ResponseVary)
}

// Delete bucket's CORS
err = client.DeleteBucketCORS(bucketName)
Expand All @@ -67,5 +85,5 @@ func BucketCORSSample() {
HandleError(err)
}

fmt.Println("BucketCORSSample completed")
fmt.Println("Bucket CORS Sample completed")
}

0 comments on commit 9b58296

Please sign in to comment.