Todos os produtos
Search
Central de documentação

Object Storage Service:Listar objetos (iOS SDK)

Última atualização: Jul 03, 2026

Este tópico descreve como listar objetos em um bucket. Por exemplo, você pode listar todos os objetos, uma quantidade específica de objetos ou objetos cujos nomes contenham um prefixo específico em um bucket.

Observações

  • Antes de executar o código de exemplo deste tópico, crie uma instância do OSSClient usando métodos como nome de domínio personalizado ou Security Token Service (STS). Para mais informações, consulte Inicialização.

    Nota

    Ao inicializar uma instância do OSSClient, especifique um endpoint que corresponda à região do bucket.

  • Para listar arquivos, é necessária a permissão ossGetObject. Para mais informações, consulte Conceder políticas de acesso personalizadas a usuários RAM.

Listar um número específico de objetos

O código de exemplo a seguir mostra como listar até 20 objetos em um bucket chamado examplebucket:

OSSGetBucketRequest * getBucket = [OSSGetBucketRequest new];
// Specify the name of the bucket. Example: examplebucket. 
getBucket.bucketName = @"examplebucket";
// Specify the maximum number of returned objects. By default, the value of this parameter is 100. The maximum value you can specify is 1000. 
getBucket.maxKeys = 20;

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;
}];
// Implement synchronous blocking to wait for the task to complete. 
// [getBucketTask waitUntilFinished];

Listar objetos cujos nomes contenham um prefixo específico

O código de exemplo a seguir mostra como listar objetos com o prefixo "file" no bucket examplebucket:

OSSGetBucketRequest * getBucket = [OSSGetBucketRequest new];
// Specify the name of the bucket. Example: examplebucket. 
getBucket.bucketName = @"examplebucket";
// Specify the prefix. 
getBucket.prefix = @"file";

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;
}];
// Implement synchronous blocking to wait for the task to complete. 
// [getBucketTask waitUntilFinished]

Listar objetos cujos nomes são alfabeticamente posteriores ao valor de marker

O código de exemplo a seguir mostra como listar objetos cujos nomes vêm depois do marcador exampleobject.txt, em ordem alfabética, no bucket examplebucket:

OSSGetBucketRequest * getBucket = [OSSGetBucketRequest new];
// Specify the name of the bucket. Example: examplebucket. 
getBucket.bucketName = @"examplebucket";
// Specify the marker. Objects whose names are alphabetically after the marker are returned. 
// If the object specified by marker does not exist in the bucket, the objects whose names are alphabetically after the value of marker are returned. 
getBucket.marker = @"exampleobject.txt";

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;
}];
// Implement synchronous blocking to wait for the task to complete. 
// [getBucketTask waitUntilFinished]

Listar objetos por página

Primeiro, declare as variáveis globais relacionadas à operação de listagem.

@interface ... {
    NSString *_marker;
    BOOL _isCompleted;
}
@end

O código de exemplo a seguir mostra como listar objetos paginados no bucket examplebucket. Neste exemplo, até 20 objetos são retornados por página.

 do {
    OSSGetBucketRequest *getBucket = [OSSGetBucketRequest new];
    getBucket.bucketName = @"examplebucket";
    getBucket.marker = _marker;  // Accessing the _marker instance variable
    getBucket.maxKeys = 20;

    OSSTask *getBucketTask = [client getBucket:getBucket];
    [getBucketTask continueWithBlock:^id(OSSTask *task) {
        if (!task.error) {
            OSSGetBucketResult *result = task.result;
            NSLog(@"Get bucket success!");
            NSLog(@"objects: %@", result.contents);
            if (result.isTruncated) {
                _marker = result.nextMarker;  // Update the _marker instance variable
                _isCompleted = NO;
            } else {
                _isCompleted = YES;
            }
        } else {
            _isCompleted = YES;
            NSLog(@"Get bucket failed, error: %@", task.error);
        }
        return nil;
    }];
    // Implement synchronous blocking to wait for the task to complete. 
    [getBucketTask waitUntilFinished];
} while (!_isCompleted);

Listar objetos com prefixo especificado por página

Primeiro, declare as variáveis globais relacionadas à operação de listagem.

@interface ... {
    NSString *_marker;
    BOOL _isCompleted;
}
@end

O código de exemplo a seguir mostra como listar objetos com o prefixo "file" no bucket examplebucket. Neste exemplo, até 20 objetos são retornados por página.

do {
    OSSGetBucketRequest *getBucket = [OSSGetBucketRequest new];
    getBucket.bucketName = @"examplebucket";
    getBucket.marker = _marker;  // Accessing the _marker instance variable
    getBucket.maxKeys = 20;
    getBucket.prefix = @"file";

    OSSTask *getBucketTask = [client getBucket:getBucket];
    [getBucketTask continueWithBlock:^id(OSSTask *task) {
        if (!task.error) {
            OSSGetBucketResult *result = task.result;
            NSLog(@"Get bucket success!");
            NSLog(@"objects: %@", result.contents);

            if (result.isTruncated) {
                _marker = result.nextMarker;  // Update the _marker instance variable
                _isCompleted = NO;
            } else {
                _isCompleted = YES;
            }
        } else {
            _isCompleted = YES;
            NSLog(@"Get bucket failed, error: %@", task.error);
        }
        return nil;
    }];
    // Implement synchronous blocking to wait for the task to complete. 
    [getBucketTask waitUntilFinished];
} while (!_isCompleted);

Referências

  • Para mais informações sobre a operação de API para listar objetos, consulte GetBucket (ListObjects).

  • Para mais informações sobre como inicializar uma instância do OSSClient, consulte Inicialização.