A bucket is a container used to store objects in Object Storage Service (OSS). Each object is contained in a bucket. This topic describes how to create a bucket.

Sample code

The following code provides an example on how to create a bucket named examplebucket:

id<OSSCredentialProvider> credentialProvider = [[OSSAuthCredentialProvider alloc] initWithAuthServerUrl:@"<StsServer>"];
OSSClientConfiguration *cfg = [[OSSClientConfiguration alloc] init];
cfg.maxRetryCount = 3;
cfg.timeoutIntervalForRequest = 15;

// If an OSSClient is released, all tasks in the session are canceled and the session becomes invalid. Make sure that the OSSClient is not released before the request is complete. 
// Set Endpoint to the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set Endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
OSSClient *client = [[OSSClient alloc] initWithEndpoint:@"Endpoint" credentialProvider:credentialProvider clientConfiguration:cfg];

OSSCreateBucketRequest * create = [OSSCreateBucketRequest new];
// Set the bucket name to examplebucket. 
create.bucketName = @"examplebucket";
// Set the access control list (ACL) of the bucket to private. 
create.xOssACL = @"private";
// Set the storage class of the bucket to Infrequent Access (IA). 
create.storageClass = OSSBucketStorageClassIA;

OSSTask * createTask = [client createBucket:create];

[createTask continueWithBlock:^id(OSSTask *task) {
    if (!task.error) {
        NSLog(@"create bucket success!");
    } else {
        NSLog(@"create bucket failed, error: %@", task.error);
    }
    return nil;
}];            

References

  • For more information about the complete sample code that is used to create a bucket, visit GitHub.
  • For more information about the API operation that you can call to create a bucket, see PutBucket.