Updated on 2023-11-09 GMT+08:00

Creating a Bucket

You can call createBucket to create a bucket.

Creating a Bucket in Simple Mode

Sample code:

static OBSClient *client;
NSString *endPoint = @"your-endpoint";
// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey.
// 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.
char* ak_env = getenv("AccessKeyID");
char* sk_env = getenv("SecretAccessKey");
NSString *AK = [NSString stringWithUTF8String:ak_env];
NSString *SK = [NSString stringWithUTF8String:sk_env];

// Initialize identity authentication.
OBSStaticCredentialProvider *credentialProvider = [[OBSStaticCredentialProvider alloc] initWithAccessKey:AK secretKey:SK];

//Initialize service configuration.
OBSServiceConfiguration *conf = [[OBSServiceConfiguration alloc] initWithURLString:endPoint credentialProvider:credentialProvider];

// Initialize an instance of OBSClient.
client = [[OBSClient alloc] initWithConfiguration:conf];

//Make a request for creating a bucket.
OBSCreateBucketRequest *request = [[OBSCreateBucketRequest alloc] initWithBucketName:@"bucketname"];
//Create a bucket.
[client createBucket:request completionHandler:^(OBSCreateBucketResponse *response, NSError *error) { 
     NSLog(@"%@",response.location); 
 }];
  • Bucket names are globally unique. Ensure that the bucket you create is named differently from any other bucket.
  • A bucket name must comply with the following rules:
    • Contains 3 to 63 characters, starts with a digit or letter, and supports only lowercase letters, digits, hyphens (-), and periods (.)
    • Cannot be an IP address.
    • 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.
  • If you create buckets of the same name in a region, no error will be reported and the bucket properties comply with those set in the first creation request.
  • In this example, the bucket is of the default ACL (public-read-write), in the OBS Standard storage class, and in the default location where the global domain resides.
  • During bucket creation, if the endpoint you use corresponds to the default region CN North-Beijing1 (cn-north-1), specifying a region is not a must. If the endpoint you use corresponds to any other region, except the default one, you must set the region to the one that the used endpoint corresponds to. Click here to query currently valid regions.
  • When creating a bucket, you can specify its region. For details, see Creating a Bucket with Parameters Specified.

Creating a Bucket with Parameters Specified

When creating a bucket, you can specify the ACL, storage class, and location for the bucket. OBS provides three storage classes for buckets. For details, see Setting or Obtaining the Storage Class of a Bucket. Sample code is as follows:
static OBSClient *client;
NSString *endPoint = @"your-endpoint";
// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey.
// 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.
char* ak_env = getenv("AccessKeyID");
char* sk_env = getenv("SecretAccessKey");
NSString *AK = [NSString stringWithUTF8String:ak_env];
NSString *SK = [NSString stringWithUTF8String:sk_env];

// Initialize identity authentication.
OBSStaticCredentialProvider *credentialProvider = [[OBSStaticCredentialProvider alloc] initWithAccessKey:AK secretKey:SK];
    
//Initialize service configuration.
OBSServiceConfiguration *conf = [[OBSServiceConfiguration alloc] initWithURLString:endPoint credentialProvider:credentialProvider];
    
// Initialize an instance of OBSClient.
client = [[OBSClient alloc] initWithConfiguration:conf];
    
//Create a bucket.
OBSCreateBucketRequest *request = [[OBSCreateBucketRequest alloc] initWithBucketName:@"bucketname"];
// Set the access control policy to public-read-write.
request.bucketACLPolicy = OBSACLPolicyPublicReadWrite;
// Set the storage class to OBS Standard.
request.defaultStorageClass = OBSStorageClassStandard;
// Set the bucket location.
request.configuration = [[OBSBucketConfiguration alloc] initWithLocationConstraint:@"bucketlocation"];
    
[client createBucket:request completionHandler:^(OBSCreateBucketResponse *response, NSError *error) {
    NSLog(@"%@",response.location);
}];