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

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint For more information about the regions and endpoints supported by OSS, see Regions and endpoints.
  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or STS, see Initialization.
  • To create a bucket, you must have the oss:PutBucket permission. For more information, see Attach a custom policy to a RAM user.

Examples

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 instance is released, all tasks in the session are canceled and the session becomes invalid. Make sure that the OSSClient instance is not released before the request is complete. 
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the 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.