Este tópico descreve como listar objetos em um bucket. Por exemplo, é possível listar todos os objetos, uma quantidade específica de objetos ou objetos cujos nomes contenham um prefixo específico.
Observações
-
Antes que você execute o código de exemplo neste tópico, crie uma instância do OSSClient usando métodos como um nome de domínio personalizado ou o Security Token Service (STS). Para mais informações, consulte Initialization.
NotaAo 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 Grant custom access policies to RAM users.
Listar uma quantidade específica 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 contêm um prefixo específico
O exemplo de código abaixo ilustra a listagem de objetos cujos nomes contêm o prefixo "file" em um bucket chamado 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 do marker
Confira a seguir um exemplo de código para listar os objetos cujos nomes são alfabeticamente posteriores ao marker exampleobject.txt em um bucket chamado 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
Inicialmente, declare as variáveis globais relacionadas à operação de listagem.
@interface ... {
NSString *_marker;
BOOL _isCompleted;
}
@end
O trecho de código a seguir demonstra como listar objetos no bucket examplebucket por página. Neste exemplo, o sistema retorna até 20 objetos 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 cujos nomes contêm o prefixo especificado por página
Primeiro, declare as variáveis globais associadas à operação de listagem.
@interface ... {
NSString *_marker;
BOOL _isCompleted;
}
@end
Abaixo, você encontra um exemplo de código para listar objetos com o prefixo de nome "file" no bucket examplebucket. Neste cenário, cada página retorna até 20 objetos.
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 da API utilizada para listar objetos, consulte GetBucket (ListObjects).
Para obter mais detalhes sobre como inicializar uma instância do OSSClient, consulte Initialization.