Tous les produits
Search
Centre de documentation

Object Storage Service:List objects (iOS SDK)

Dernière mise à jour :Aug 18, 2026

Cette rubrique explique comment répertorier les objets d'un compartiment. Vous pouvez par exemple lister tous les objets, un nombre spécifique d'objets ou les objets dont le nom contient un préfixe donné dans un compartiment.

Notes

  • Avant d'exécuter l'exemple de code présenté dans cette rubrique, créez une instance OSSClient en utilisant des méthodes telles que l'utilisation d'un nom de domaine personnalisé ou du Security Token Service (STS). Pour plus d'informations, consultez la section Initialisation.

    Remarque

    Lorsque vous initialisez une instance OSSClient, spécifiez un endpoint correspondant à la région du compartiment.

  • Pour répertorier les fichiers, vous avez besoin de l'autorisation ossGetObject. Pour plus d'informations, consultez la section Accorder des stratégies d'accès personnalisées aux utilisateurs RAM.

Répertorier un nombre spécifique d'objets

L'exemple de code suivant montre comment répertorier jusqu'à 20 objets dans un compartiment nommé 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];

Répertorier les objets dont le nom contient un préfixe spécifique

L'exemple de code suivant montre comment répertorier les objets dont le nom contient le préfixe « file » dans un compartiment nommé 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]

Répertorier les objets dont le nom est alphabétiquement supérieur à la valeur du marqueur

L'exemple de code suivant montre comment répertorier les objets dont le nom est alphabétiquement supérieur au marqueur exampleobject.txt dans un compartiment nommé 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]

Répertorier les objets par page

Commencez par déclarer les variables globales liées à l'opération de liste.

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

L'exemple de code suivant montre comment répertorier les objets du compartiment examplebucket par page. Dans cet exemple, jusqu'à 20 objets sont renvoyés par page.

 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);

Répertorier par page les objets dont le nom contient le préfixe spécifié

Commencez par déclarer les variables globales liées à l'opération de liste.

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

L'exemple de code suivant montre comment répertorier les objets dont le nom commence par le préfixe « file » dans le compartiment examplebucket. Dans cet exemple, jusqu'à 20 objets sont renvoyés par page.

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);

Références

  • Pour plus d'informations sur l'opération API que vous pouvez appeler pour répertorier les objets, consultez la section GetBucket (ListObjects).

  • Pour plus d'informations sur l'initialisation d'une instance OSSClient, consultez la section Initialisation.