Updated on 2024-09-05 GMT+08:00

Creating a Bucket (SDK for Go)

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. Otherwise, status code 400 is returned.

  • 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.
  • A 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 the same name as an existing bucket 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.
  • Not all regions support the creation of multi-AZ buckets. You can check whether a region allows you to create multi-AZ buckets by referring to Product Pricing Details.

Method

func (obsClient ObsClient) CreateBucket(input *CreateBucketInput) (output *BaseModel, err error)

Request Parameters

Table 1 List of request parameters

Parameter

Type

Mandatory (Yes/No)

Description

input

*CreateBucketInput

Yes

Explanation:

Input parameters for creating a bucket. For details, see Table 2.

Table 2 CreateBucketInput

Parameter

Type

Mandatory (Yes/No)

Description

Bucket

string

Yes

Explanation:

Bucket name

Restrictions:

  • A bucket name must be unique across all accounts and regions.
  • A bucket name:
    • Must be 3 to 63 characters long and start with a digit or letter. Lowercase letters, digits, hyphens (-), and periods (.) are allowed.
    • Cannot be formatted as an IP address.
    • Cannot start or end with a hyphen (-) or period (.).
    • Cannot contain two consecutive periods (..), for example, my..bucket.
    • Cannot contain a period (.) and a hyphen (-) adjacent to each other, for example, my-.bucket or my.-bucket.
  • If you repeatedly create buckets of the same name in the same region, no error will be reported and the bucket attributes comply with those set in the first creation request.

Default value:

None

Location

string

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.

ACL

AclType

No

Explanation:

Access control list (ACL) that can be pre-defined when a bucket is created. For details about ACLs, see ACLs.

Value range:

See Table 3.

Default value:

private

StorageClass

StorageClassType

No

Explanation:

Bucket storage class that can be pre-defined during bucket creation

Value range:

StorageClassType lists the available value options.

Default value:

STANDARD

GrantReadId

string

No

Explanation:

ID (domain_id) of an account the READ permission is granted to. The account with the READ permission can list objects, multipart uploads, and object versions in the bucket you are creating, and can obtain bucket metadata.

Value range:

To obtain the account ID, see How Do I Get My Account ID and User ID?

Default value:

None

GrantWriteId

string

No

Explanation:

ID (domain_id) of an account the WRITE permission is granted to. The account with the WRITE permission can create, delete, and overwrite objects in the bucket you are creating, and can initiate or abort multipart uploads, as well as upload, copy, and assemble parts.

Value range:

To obtain the account ID, see How Do I Get My Account ID and User ID?

Default value:

None

GrantReadAcpId

string

No

Explanation:

ID (domain_id) of an account the READ_ACP permission is granted to. The account with the READ_ACP permission can read the ACL information of the bucket you are creating.

Value range:

To obtain the account ID, see How Do I Get My Account ID and User ID?

Default value:

None

GrantWriteAcpId

string

No

Explanation:

ID (domain_id) of an account the WRITE_ACP permission is granted to. The account with the WRITE_ACP permission can modify the ACL information of the bucket you are creating.

Value range:

To obtain the account ID, see How Do I Get My Account ID and User ID?

Default value:

None

GrantFullControlId

string

No

Explanation:

ID (domain_id) of an account the FULL_CONTROL permission is granted to. The account with the FULL_CONTROL permission can perform any operation on the bucket you are creating.

Value range:

To obtain the account ID, see How Do I Get My Account ID and User ID?

Default value:

None

GrantReadDeliveredId

string

No

Explanation:

ID (domain_id) of an account the READ permission is granted to. By default, this READ permission applies to all objects in the bucket.

Value range:

To obtain the account ID, see How Do I Get My Account ID and User ID?

Default value:

None

GrantFullControlDeliveredId

string

No

Explanation:

ID (domain_id) of an account the FULL_CONTROL permission is granted to. The account with the FULL_CONTROL permission has full control over the bucket you are creating. By default, the FULL_CONTROL permission applies to all objects in the bucket.

Value range:

To obtain the account ID, see How Do I Get My Account ID and User ID?

Default value:

None

Epid

string

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

Default value:

None

AvailableZone

string

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:

single AZ

IsFSFileInterface

