A bucket is a container that stores objects in OSS. Every object is contained in a bucket.

Create a bucket

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

OSSCreateBucketRequest * create = [OSSCreateBucketRequest new];
create.bucketName = @"<bucketName>";
create.xOssACL = @"public-read";
create.location = @"oss-cn-hangzhou";

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;
}];
			
Note
  • You can create up to 100 buckets in a region.
  • The name of each bucket must be globally unique in OSS. To create a bucket, you must select a unique bucket name.
  • When you create a bucket, you can configure the bucket ACL. If no ACL is configured, the bucket ACL is Private.
  • After a bucket is created, the region to which the bucket belongs is returned.

List buckets

The following code provides an example on how to list all buckets owned by the requester:

OSSGetServiceRequest * getService = [OSSGetServiceRequest new];
OSSTask * getServiceTask = [client getService:getService];
[getServiceTask continueWithBlock:^id(OSSTask *task) {
    if (! task.error) {
        OSSGetServiceResult * result = task.result;
        NSLog(@"buckets: %@", result.buckets);
        NSLog(@"owner: %@, %@", result.ownerId, result.ownerDispName);
        [result.buckets enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSDictionary * bucketInfo = obj;
            NSLog(@"BucketName: %@", [bucketInfo objectForKey:@"Name"]);
            NSLog(@"CreationDate: %@", [bucketInfo objectForKey:@"CreationDate"]);
            NSLog(@"Location: %@", [bucketInfo objectForKey:@"Location"]);
        }];
    }
    return nil;
}];
			
Note Anonymous users do not have the permission to list buckets.

List objects in a bucket

The following code provides an example on how to list objects in a bucket:

OSSGetBucketRequest * getBucket = [OSSGetBucketRequest new];
getBucket.bucketName = @"<bucketName>";
// getBucket.marker = @"";
// getBucket.prefix = @"";
// getBucket.delimiter = @"";
OSSTask * getBucketTask = [client getBucket:getBucket];
[getBucketTask continueWithBlock:^id(OSSTask *task) {
    if (! task.error) {
        OSSGetBucketResult * result = task.result;
        NSLog(@"get bucket success!") ;
        for (NSDictionary * objectInfo in result.contents) {
            NSLog(@"list object: %@", objectInfo);
        }
    } else {
        NSLog(@"get bucket failed, error: %@", task.error);
    }
    return nil;
}];
Note
  • To list objects in a bucket, you must have the access permission on the bucket.
  • You can set the prefix, marker, delimiter, and max-keys parameters to list desired results only.

Delete a bucket

The following code provides an example on how to delete a bucket:

OSSDeleteBucketRequest * delete = [OSSDeleteBucketRequest new];
delete.bucketName = @"<bucketName>";
OSSTask * deleteTask = [client deleteBucket:delete];
[deleteTask continueWithBlock:^id(OSSTask *task) {
    if (! task.error) {
        NSLog(@"delete bucket success!") ;
    } else {
        NSLog(@"delete bucket failed, error: %@", task.error);
    }
    return nil;
}];
Notice Before you delete a bucket, delete all objects or object parts generated by incomplete multipart upload in the bucket.