-
Notifications
You must be signed in to change notification settings - Fork 0
/
put_bucket_cors.go
66 lines (60 loc) · 1.47 KB
/
put_bucket_cors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* Author: CZ cz.theng@gmail.com
*/
package cors
import (
"encoding/xml"
"github.com/cz-it/aliyun-oss-golang-sdk/ossapi"
"path"
)
//RuleInfo is CORS rules
type RuleInfo struct {
AllowedOrigin []string
AllowedMethod []string
AllowedHeader []string
ExposeHeader []string
MaxAgeSeconds uint64
}
// Info is XML wraper
type Info struct {
XMLName xml.Name `xml:"CORSConfiguration"`
CORSRule []RuleInfo
}
// Create Create a CORS rule
// @param bucketName : name of bucket
// @param location : bucket's loction
// @param corsInfo : cors rules
// @return ossapiError : nil on success
func Create(bucketName, location string, corsInfo []RuleInfo) (ossapiError *ossapi.Error) {
host := bucketName + "." + location + ".aliyuncs.com"
info := &Info{CORSRule: corsInfo}
body, err := xml.Marshal(info)
if err != nil {
ossapi.Logger.Error("xml.Marshal(cfg) Error:%s", err.Error())
ossapiError = ossapi.OSSAPIError
return
}
body = append([]byte(xml.Header), body...)
resource := path.Join("/", bucketName)
req := &ossapi.Request{
Host: host,
Path: "/?cors",
Method: "PUT",
SubRes: []string{"cors"},
Resource: resource + "/",
Body: body,
CntType: "application/xml"}
rsp, err := req.Send()
if err != nil {
if _, ok := err.(*ossapi.Error); !ok {
ossapi.Logger.Error("GetService's Send Error:%s", err.Error())
ossapiError = ossapi.OSSAPIError
return
}
}
if rsp.Result != ossapi.ErrSUCC {
ossapiError = err.(*ossapi.Error)
return
}
return
}