bool

No

Explanation:

Whether a parallel file system is created

Value range:

  • true: A parallel file system is created.
  • false: A bucket is created.

Default value:

false

Table 3 AclType

Constant

Default Value

Description

AclPrivate

private

Private read/write

A bucket or object can only be accessed by its owner.

AclPublicRead

public-read

Public read and private write

If this permission is granted on a bucket, anyone can read the object list, multipart tasks, metadata, and object versions in the bucket.

If it is granted on an object, anyone can read the content and metadata of the object.

AclPublicReadWrite

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.

AclPublicReadDelivered

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:

AclPublicReadDelivered does not apply to objects.

AclPublicReadWriteDelivered

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:

AclPublicReadWriteDelivered does not apply to objects.

AclBucketOwnerFullControl

bucket-owner-full-control

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.

Table 4 StorageClassType

Constant

Default Value

Description

StorageClassStandard

STANDARD

OBS Standard

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.

StorageClassWarm

WARM

OBS Infrequent Access

Used for storing data that is semi-frequently accessed (fewer than 12 times a year) but is instantly available when needed.

StorageClassCold

COLD

OBS Archive

Used for storing rarely accessed (once a year) data.

Responses

Table 5 List of returned results

Parameter

Type

Description

output

*BaseModel

Explanation:

Returned results. For details, see Table 6.

err

error

Explanation:

Error messages returned by the API

Table 6 BaseModel

Parameter

Type

Description

StatusCode

int

Explanation:

HTTP status code

Value range:

A status code is a group of digits that can be 2xx (indicating successes) or 4xx or 5xx (indicating errors). It indicates the status of a response. For more information, see Status Code.

Default value:

None

RequestId

string

Explanation:

Request ID returned by the OBS server

Default value:

None

ResponseHeaders

map[string][]string

Explanation:

HTTP response headers

Default value:

None

Code Examples

This example creates a bucket named examplebucket. For bucket details, see the code comments below.

 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
40
41
42
43
44
45
46
47
48
49
50
package main

import (
    "fmt"
    "os"
    obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
)

func main() {
    //Obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. Using hard coding may result in leakage.
    //Obtain an AK/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/SK pair and a security token to access OBS, you are advised not to use hard coding to reduce leakage risks. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways.
    // securityToken := os.Getenv("SecurityToken")
    // Enter the endpoint corresponding to the region where the bucket is to be created. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
    endPoint := "https://obs.ap-southeast-1.myhuaweicloud.com"
    // Create an obsClient instance.
    // If you use a temporary AK/SK pair and a security token to access OBS, use the obs.WithSecurityToken method to specify a security token when creating an instance.
    obsClient, err := obs.New(ak, sk, endPoint/*, obs.WithSecurityToken(securityToken)*/)
    if err != nil {
        fmt.Printf("Create obsClient error, errMsg: %s", err.Error())
    }
    input := &obs.CreateBucketInput{}
    // Specify a bucket name.
    input.Bucket = "examplebucket"
    // 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.
    input.Location = "ap-southeast-1"
    // Specify an access control policy for the bucket. obs.AclPrivate is used as an example.
    input.ACL = obs.AclPrivate
    // Specify a storage class for the bucket. obs.StorageClassWarm is used as an example. If this parameter is not specified, the created bucket is in the Standard storage class.
    input.StorageClass = obs.StorageClassWarm
    // Specify the AZ type for the bucket. 3az is used as an example. If the bucket region does not support multi-AZ storage, single-AZ storage will be applied. If this parameter is not specified, single-AZ storage is used by default.
    input.AvailableZone = "3az"
   // Create a bucket.
    output, err := obsClient.CreateBucket(input)
    if err == nil {
        fmt.Printf("Create bucket:%s successful!\n", input.Bucket)
        fmt.Printf("RequestId:%s\n", output.RequestId)
        return
    }
    fmt.Printf("Create bucket:%s fail!\n", input.Bucket)
    if obsError, ok := err.(obs.ObsError); ok {
        fmt.Println("An ObsError was found, which means your request sent to OBS was rejected with an error response.")
        fmt.Println(obsError.Error())
    } else {
        fmt.Println("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.")
        fmt.Println(err)
    }
}