Creating a Bucket

API Description

You can use this API to create a bucket and name it as you specify. The created bucket name must be unique in OBS. If the same account creates buckets of the same name in the same region, no error will be reported and the bucket properties comply with those set in the first creation request. If the same account repeatedly creates buckets with the same name in the same region, status code 200 is returned. In other cases, status code 409 is returned, indicating that the bucket already exists. Each user can create a maximum of 100 buckets.

Method Definition

ObsClient.createBucket(bucketName, header, location)

Request Parameters

Field

Type

Optional or Mandatory

Description

bucketName

str

Mandatory

Bucket name

A bucket name must comply with the following rules:
  • Contains 3 to 63 characters chosen from lowercase letters, digits, hyphens (-), and periods (.), and starts with a digit or letter.
  • Cannot be an IP address or similar.
  • Cannot start or end with a hyphen (-) or period (.).
  • Cannot contain two consecutive periods (.), for example, my..bucket.
  • Cannot contain periods (.) and hyphens (-) adjacent to each other, for example, my-.bucket or my.-bucket.

header

CreateBucketHeader

Optional

Additional header of the request for creating a bucket

location

str

Mandatory unless the region where the OBS service resides is not the default region.

Region where the bucket will be created.

This parameter value must match the endpoint you use.

  • When the endpoint of the CN North-Beijing1 region is used for bucket creation,
    • if location is not included, the bucket will be created in CN North-Beijing1 (cn-north-1) by default.
    • if another region, for example, CN North-Beijing4 (cn-north-4), is specified for location, the bucket will be created in the CN North-Beijing4 region.
  • When the endpoint of a region other than CN North-Beijing1 is used for bucket creation, location must be set to the region that the used endpoint corresponds to.

    For example, if obs.cn-north-4.myhuaweicloud.com is used, you must set location to cn-north-4.

For details about OBS regions and endpoints, see Regions and Endpoints.

Returned Results

Type

Description

GetResult

SDK common result object

Sample Code

With the endpoint of the default region CN North-Beijing1 (cn-north-1) used for bucket creation, you do not need to specify location and the bucket will be created in the CN North-Beijing1 region.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
try:
    from obs import CreateBucketHeader
    from obs import ObsClient
    from obs import StorageClass

    ak = '*** Provide your Access Key ***'
    sk = '*** Provide your Secret Key ***'
    server='https://obs.cn-north-1.myhuaweicloud.com'

    obsClient = ObsClient(access_key_id=ak, secret_access_key=sk, server=server)
    resp = obsClient.createBucket(bucketName='bucketname', header=CreateBucketHeader(aclControl='private', storageClass=StorageClass.WARM)) 
     
    if resp.status < 300: 
        print('requestId:', resp.requestId) 
    else: 
        print('errorCode:', resp.errorCode) 
        print('errorMessage:', resp.errorMessage)
except:
    import traceback
    print(traceback.format_exc())
With the endpoint of a region other than CN North-Beijing1 (cn-north-1) used for bucket creation, you must set location to the region that the used endpoint corresponds to. The bucket will be created in the region specified for location.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
try:
    from obs import CreateBucketHeader
    from obs import ObsClient

    ak = '*** Provide your Access Key ***'
    sk = '*** Provide your Secret Key ***'
    # Use the endpoint of a non-default region.
    server = 'https://obs.other-region.myhuaweicloud.com'
    obsClient = ObsClient(access_key_id=ak, secret_access_key=sk, server=server)
    resp = obsClient.createBucket(bucketName='bucketname', location="other-region")
     
    if resp.status < 300: 
        print('requestId:', resp.requestId) 
    else: 
        print('errorCode:', resp.errorCode) 
        print('errorMessage:', resp.errorMessage)
except:
    import traceback
    print(traceback.format_exc())