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 running the sample code in this topic, create an OSSClient instance using a method such as a custom domain name or the Security Token Service (STS). For more information, see Initialization (iOS SDK).
NoteThe listed buckets are from the region specified by the endpoint in your initialization configuration.
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 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 specified 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 in all regions for the current account. Limit the number of buckets to 500. Default: 100. Max: 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 for listing buckets, see ListBuckets (GetService).
For more information about OSSClient instance initialization, see Initialize an OSSClient instance.