Use OSSGetServiceRequest to list buckets under the current Alibaba Cloud account in all regions. Results are returned in alphabetical order and are scoped to the region of the endpoint used during OSSClient initialization.
The listed buckets are from the region specified by the endpoint in your initialization configuration.
Prerequisites
Before you begin, ensure that you have an initialized OSSClient instance (see Initialization (iOS SDK)).
List all buckets
The following example lists all buckets in the region associated with your OSSClient endpoint.
OSSGetServiceRequest *getService = [OSSGetServiceRequest new];
OSSTask *getServiceTask = [client getService:getService];
[getServiceTask continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
OSSGetServiceResult *result = task.result;
// Owner information
NSLog(@"Owner ID: %@, Display name: %@", result.ownerId, result.ownerDispName);
// Bucket list
[result.buckets enumerateObjectsUsingBlock:^(NSDictionary *bucketInfo, NSUInteger idx, BOOL *stop) {
NSLog(@"Name: %@", bucketInfo[@"Name"]);
NSLog(@"Location: %@", bucketInfo[@"Location"]);
NSLog(@"CreationDate: %@", bucketInfo[@"CreationDate"]);
}];
} else {
NSLog(@"Failed to list buckets: %@", task.error);
}
return nil;
}];
// To wait for the task synchronously, uncomment the following line:
// [getServiceTask waitUntilFinished];The result object exposes the following fields:
| Field | Type | Description |
|---|---|---|
result.buckets | NSArray | Array of bucket dictionaries, each containing Name, Location, and CreationDate |
result.ownerId | NSString | Owner account ID |
result.ownerDispName | NSString | Owner display name |
List buckets by prefix
Set prefix to return only buckets whose names start with a specific string.
OSSGetServiceRequest *getService = [OSSGetServiceRequest new];
getService.prefix = @"example";
OSSTask *getServiceTask = [client getService:getService];
[getServiceTask continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
OSSGetServiceResult *result = task.result;
[result.buckets enumerateObjectsUsingBlock:^(NSDictionary *bucketInfo, NSUInteger idx, BOOL *stop) {
NSLog(@"Name: %@", bucketInfo[@"Name"]);
NSLog(@"Location: %@", bucketInfo[@"Location"]);
NSLog(@"CreationDate: %@", bucketInfo[@"CreationDate"]);
}];
} else {
NSLog(@"Failed to list buckets: %@", task.error);
}
return nil;
}];List buckets after a marker
Set marker to return only buckets whose names are alphabetically after the specified bucket name. Use this to paginate through large numbers of buckets.
OSSGetServiceRequest *getService = [OSSGetServiceRequest new];
getService.marker = @"examplebucket";
OSSTask *getServiceTask = [client getService:getService];
[getServiceTask continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
OSSGetServiceResult *result = task.result;
[result.buckets enumerateObjectsUsingBlock:^(NSDictionary *bucketInfo, NSUInteger idx, BOOL *stop) {
NSLog(@"Name: %@", bucketInfo[@"Name"]);
NSLog(@"Location: %@", bucketInfo[@"Location"]);
NSLog(@"CreationDate: %@", bucketInfo[@"CreationDate"]);
}];
} else {
NSLog(@"Failed to list buckets: %@", task.error);
}
return nil;
}];Limit the number of results
Set maxKeys to cap the number of buckets returned per request. The default is 100 and the maximum is 1,000.
OSSGetServiceRequest *getService = [OSSGetServiceRequest new];
// Default: 100. Max: 1,000.
getService.maxKeys = 500;
OSSTask *getServiceTask = [client getService:getService];
[getServiceTask continueWithBlock:^id(OSSTask *task) {
if (!task.error) {
OSSGetServiceResult *result = task.result;
[result.buckets enumerateObjectsUsingBlock:^(NSDictionary *bucketInfo, NSUInteger idx, BOOL *stop) {
NSLog(@"Name: %@", bucketInfo[@"Name"]);
NSLog(@"Location: %@", bucketInfo[@"Location"]);
NSLog(@"CreationDate: %@", bucketInfo[@"CreationDate"]);
}];
} else {
NSLog(@"Failed to list buckets: %@", task.error);
}
return nil;
}];Parameters
| Parameter | Type | Default | Max | Description |
|---|---|---|---|---|
prefix | NSString | — | — | Returns only buckets whose names start with this value |
marker | NSString | — | — | Returns only buckets whose names are alphabetically after this value |
maxKeys | int | 100 | 1,000 | Maximum number of buckets to return |
What's next
ListBuckets (GetService) — underlying API reference
Initialize an OSSClient instance — OSSClient setup