Creating a Bucket (SDK for Python)
Function
This API creates an OBS bucket. Buckets are containers for storing objects (files uploaded to OBS) in OBS.
When creating a bucket, you can also configure parameters such as the storage class, region, and access control as needed.
Restrictions
- To create a bucket, you must have the obs:bucket:CreateBucket permission. IAM is recommended for granting permissions. For details, see IAM Custom Policies.
- The mapping between OBS regions and endpoints must comply with what is listed in Regions and Endpoints.
When creating a bucket, if you use the endpoint obs.myhuaweicloud.com for client initialization, you do not have to specify a region (indicated by location) where the bucket will be created, because OBS automatically creates the bucket in the CN North-Beijing1 (cn-north-1) region. However, if the endpoint you use is not obs.myhuaweicloud.com, you must specify a region that matches the used endpoint, or status code 400 is returned.
For example, if the endpoint used for initialization is obs.ap-southeast-1.myhuaweicloud.com, you must set Location to ap-southeast-1 when creating a bucket.
- A maximum of 100 buckets (regardless of regions) can be created for an account. There is no limit on the number and size of objects in a bucket.
- The created bucket name must be unique in OBS. If you repeatedly create buckets with the same name in the same region, an HTTP status code 200 will be returned. In other cases, creating a bucket with an existing bucket name will have an HTTP status code 409 returned, indicating that such a bucket already exists.
- The name of a deleted bucket can be reused for another bucket or a parallel file system at least 30 minutes after the deletion.
Method
ObsClient.createBucket(bucketName, header, location)
Request Parameters
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
bucketName |
str |
Yes |
Explanation: Bucket name Restrictions:
Default value: None |
header |
No |
Explanation: Headers in the request used for configuring the storage class, redundancy policy, and other basic information about the bucket Value range: See Table 2. Default value: None |
|
location |
str |
Yes if the region where the OBS service resides is not the default region |
Explanation: Region where a bucket will be created. Restrictions: If the used endpoint is obs.myhuaweicloud.com, this parameter is not required. If any other endpoint is used, this parameter is required. Value range: To learn about valid regions and endpoints, see Regions and Endpoints. An endpoint is the request address for calling an API. Endpoints vary depending on services and regions. To obtain the regions and endpoints, contact the enterprise administrator. Default value: If obs.myhuaweicloud.com is used as the endpoint and no region is specified, cn-north-1 (the CN North-Beijing1 region) is used by default. |
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
aclControl |
str |
No |
Explanation: Access control list (ACL) that can be pre-defined during bucket creation Value range: See Table 3. Default value: PRIVATE |
storageClass |
str |
No |
Explanation: Bucket storage class that can be pre-defined during bucket creation Value range: See Table 4. Default value: STANDARD |
extensionGrants |
list of ExtensionGrant |
No |
Explanation: Extended permission list that can be specified during bucket creation Value range: See Table 5. Default value: None |
availableZone |
str |
No |
Explanation: Data redundancy type that can be specified during bucket creation Restrictions: Multi-AZ redundancy is not available for Archive storage. If the region where the bucket is located does not support multi-AZ storage, single-AZ storage is used by default. Value range: To configure multi-AZ storage for the bucket, set this parameter to 3az. To configure single-AZ storage (default value assigned by OBS) for the bucket, you do not need to specify this parameter. Default value: If this parameter is left blank, single AZ is used by default. |
epid |
str |
No |
Explanation: Enterprise project ID that can be specified during bucket creation. If you have enabled Enterprise Project Management Service (EPS), you can obtain the project ID from the EPS console. Restrictions: The value of Epid is a Universally Unique Identifier (UUID). Epid is not required if you have not enabled EPS yet. Example: 9892d768-2d13-450f-aac7-ed0e44c2585f Value range: See How Do I Obtain an Enterprise Project ID? Default value: None |
isPFS |
bool |
No |
Explanation: Whether a parallel file system is created Value range:
Default value: False |
Constant |
Default Value |
Description |
---|---|---|
HeadPermission.PRIVATE |
private |
Private read/write A bucket or object can only be accessed by its owner. |
HeadPermission.PUBLIC_READ |
public-read |
Public read and private write If this permission is granted on a bucket, anyone can read the object list, multipart uploads, metadata, and object versions in the bucket. If it is granted on an object, anyone can read the content and metadata of the object. |
HeadPermission.PUBLIC_READ_WRITE |
public-read-write |
Public read/write If this permission is granted on a bucket, anyone can read the object list, multipart tasks, metadata, and object versions in the bucket, and can upload or delete objects, initiate multipart upload tasks, upload parts, assemble parts, copy parts, and abort multipart upload tasks. If it is granted on an object, anyone can read the content and metadata of the object. |
HeadPermission.PUBLIC_READ_DELIVERED |
public-read-delivered |
Public read on a bucket as well as objects in the bucket If this permission is granted on a bucket, anyone can read the object list, multipart tasks, metadata, and object versions, and read the content and metadata of objects in the bucket.
NOTE:
PUBLIC_READ_DELIVERED cannot be applied to objects. |
HeadPermission.PUBLIC_READ_WRITE_DELIVERED |
public-read-write-delivered |
Public read/write on a bucket as well as objects in the bucket If this permission is granted on a bucket, anyone can read the object list, multipart uploads, metadata, and object versions in the bucket, and can upload or delete objects, initiate multipart upload tasks, upload parts, assemble parts, copy parts, and abort multipart uploads. They can also read the content and metadata of objects in the bucket.
NOTE:
PUBLIC_READ_WRITE_DELIVERED cannot be applied to objects. |
HeadPermission.BUCKET_OWNER_FULL_CONTROL |
public-read-write-delivered |
If this permission is granted on an object, only the bucket and object owners have the full control over the object. By default, if you upload an object to a bucket of any other user, the bucket owner does not have the permissions on your object. After you grant this policy to the bucket owner, the bucket owner can have full control over your object. |
Parameter |
Type |
Description |
---|---|---|
STANDARD |
Standard storage class |
Explanation: Features low access latency and high throughput and is used for storing massive, frequently accessed (multiple times a month) or small objects (< 1 MB) requiring quick response. |
WARM |
Infrequent Access storage class |
Explanation: Used for storing data that is semi-frequently accessed (fewer than 12 times a year) but is instantly available when needed. |
COLD |
Archive storage class |
Explanation: Used for storing rarely accessed (once a year) data. |
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
granteeId |
str |
No |
Explanation: Account (domain) ID of the grantee Value range: To obtain the account ID, see How Do I Get My Account ID and IAM User ID? (SDK for Python) Default value: None |
permission |
str |
No |
Explanation: Custom permissions for the bucket Value range: You can select one or more permissions from Table 6. Default value: None |
Constant |
Description |
---|---|
READ |
Read permission A grantee with this permission for a bucket can obtain the list of objects, multipart uploads, bucket metadata, and object versions in the bucket. A grantee with this permission for an object can obtain the object content and metadata. |
WRITE |
Write permission A grantee with this permission for a bucket can upload, overwrite, and delete any object or part in the bucket. Such permission for an object is not applicable. |
READ_ACP |
Permission to read ACL configurations A grantee with this permission can obtain the ACL of a bucket or object. A bucket or object owner has this permission for the bucket or object permanently. |
WRITE_ACP |
Permission to modify ACL configurations A grantee with this permission can update the ACL of a bucket or object. A bucket or object owner has this permission for the bucket or object permanently. A grantee with this permission can modify the access control policy and thus the grantee obtains full access permissions. |
FULL_CONTROL |
Full control access, including read and write permissions for a bucket and its ACL, or for an object and its ACL. A grantee with this permission for a bucket has READ, WRITE, READ_ACP, and WRITE_ACP permissions for the bucket. A grantee with this permission for an object has READ, READ_ACP, and WRITE_ACP permissions for the object. |
Responses
Type |
Description |
---|---|
Explanation: SDK common results |
Parameter |
Type |
Description |
---|---|---|
status |
int |
Explanation: HTTP status code Value range: A status code is a group of digits ranging from 2xx (indicating successes) to 4xx or 5xx (indicating errors). It indicates the status of a response. For more information, see Status Code. Default value: None |
reason |
str |
Explanation: Reason description. Default value: None |
errorCode |
str |
Explanation: Error code returned by the OBS server. If the value of status is less than 300, this parameter is left blank. Default value: None |
errorMessage |
str |
Explanation: Error message returned by the OBS server. If the value of status is less than 300, this parameter is left blank. Default value: None |
requestId |
str |
Explanation: Request ID returned by the OBS server Default value: None |
indicator |
str |
Explanation: Error indicator returned by the OBS server. Default value: None |
hostId |
str |
Explanation: Requested server ID. If the value of status is less than 300, this parameter is left blank. Default value: None |
resource |
str |
Explanation: Error source (a bucket or an object). If the value of status is less than 300, this parameter is left blank. Default value: None |
header |
list |
Explanation: Response header list, composed of tuples. Each tuple consists of two elements, respectively corresponding to the key and value of a response header. Default value: None |
body |
object |
Explanation: Result content returned after the operation is successful. If the value of status is larger than 300, this parameter is left blank. The value varies with the API being called. For details, see Bucket-Related APIs (SDK for Python) and Object-Related APIs (SDK for Python). Default value: None |
Code Examples
This example creates a bucket named examplebucket and specifies its location, ACL, storage class, and redundancy type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
from obs import CreateBucketHeader, HeadPermission from obs import ObsClient import os import traceback # Obtain an AK and SK pair using environment variables or import the AK and SK pair in other ways. Using hard coding may result in leakage. # Obtain an AK and SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html. ak = os.getenv("AccessKeyID") sk = os.getenv("SecretAccessKey") # (Optional) If you use a temporary AK and SK pair and a security token to access OBS, obtain them from environment variables. security_token = os.getenv("SecurityToken") # Set server to the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one in use. server = "https://obs.ap-southeast-1.myhuaweicloud.com" # Create an obsClient instance. # If you use a temporary AK and SK pair and a security token to access OBS, you must specify security_token when creating an instance. obsClient = ObsClient(access_key_id=ak, secret_access_key=sk, server=server) try: # Add additional headers to specify a private bucket in the Infrequent Access storage class that supports multi-AZ storage. header = CreateBucketHeader(aclControl=HeadPermission.PRIVATE, storageClass="STANDARD", availableZone="3az") # Specify the region where the bucket is to be created. The region must be the same as that in the endpoint passed. ap-southeast-1 is used as an example. location = "ap-southeast-1" bucketName = "examplebucket" # Create a bucket. resp = obsClient.createBucket(bucketName, header, location) # If status code 2xx is returned, the API is called successfully. Otherwise, the API call fails. if resp.status < 300: print('Create Bucket Succeeded') print('requestId:', resp.requestId) else: print('Create Bucket Failed') print('requestId:', resp.requestId) print('errorCode:', resp.errorCode) print('errorMessage:', resp.errorMessage) except: print('Create Bucket Failed') print(traceback.format_exc()) |
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot