• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. API Reference (PHP SDK)
  4. Bucket-Related APIs
  5. PUT Bucket cors

PUT Bucket cors

API Description

You can use this API to set CORS rules for a bucket to allow client browsers to send cross-domain requests.

Method Definition

1. ObsClient->setBucketCors(array $parameter)
2. ObsClient->setBucketCorsAsync(array $parameter, callable $callback)

Request Parameter

Field

Type

Optional or Mandatory

Description

Bucket

string

Mandatory

Bucket name

CorsRule

indexed array

Mandatory

CORS rules of a bucket

  

ID

string

Optional

CORS rule ID. It is a 1-255 character string.

AllowedMethod

indexed array of strings

Mandatory

HTTP methods allowed by the CORS rule. Possible values are:

  • GET
  • PUT
  • HEAD
  • POST
  • DELETE

AllowedOrigin

indexed array of strings

Mandatory

Origins (character strings representing domain names) allowed by the CORS rule. Each AllowedOrigin can contain up to one wildcard character (*).

AllowedHeader

indexed array of strings

Optional

Request headers allowed by the CORS rule. Each AllowedHeader can contain up to one wildcard character (*).

MaxAgeSeconds

integer

Optional

Cache duration (in seconds) of the cross-region request result in the client allowed by the CORS rule. The value must be an integer.

ExposeHeader

indexed array of strings

Optional

Additional response headers allowed by the CORS rule. It cannot contain spaces.

Returned Result

Field

Type

Description

HttpStatusCode

integer

HTTP status code

Reason

string

Reason description

RequestId

string

Request ID returned by the OBS server

Sample Code

try{
       $resp = $obsClient -> setBucketCors([
              'Bucket' => 'bucketname',
              'CorsRule' => [
                     [
                           '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());
}