Skip to content

Commit d838eb3

Browse files
yangzong18huiguangjun
authored andcommitted
add bucket cors api
1 parent f184d83 commit d838eb3

8 files changed

+2026
-0
lines changed

oss/api_op_bucket_cors.go

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
package oss
2+
3+
import (
4+
"context"
5+
6+
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/signer"
7+
)
8+
9+
type CORSConfiguration struct {
10+
// The container that stores CORS rules. Up to 10 rules can be configured for a bucket.
11+
CORSRules []CORSRule `xml:"CORSRule"`
12+
13+
// Indicates whether the Vary: Origin header was returned. Default value: false.- true: The Vary: Origin header is returned regardless whether the request is a cross-origin request or whether the cross-origin request succeeds.- false: The Vary: Origin header is not returned.
14+
ResponseVary *bool `xml:"ResponseVary"`
15+
}
16+
17+
type CORSRule struct {
18+
// The origins from which cross-origin requests are allowed.
19+
AllowedOrigins []string `xml:"AllowedOrigin"`
20+
21+
// The methods that you can use in cross-origin requests.
22+
AllowedMethods []string `xml:"AllowedMethod"`
23+
24+
// Specifies whether the headers specified by Access-Control-Request-Headers in the OPTIONS preflight request are allowed. Each header specified by Access-Control-Request-Headers must match the value of an AllowedHeader element. You can use only one asterisk (\*) as the wildcard character.
25+
AllowedHeaders []string `xml:"AllowedHeader"`
26+
27+
// The response headers for allowed access requests from applications, such as an XMLHttpRequest object in JavaScript. The asterisk (\*) wildcard character is not supported.
28+
ExposeHeaders []string `xml:"ExposeHeader"`
29+
30+
// The period of time within which the browser can cache the response to an OPTIONS preflight request for the specified resource. Unit: seconds.You can specify only one MaxAgeSeconds element in a CORS rule.
31+
MaxAgeSeconds *int64 `xml:"MaxAgeSeconds"`
32+
}
33+
34+
type PutBucketCorsRequest struct {
35+
// The name of the bucket.
36+
Bucket *string `input:"host,bucket,required"`
37+
38+
// The request body schema.
39+
CORSConfiguration *CORSConfiguration `input:"body,CORSConfiguration,xml,required"`
40+
41+
RequestCommon
42+
}
43+
44+
type PutBucketCorsResult struct {
45+
ResultCommon
46+
}
47+
48+
// PutBucketCors Configures cross-origin resource sharing (CORS) rules for a bucket.
49+
func (c *Client) PutBucketCors(ctx context.Context, request *PutBucketCorsRequest, optFns ...func(*Options)) (*PutBucketCorsResult, error) {
50+
var err error
51+
if request == nil {
52+
request = &PutBucketCorsRequest{}
53+
}
54+
input := &OperationInput{
55+
OpName: "PutBucketCors",
56+
Method: "PUT",
57+
Headers: map[string]string{
58+
HTTPHeaderContentType: contentTypeXML,
59+
},
60+
Parameters: map[string]string{
61+
"cors": "",
62+
},
63+
Bucket: request.Bucket,
64+
}
65+
input.OpMetadata.Set(signer.SubResource, []string{"cors"})
66+
67+
if err = c.marshalInput(request, input, updateContentMd5); err != nil {
68+
return nil, err
69+
}
70+
output, err := c.invokeOperation(ctx, input, optFns)
71+
if err != nil {
72+
return nil, err
73+
}
74+
75+
result := &PutBucketCorsResult{}
76+
77+
if err = c.unmarshalOutput(result, output, unmarshalBodyXmlMix); err != nil {
78+
return nil, c.toClientError(err, "UnmarshalOutputFail", output)
79+
}
80+
81+
return result, err
82+
}
83+
84+
type GetBucketCorsRequest struct {
85+
// The name of the bucket.
86+
Bucket *string `input:"host,bucket,required"`
87+
88+
RequestCommon
89+
}
90+
91+
type GetBucketCorsResult struct {
92+
// The container that stores CORS configuration.
93+
CORSConfiguration *CORSConfiguration `output:"body,CORSConfiguration,xml"`
94+
95+
ResultCommon
96+
}
97+
98+
// GetBucketCors Queries the cross-origin resource sharing (CORS) rules that are configured for a bucket.
99+
func (c *Client) GetBucketCors(ctx context.Context, request *GetBucketCorsRequest, optFns ...func(*Options)) (*GetBucketCorsResult, error) {
100+
var err error
101+
if request == nil {
102+
request = &GetBucketCorsRequest{}
103+
}
104+
input := &OperationInput{
105+
OpName: "GetBucketCors",
106+
Method: "GET",
107+
Headers: map[string]string{
108+
HTTPHeaderContentType: contentTypeXML,
109+
},
110+
Parameters: map[string]string{
111+
"cors": "",
112+
},
113+
Bucket: request.Bucket,
114+
}
115+
input.OpMetadata.Set(signer.SubResource, []string{"cors"})
116+
117+
if err = c.marshalInput(request, input, updateContentMd5); err != nil {
118+
return nil, err
119+
}
120+
output, err := c.invokeOperation(ctx, input, optFns)
121+
if err != nil {
122+
return nil, err
123+
}
124+
125+
result := &GetBucketCorsResult{}
126+
127+
if err = c.unmarshalOutput(result, output, unmarshalBodyXmlMix); err != nil {
128+
return nil, c.toClientError(err, "UnmarshalOutputFail", output)
129+
}
130+
131+
return result, err
132+
}
133+
134+
type DeleteBucketCorsRequest struct {
135+
// The name of the bucket.
136+
Bucket *string `input:"host,bucket,required"`
137+
138+
RequestCommon
139+
}
140+
141+
type DeleteBucketCorsResult struct {
142+
ResultCommon
143+
}
144+
145+
// DeleteBucketCors Disables the cross-origin resource sharing (CORS) feature and deletes all CORS rules for a bucket.
146+
func (c *Client) DeleteBucketCors(ctx context.Context, request *DeleteBucketCorsRequest, optFns ...func(*Options)) (*DeleteBucketCorsResult, error) {
147+
var err error
148+
if request == nil {
149+
request = &DeleteBucketCorsRequest{}
150+
}
151+
input := &OperationInput{
152+
OpName: "DeleteBucketCors",
153+
Method: "DELETE",
154+
Headers: map[string]string{
155+
HTTPHeaderContentType: contentTypeXML,
156+
},
157+
Parameters: map[string]string{
158+
"cors": "",
159+
},
160+
Bucket: request.Bucket,
161+
}
162+
input.OpMetadata.Set(signer.SubResource, []string{"cors"})
163+
164+
if err = c.marshalInput(request, input, updateContentMd5); err != nil {
165+
return nil, err
166+
}
167+
output, err := c.invokeOperation(ctx, input, optFns)
168+
if err != nil {
169+
return nil, err
170+
}
171+
172+
result := &DeleteBucketCorsResult{}
173+
174+
if err = c.unmarshalOutput(result, output, unmarshalBodyXmlMix); err != nil {
175+
return nil, c.toClientError(err, "UnmarshalOutputFail", output)
176+
}
177+
178+
return result, err
179+
}
180+
181+
type OptionObjectRequest struct {
182+
// The name of the bucket.
183+
Bucket *string `input:"host,bucket,required"`
184+
185+
// The full path of the object.
186+
Key *string `input:"path,key,required"`
187+
188+
// The origin of the request. It is used to identify a cross-origin request. You can specify only one Origin header in a cross-origin request. By default, this header is left empty.
189+
Origin *string `input:"header,Origin,required"`
190+
191+
// The method to be used in the actual cross-origin request. You can specify only one Access-Control-Request-Method header in a cross-origin request. By default, this header is left empty.
192+
AccessControlRequestMethod *string `input:"header,Access-Control-Request-Method,required"`
193+
194+
// The custom headers to be sent in the actual cross-origin request. You can configure multiple custom headers in a cross-origin request. Custom headers are separated by commas (,). By default, this header is left empty.
195+
AccessControlRequestHeaders *string `input:"header,Access-Control-Request-Headers"`
196+
197+
RequestCommon
198+
}
199+
200+
type OptionObjectResult struct {
201+
// The HTTP method of the request. If the request is denied, the response does not contain the header.
202+
AccessControlAllowMethods *string `output:"header,Access-Control-Allow-Methods"`
203+
204+
// The list of headers included in the request. If the request includes headers that are not allowed, the response does not contain the headers and the request is denied.
205+
AccessControlAllowHeaders *string `output:"header,Access-Control-Allow-Headers"`
206+
207+
// The list of headers that can be accessed by JavaScript applications on a client.
208+
AccessControlExposeHeaders *string `output:"header,Access-Control-Expose-Headers"`
209+
210+
// The maximum duration for the browser to cache preflight results. Unit: seconds.
211+
AccessControlMaxAge *int64 `output:"header,Access-Control-Max-Age"`
212+
213+
// The origin that is included in the request. If the request is denied, the response does not contain the header.
214+
AccessControlAllowOrigin *string `output:"header,Access-Control-Allow-Origin"`
215+
216+
ResultCommon
217+
}
218+
219+
// OptionObject Determines whether to send a cross-origin request. Before a cross-origin request is sent, the browser sends a preflight OPTIONS request that includes a specific origin, HTTP method, and header information to Object Storage Service (OSS) to determine whether to send the cross-origin request.
220+
func (c *Client) OptionObject(ctx context.Context, request *OptionObjectRequest, optFns ...func(*Options)) (*OptionObjectResult, error) {
221+
var err error
222+
if request == nil {
223+
request = &OptionObjectRequest{}
224+
}
225+
input := &OperationInput{
226+
OpName: "OptionObject",
227+
Method: "OPTIONS",
228+
Headers: map[string]string{
229+
HTTPHeaderContentType: contentTypeXML,
230+
},
231+
Bucket: request.Bucket,
232+
Key: request.Key,
233+
}
234+
235+
if err = c.marshalInput(request, input, updateContentMd5); err != nil {
236+
return nil, err
237+
}
238+
output, err := c.invokeOperation(ctx, input, optFns)
239+
if err != nil {
240+
return nil, err
241+
}
242+
243+
result := &OptionObjectResult{}
244+
245+
if err = c.unmarshalOutput(result, output, unmarshalHeader, unmarshalBodyXmlMix); err != nil {
246+
return nil, c.toClientError(err, "UnmarshalOutputFail", output)
247+
}
248+
249+
return result, err
250+
}

0 commit comments

Comments
 (0)