Configuring CORS Rules for a Bucket

API Description

CORS enables resources to be accessed by web applications from a different domain. You can use this API to configure CORS rules for a bucket to allow client browsers to send cross-domain requests.

Method Definition

ObsClient.setBucketCors(bucketName, corsRuleList)

Request Parameters

Field

Type

Optional or Mandatory

Description

bucketName

str

Mandatory

Bucket name

corsRuleList

list of CorsRule

Mandatory

List of CORS rules of a bucket

Returned Results

Type

Description

GetResult

SDK common result object

Sample Code

try:
    from obs 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())