• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. API Reference (Python 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

ObsClient.setBucketCors(bucketName, corsRuleList)

Request Parameter

Field

Type

Optional or Mandatory

Description

bucketName

str

Mandatory

Bucket name

corsRuleList

list of CorsRule

Mandatory

CORS rule list of the bucket

Returned Result

Type

Description

GetResult

SDK common result object

Sample Code

try:
    from com.obs.models.cors_rule import CorsRule 
     
    cors1 = CorsRule(id='rule1', allowedMethod=['PUT', 'POST', 'GET', 'DELETE', 'HEAD'], 
                     allowedOrigin=['obs.hostname', 'obs.hostname1'], allowedHeader=['obs-header-1'], 
                     maxAgeSecond=60) 
    cors2 = CorsRule(id='rule2', allowedMethod=['PUT', 'POST', 'GET'], 
                     allowedOrigin=['obs.hostname', 'obs.hostname1'], allowedHeader=['header-1', 'header-2'], 
                     maxAgeSecond=50, exposeHeader=['head1']) 
     
    corsList = [cors1, cors2] 
     
    resp = obsClient.setBucketCors('bucketname', corsList) 
      
    if resp.status < 300: 
        print('requestId:', resp.requestId) 
    else: 
        print('errorCode:', resp.errorCode) 
        print('errorMessage:', resp.errorMessage)
except:
    import traceback
    print(traceback.format_exc())