iOS列举存储空间

存储空间(Bucket)是用来存储对象(Object)的容器。对象都隶属于存储空间。存储空间按照字母顺序排列。您可以列举当前账号所有地域下符合指定条件的存储空间。

注意事项

  • 使用本文示例前您需要先通过自定义域名、STS等方式新建OSSClient,具体请参见初始化

    说明

    所列举的存储空间的所属地域取决于初始化配置的endpoint地域信息。

列举所有存储空间

以下代码用于列举当前账号所有地域下的存储空间。

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"]);
        }];
    } else {
        NSLog(@"get service failed, error: %@", task.error);
    }
    return nil;
}];
// 实现同步阻塞等待任务完成。
// [getServiceTask waitUntilFinished];

列举指定前缀的存储空间

以下代码用于列举当前账号所有地域下以example为前缀(prefix)的存储空间。

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;
}];
// 实现同步阻塞等待任务完成。
// [getServiceTask waitUntilFinished];

列举指定marker之后的存储空间

以下代码用于列举当前账号所有地域下名称的字母序排在examplebucket之后的存储空间。

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;
}];
// 实现同步阻塞等待任务完成。
// [getServiceTask waitUntilFinished];

列举指定个数的存储空间

以下代码用于列举当前账号所有地域下的存储空间,并指定列举的最大个数为500。

OSSGetServiceRequest * getService = [OSSGetServiceRequest new];
// 列举当前账号所有地域下的存储空间,限定此次列举存储空间的最大个数为500。默认值为100,最大值为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;
}];
// 实现同步阻塞等待任务完成。
// [getServiceTask waitUntilFinished];

相关文档