S3 / Client / put_bucket_cors
put_bucket_cors#
- S3.Client.put_bucket_cors(**kwargs)#
Sets the
corsconfiguration for your bucket. If the configuration exists, Amazon S3 replaces it.To use this operation, you must be allowed to perform the
s3:PutBucketCORSaction. By default, the bucket owner has this permission and can grant it to others.You set this configuration on a bucket so that the bucket can service cross-origin requests. For example, you might want to enable a request whose origin is
http://www.example.comto access your Amazon S3 bucket atmy.example.bucket.comby using the browser’sXMLHttpRequestcapability.To enable cross-origin resource sharing (CORS) on a bucket, you add the
corssubresource to the bucket. Thecorssubresource is an XML document in which you configure rules that identify origins and the HTTP methods that can be executed on your bucket. The document is limited to 64 KB in size.When Amazon S3 receives a cross-origin request (or a pre-flight OPTIONS request) against a bucket, it evaluates the
corsconfiguration on the bucket and uses the firstCORSRulerule that matches the incoming browser request to enable a cross-origin request. For a rule to match, the following conditions must be met:The request’s
Originheader must matchAllowedOriginelements.The request method (for example, GET, PUT, HEAD, and so on) or the
Access-Control-Request-Methodheader in case of a pre-flightOPTIONSrequest must be one of theAllowedMethodelements.Every header specified in the
Access-Control-Request-Headersrequest header of a pre-flight request must match anAllowedHeaderelement.
For more information about CORS, go to Enabling Cross-Origin Resource Sharing in the Amazon S3 User Guide.
Related Resources
See also: AWS API Documentation
Request Syntax
response = client.put_bucket_cors( Bucket='string', CORSConfiguration={ 'CORSRules': [ { 'ID': 'string', 'AllowedHeaders': [ 'string', ], 'AllowedMethods': [ 'string', ], 'AllowedOrigins': [ 'string', ], 'ExposeHeaders': [ 'string', ], 'MaxAgeSeconds': 123 }, ] }, ExpectedBucketOwner='string' )
- Parameters:
Bucket (string) –
[REQUIRED]
Specifies the bucket impacted by the ``cors``configuration.
CORSConfiguration (dict) –
[REQUIRED]
Describes the cross-origin access configuration for objects in an Amazon S3 bucket. For more information, see Enabling Cross-Origin Resource Sharing in the Amazon S3 User Guide.
CORSRules (list) – [REQUIRED]
A set of origins and methods (cross-origin access that you want to allow). You can add up to 100 rules to the configuration.
(dict) –
Specifies a cross-origin access rule for an Amazon S3 bucket.
ID (string) –
Unique identifier for the rule. The value cannot be longer than 255 characters.
AllowedHeaders (list) –
Headers that are specified in the
Access-Control-Request-Headersheader. These headers are allowed in a preflight OPTIONS request. In response to any preflight OPTIONS request, Amazon S3 returns any requested headers that are allowed.(string) –
AllowedMethods (list) – [REQUIRED]
An HTTP method that you allow the origin to execute. Valid values are
GET,PUT,HEAD,POST, andDELETE.(string) –
AllowedOrigins (list) – [REQUIRED]
One or more origins you want customers to be able to access the bucket from.
(string) –
ExposeHeaders (list) –
One or more headers in the response that you want customers to be able to access from their applications (for example, from a JavaScript
XMLHttpRequestobject).(string) –
MaxAgeSeconds (integer) –
The time in seconds that your browser is to cache the preflight response for the specified resource.
ExpectedBucketOwner (string) – Ignored by COS if present.
- Returns:
None
Examples
The following example enables PUT, POST, and DELETE requests from www.example.com, and enables GET requests from any domain.
response = client.put_bucket_cors( Bucket='', CORSConfiguration={ 'CORSRules': [ { 'AllowedHeaders': [ '*', ], 'AllowedMethods': [ 'PUT', 'POST', 'DELETE', ], 'AllowedOrigins': [ 'http://www.example.com', ], 'ExposeHeaders': [ 'x-amz-server-side-encryption', ], 'MaxAgeSeconds': 3000, }, { 'AllowedHeaders': [ 'Authorization', ], 'AllowedMethods': [ 'GET', ], 'AllowedOrigins': [ '*', ], 'MaxAgeSeconds': 3000, }, ], }, ContentMD5='', ) print(response)
Expected Output:
{ 'ResponseMetadata': { '...': '...', }, }