All Products
Search
Document Center

Object Storage Service:Create a bucket by using OSS SDK for iOS

Last Updated:Aug 20, 2024

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

Usage notes

  • Before you run the sample code in this topic, you must create an OSSClient instance by using methods such as using a custom domain name or Security Token Service (STS). For more information, see Initialization.

    Note

    When you initialize an OSSClient instance, specify an endpoint that maps to the region in which you want to create the bucket.

  • 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 sample code provides an example on how to create a bucket named examplebucket:

// Construct a request to create a bucket. 
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;
}];
// Implement synchronous blocking to wait for the task to complete. 
// [createTask waitUntilFinished];          

References

  • For 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.

  • For more information about how to initialize an OSSClient instance, see Initialization.