设置桶的CORS配置
功能说明
设置桶的跨域资源共享规则,以允许客户端浏览器进行跨域请求。
方法定义
1. ObsClient->setBucketCors(array $parameter) 2. ObsClient->setBucketCorsAsync(array $parameter, callable $callback)
请求参数
字段名 | 类型 | 约束 | 说明 | |
|---|---|---|---|---|
Bucket | string | 必选 | 桶名。 | |
CorsRules | indexed array | 必选 | 桶的CORS规则列表。 | |
- | ID | string | 可选 | CORS规则ID,由不超过255个字符的字符串组成。 |
AllowedMethod | indexed array of strings | 必选 | CORS规则允许的HTTP方法,支持的值:
| |
AllowedOrigin | indexed array of strings | 必选 | CORS规则允许的请求来源(表示域名的字符串)。可以带一个匹配符“*”,每一个AllowedOrigin最多可以带一个“*”通配符。 | |
AllowedHeader | indexed array of strings | 可选 | CORS规则允许请求中可携带的头域,不可出现空格。可以带一个匹配符“*”,且每一个AllowedHeader最多可以带一个“*”通配符。 | |
MaxAgeSeconds | integer | 可选 | CORS规则允许客户端可以对跨域请求返回结果的缓存时间,以秒为单位,整数类型。 | |
ExposeHeader | indexed array of strings | 可选 | CORS规则允许响应中可返回的附加头域,不可出现空格。 | |
返回结果
字段名 | 类型 | 说明 |
|---|---|---|
HttpStatusCode | integer | HTTP状态码。 |
Reason | string | HTTP文本描述。 |
RequestId | string | OBS服务端返回的请求ID。 |
代码样例
try{
$resp = $obsClient -> setBucketCors([
'Bucket' => 'bucketname',
'CorsRules' => [
[
'ID' => 'rule1',
'AllowedMethod' => ['PUT','POST','GET','DELETE','HEAD'],
'AllowedOrigin' => ['obs.hostname','obs.hostname1'],
'AllowedHeader' => ['obs-header-1'],
'MaxAgeSeconds' => 60
],
[
'ID' => 'rule2',
'AllowedMethod' => ['PUT','POST','GET'],
'AllowedOrigin' => ['obs.hostname','obs.hostname1'],
'AllowedHeader' => ['header-1','header-2'],
'MaxAgeSeconds' => 50
]
]
]);
printf("RequestId:%s\n", $resp['RequestId']);
}catch (Obs\Common\ObsException $obsException){
printf("ExceptionCode:%s\n", $obsException->getExceptionCode());
printf("ExceptionMessage:%s\n", $obsException->getExceptionMessage());
} 
