All Products
Search
Document Center

Object Storage Service:List buckets by using OSS SDK for iOS

Last Updated:Aug 20, 2024

A bucket is a container for objects stored in Object Storage Service (OSS). All objects in OSS are contained in buckets. Buckets are listed in alphabetical order. You can list buckets that belong to the current Alibaba Cloud account in all regions and meet specific conditions.

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 list buckets.

List all buckets within an Alibaba Cloud account

The following sample code provides an example on how to list buckets in all regions within the current Alibaba Cloud account:

OSSGetServiceRequest * getService = [OSSGetServiceRequest new];
// List all buckets in all regions within the current Alibaba Cloud account.     
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"]);
        }];
    } else {
        NSLog(@"get service failed, error: %@", task.error);
    }
    return nil;
}];
// Implement synchronous blocking to wait for the task to complete. 
// [getServiceTask waitUntilFinished];

List buckets whose names contain a specific prefix

The following sample code provides an example on how to list buckets whose names contain the example prefix in all regions within the current Alibaba Cloud account:

OSSGetServiceRequest * getService = [OSSGetServiceRequest new];
getService.prefix = @"example";

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"]);
        }];
    } else {
        NSLog(@"get service failed, error: %@", task.error);
    }
    return nil;
}];
// Implement synchronous blocking to wait for the task to complete. 
// [getServiceTask waitUntilFinished];

List buckets whose names are alphabetically after the bucket specified by marker

The following sample code provides an example on how to list buckets whose names are alphabetically after the bucket named examplebucket in all regions within the current Alibaba Cloud account:

OSSGetServiceRequest * getService = [OSSGetServiceRequest new];
getService.marker = @"examplebucket";

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"]);
        }];
    } else {
        NSLog(@"get service failed, error: %@", task.error);
    }
    return nil;
}];
// Implement synchronous blocking to wait for the task to complete. 
// [getServiceTask waitUntilFinished];

List a specific number of buckets

The following sample code provides an example on how to list the buckets in all regions within the current Alibaba Cloud account and set the maximum number of buckets that can be listed to 500:

OSSGetServiceRequest * getService = [OSSGetServiceRequest new];
// List all buckets that belong to the current account. Set the maximum number of buckets that can be returned by the listing operation to 500. The default value is 100, and the maximum value is 1000. 
getService.maxKeys = 500;
    
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"]);
        }];
    } else {
        NSLog(@"get service failed, error: %@", task.error);
    }
    return nil;
}];
// Implement synchronous blocking to wait for the task to complete. 
// [getServiceTask waitUntilFinished];

References

  • For more information about the API operation that you can call to list buckets, see ListBuckets (GetService).

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