Skip to content

Commit

Permalink
Merge pull request #25 from ks3sdklib/fix_getcors_xml
Browse files Browse the repository at this point in the history
fix : 修复获取跨域规则
  • Loading branch information
wilac-pv authored Jul 20, 2023
2 parents 1109bd4 + 9524038 commit a7ba131
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 21 deletions.
2 changes: 1 addition & 1 deletion aws/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ package aws
const SDKName = "aws-sdk-go"

// SDKVersion is the version of this SDK
const SDKVersion = "1.2.3"
const SDKVersion = "1.2.5"
3 changes: 3 additions & 0 deletions internal/protocol/rest/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func PayloadType(i interface{}) string {
}
if field, ok := v.Type().FieldByName("SDKShapeTraits"); ok {
if payloadName := field.Tag.Get("payload"); payloadName != "" {
if payloadName == "GetBucketCORSInput" {
return field.Tag.Get("type")
}
if member, ok := v.Type().FieldByName(payloadName); ok {
return member.Tag.Get("type")
}
Expand Down
28 changes: 26 additions & 2 deletions internal/protocol/restxml/restxml.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ks3sdklib/aws-sdk-go/internal/protocol/query"
"github.com/ks3sdklib/aws-sdk-go/internal/protocol/rest"
"github.com/ks3sdklib/aws-sdk-go/internal/protocol/xml/xmlutil"
"io/ioutil"
)

// Build builds a request payload for the REST XML protocol.
Expand All @@ -39,15 +40,38 @@ func Build(r *aws.Request) {
func Unmarshal(r *aws.Request) {
if t := rest.PayloadType(r.Data); t == "structure" || t == "" {
defer r.HTTPResponse.Body.Close()
decoder := xml.NewDecoder(r.HTTPResponse.Body)
err := xmlutil.UnmarshalXML(r.Data, decoder, "")
data, err := ioutil.ReadAll(r.HTTPResponse.Body)
if err != nil {
r.Error = apierr.New("ReadBody", "failed to read response body", err)
return
}
decoder := xml.NewDecoder(bytes.NewReader(data))
err = xmlutil.UnmarshalXML(r.Data, decoder, "")
if err != nil {
r.Error = apierr.New("Unmarshal", "failed to decode REST XML response", err)
return
}
return
}
}

type GetBucketCORSOutput struct {
CORSConfiguration *CORSConfiguration `xml:"CORSConfiguration"`
Metadata map[string]*string `xml:"-"`
}

type CORSConfiguration struct {
Rules []*CORSRule `xml:"CORSRule"`
}

type CORSRule struct {
AllowedHeader []string `xml:"AllowedHeader"`
AllowedMethod []string `xml:"AllowedMethod"`
AllowedOrigin []string `xml:"AllowedOrigin"`
ExposeHeader []string `xml:"ExposeHeader"`
MaxAgeSeconds int `xml:"MaxAgeSeconds"`
}

// UnmarshalMeta unmarshals response headers for the REST XML protocol.
func UnmarshalMeta(r *aws.Request) {
rest.Unmarshal(r)
Expand Down
42 changes: 25 additions & 17 deletions service/s3/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func (c *S3) GetBucketCORSRequest(input *GetBucketCORSInput) (req *aws.Request,
func (c *S3) GetBucketCORS(input *GetBucketCORSInput) (*GetBucketCORSOutput, error) {
req, out := c.GetBucketCORSRequest(input)
err := req.Send()
if req.Data != nil {
out = req.Data.(*GetBucketCORSOutput)
}
return out, err
}

Expand Down Expand Up @@ -114,16 +117,6 @@ type metadataPutBucketCORSInput struct {
AutoFillMD5 bool
}

type PutBucketCORSOutput struct {
metadataPutBucketCORSOutput `json:"-" xml:"-"`

Metadata map[string]*string `location:"headers" type:"map"`
}

type metadataPutBucketCORSOutput struct {
SDKShapeTraits bool `type:"structure"`
}

type GetBucketCORSInput struct {
Bucket *string `location:"uri" locationName:"Bucket" type:"string" required:"true"`

Expand All @@ -137,19 +130,34 @@ type metadataInput struct {
}

type GetBucketCORSOutput struct {
CORSRules []*CORSRule `locationName:"CORSRule" type:"list" flattened:"true"`
metadataInput `json:"-" xml:"-"`
Metadata map[string]*string `location:"headers" type:"map"`
Metadata map[string]*string `location:"headers" type:"map"`
Rules []*GetCORSRule `locationName:"CORSRule" type:"list" flattened:"true" xml:"CORSRule"`
}
type GetCORSRule struct {
AllowedHeader []*string `locationName:"AllowedHeader" type:"list" flattened:"true" `
AllowedMethod []*string `locationName:"AllowedMethod" type:"list" flattened:"true"`
AllowedOrigin []*string `locationName:"AllowedOrigin" type:"list" flattened:"true"`
ExposeHeader []*string `locationName:"ExposeHeader" type:"list" flattened:"true"`
MaxAgeSeconds *int64 `locationName:"MaxAgeSeconds" flattened:"true"` // Max cache ages in seconds
}

type CORSConfiguration struct {
Rules []*CORSRule `locationName:"CORSRule" type:"list" flattened:"true"` // CORS rules
Rules []*CORSRule `locationName:"CORSRule" type:"list" flattened:"true" xml:"CORSRule"`
}

type PutBucketCORSOutput struct {
metadataPutBucketCORSOutput `json:"-" xml:"-"`

Metadata map[string]*string `location:"headers" type:"map"`
}

type metadataPutBucketCORSOutput struct {
SDKShapeTraits bool `type:"structure"`
}

type CORSRule struct {
AllowedHeader []string `locationName:"AllowedHeader" type:"list" flattened:"true" `
AllowedHeader []string `locationName:"AllowedHeader" type:"list" flattened:"true"`
AllowedMethod []string `locationName:"AllowedMethod" type:"list" flattened:"true"`
AllowedOrigin []string `locationName:"AllowedOrigin" type:"list" flattened:"true"`
ExposeHeader []string `locationName:"ExposeHeader" type:"list" flattened:"true"`
MaxAgeSeconds int `locationName:"MaxAgeSeconds" flattened:"true"` // Max cache ages in seconds
MaxAgeSeconds int64 `locationName:"MaxAgeSeconds" flattened:"true"` // Max cache ages in seconds
}
4 changes: 3 additions & 1 deletion test/bucketsample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (s *Ks3utilCommandSuite) TestDeleteBucketLifeRules(c *C) {
}

//设置bucket cors
func (s *Ks3utilCommandSuite) TestSetBucketCors() {
func (s *Ks3utilCommandSuite) TestSetBucketCors(c *C) {

// 配置CORS规则
corsConfiguration := &s3.CORSConfiguration{
Expand Down Expand Up @@ -138,6 +138,8 @@ func (s *Ks3utilCommandSuite) TestGetBucketCors(c *C) {
})
fmt.Println("结果:\n", awsutil.StringValue(resp), err)
}

//删除bucket cors
func (s *Ks3utilCommandSuite) TestDeleteBucketCors(c *C) {
resp, err := client.DeleteBucketCORS(&s3.DeleteBucketCORSInput{
Bucket: aws.String(bucket),
Expand Down

0 comments on commit a7ba131

Please sign in to comment.