Bucket cross-origin resource access
Updated at:2025-11-03
Application scenarios
Cross-Origin Resource Sharing (CORS) allows web-based applications to access resources outside their current domain. BOS provides APIs to help developers manage cross-origin access permissions effectively.
Set CORS rules
The following code sets a CORS rule:
Plain Text
1conf = {}
2conf['allowedOrigins'] = ['http://www.boluor.com']
3conf['allowedMethods'] = ['GET', 'HEAD', 'DELETE']
4conf['allowedHeaders'] = ['Authorization', 'x-bce-test', 'x-bce-test2']
5conf['allowedExposeHeaders'] = ['user-custom-expose-header']
6conf['maxAgeSeconds'] = 3600
7confs = []
8 #Each bucket allows up to 100 rules.
9confs.append(conf)
10bos_client.put_bucket_cors(bucket_name, confs)
Note:
- If an existing rule is found, it will overwrite the original rule.
- Only the bucket owner or users granted FULL_CONTROL permissions can set bucket CORS. If the necessary permissions are not granted, the server will return a 403 Forbidden error with the AccessDenied code.
For a detailed explanation of the parameters related to CORS rules, please refer to [PutBucketCors API](BOS/API Reference/Bucket-Related Interface/Cross-Origin Access/PutBucketCors.md).
Get the CORS rules of bucket
The following code can get the CORS configuration of the bucket:
Python
1response = bos_client.get_bucket_cors(bucket_name)
Disable the bucket CORS function and clear all rules
The following code can disable the CORS function of the bucket and clear all rules:
Python
1bos_client.delete_bucket_cors(bucket_name)
