Bucket跨域资源访问
更新时间:2026-09-20
应用场景
跨域资源共享(CORS)允许 Web 端的应用程序访问不属于本域的资源。BOS 提供接口方便开发者控制跨域访问的各种权限。
设定 CORS 规则
如下代码设定了一个 CORS 规则:
Python
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bos.bos_client import BosClient
4
5access_key_id = 'your-access-key-id'
6secret_access_key = 'your-secret-access-key'
7endpoint = 'https://bj.bcebos.com'
8bucket_name = 'your-bucket-name'
9
10bos_client = BosClient(BceClientConfiguration(
11 credentials=BceCredentials(access_key_id, secret_access_key),
12 endpoint=endpoint
13))
14
15conf = {}
16conf['allowedOrigins'] = ['http://www.boluor.com']
17conf['allowedMethods'] = ['GET', 'HEAD', 'DELETE']
18conf['allowedHeaders'] = ['Authorization', 'x-bce-test', 'x-bce-test2']
19conf['allowedExposeHeaders'] = ['user-custom-expose-header']
20conf['maxAgeSeconds'] = 3600
21
22confs = []
23# 每个 Bucket 最多允许有 100 条规则。
24confs.append(conf)
25
26response = bos_client.put_bucket_cors(bucket_name, confs)
27print(response)
参数说明
| 参数 | 必填 | 说明 |
|---|---|---|
bucket_name |
是 | Bucket 名称。 |
conf['allowedOrigins'] |
是 | 允许跨域请求的来源域名列表。 |
conf['allowedMethods'] |
是 | 允许的跨域请求方法列表,例如 GET、HEAD、DELETE。 |
conf['allowedHeaders'] |
否 | 允许浏览器在跨域请求中携带的请求头列表。 |
conf['allowedExposeHeaders'] |
否 | 允许浏览器从响应中读取的响应头列表。 |
conf['maxAgeSeconds'] |
否 | 浏览器缓存预检请求结果的时间,单位为秒。 |
confs |
是 | CORS 规则列表,每个 Bucket 最多允许有 100 条规则。 |
调用成功后,BOS 对设置 CORS 规则的请求返回空响应体。可通过 get_bucket_cors 获取 Bucket 的 CORS 配置,确认规则是否已生效。
注意:
- 如果原规则存在则覆盖原规则。
- 只有 Bucket 的所有者和被授予 FULL_CONTROL 权限的用户才能设置 Bucket 的 CORS。没有权限时,返回 403 Forbidden 错误,错误码:AccessDenied。
CORS 规则相关参数的详细解释请参见 PutBucketCors 接口。
获取 Bucket 的 CORS 规则
如下代码可获取 Bucket 的 CORS 配置:
Python
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bos.bos_client import BosClient
4
5access_key_id = 'your-access-key-id'
6secret_access_key = 'your-secret-access-key'
7endpoint = 'https://bj.bcebos.com'
8bucket_name = 'your-bucket-name'
9
10bos_client = BosClient(BceClientConfiguration(
11 credentials=BceCredentials(access_key_id, secret_access_key),
12 endpoint=endpoint
13))
14
15response = bos_client.get_bucket_cors(bucket_name)
16print(response.status_code)
17print(response.cors_configuration)
参数说明
| 参数 | 必填 | 说明 |
|---|---|---|
bucket_name |
是 | Bucket 名称。 |
返回值说明:
| 返回值 | 说明 |
|---|---|
response.status_code |
请求状态码。请求成功时通常为 200。 |
response.cors_configuration |
Bucket 的 CORS 规则列表,包含 allowedOrigins、allowedMethods、allowedHeaders、allowedExposeHeaders、maxAgeSeconds 等配置项。 |
返回示例:
JSON
1{
2 "corsConfiguration": [
3 {
4 "allowedOrigins": [
5 "http://www.boluor.com"
6 ],
7 "allowedMethods": [
8 "GET",
9 "HEAD",
10 "DELETE"
11 ],
12 "allowedHeaders": [
13 "Authorization",
14 "x-bce-test",
15 "x-bce-test2"
16 ],
17 "allowedExposeHeaders": [
18 "user-custom-expose-header"
19 ],
20 "maxAgeSeconds": 3600
21 }
22 ]
23}
关闭 Bucket 的 CORS 功能并清空所有规则
如下代码可以关闭 Bucket 的 CORS 功能并清空所有规则:
Python
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bos.bos_client import BosClient
4
5access_key_id = 'your-access-key-id'
6secret_access_key = 'your-secret-access-key'
7endpoint = 'https://bj.bcebos.com'
8bucket_name = 'your-bucket-name'
9
10bos_client = BosClient(BceClientConfiguration(
11 credentials=BceCredentials(access_key_id, secret_access_key),
12 endpoint=endpoint
13))
14
15response = bos_client.delete_bucket_cors(bucket_name)
16print(response)
参数说明
| 参数 | 必填 | 说明 |
|---|---|---|
bucket_name |
是 | Bucket 名称。 |
调用成功后,Bucket 的 CORS 功能被关闭,已有 CORS 规则会被清空。删除后再次调用 get_bucket_cors 获取 CORS 配置时,如果服务端返回 NoSuchCORSConfiguration,表示当前 Bucket 不存在 CORS 配置。
评价此篇文章
