Listing Objects
You can call listObjects to list objects in a bucket.
The following table describes the parameters involved in this API.
Parameter |
Description |
Method in OBS iOS SDK |
---|---|---|
bucketName |
Bucket name |
request.bucketName |
prefix |
Name prefix that the objects to be listed must contain |
request.prefix |
marker |
Object name to start with when listing objects in a bucket. All objects are listed in the lexicographical order. |
request.marker |
maxKeys |
Maximum number of objects listed in the response body. The value ranges from 1 to 1000. If the value exceeds 1000, only 1,000 objects are returned. |
request.maxKeys |
delimiter |
Character used to group object names. If the object name contains the delimiter parameter, the character string from the first character to the first delimiter in the object name is grouped under a single result element, commonPrefix. (If a prefix is specified in the request, the prefix must be removed from the object name.) For a parallel file system, if this parameter is not specified, all the content in the directory is recursively listed by default, and subdirectories are also listed. In big data scenarios, parallel file systems usually have deep directory levels and each directory has a large number of files. In such case, you are advised to configure [delimiter="/"] to list the content in the current directory, but not list subdirectories, thereby improving the listing efficiency. |
request.delimiter |
Listing Parts in Simple Mode
The following sample code shows how to list objects in simple mode. A maximum of 1000 objects can be returned.
static OBSClient *client; NSString *endPoint = @"your-endpoint"; // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey. // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/eu/usermanual-ca/ca_01_0003.html. char* ak_env = getenv("AccessKeyID"); char* sk_env = getenv("SecretAccessKey"); NSString *AK = [NSString stringWithUTF8String:ak_env]; NSString *SK = [NSString stringWithUTF8String:sk_env]; // Initialize identity authentication. OBSStaticCredentialProvider *credentialProvider = [[OBSStaticCredentialProvider alloc] initWithAccessKey:AK secretKey:SK]; //Initialize service configuration. OBSServiceConfiguration *conf = [[OBSServiceConfiguration alloc] initWithURLString:endPoint credentialProvider:credentialProvider]; // Initialize an instance of OBSClient. client = [[OBSClient alloc] initWithConfiguration:conf]; // List objects. OBSListObjectsRequest *request = [[OBSListObjectsRequest alloc] initWithBucketName:@"bucketname"]; [client listObjects:request completionHandler:^(OBSListObjectsResponse *response, NSError *error) { for (int i =0; i<response.contentsList.count; i++) { NSLog(@"%@ \n",response.contentsList[i].key); } }];
- A maximum of 1,000 objects can be listed each time. If a bucket contains more than 1,000 objects, response.isTruncated in the response is YES, indicating not all objects were listed. In such case, you can use response.nextMarker to obtain the start position for next listing.
- If you want to obtain all objects in a specified bucket, you can use the paging mode for listing objects.
Listing Versioning Objects by Specifying the Number
Sample code:
static OBSClient *client; NSString *endPoint = @"your-endpoint"; // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey. // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/eu/usermanual-ca/ca_01_0003.html. char* ak_env = getenv("AccessKeyID"); char* sk_env = getenv("SecretAccessKey"); NSString *AK = [NSString stringWithUTF8String:ak_env]; NSString *SK = [NSString stringWithUTF8String:sk_env]; // Initialize identity authentication. OBSStaticCredentialProvider *credentialProvider = [[OBSStaticCredentialProvider alloc] initWithAccessKey:AK secretKey:SK]; //Initialize service configuration. OBSServiceConfiguration *conf = [[OBSServiceConfiguration alloc] initWithURLString:endPoint credentialProvider:credentialProvider]; // Initialize an instance of OBSClient. client = [[OBSClient alloc] initWithConfiguration:conf]; // List objects. OBSListObjectsRequest *request = [[OBSListObjectsRequest alloc] initWithBucketName:@"bucketname"]; // Set the number of objects to be listed to 1. request.maxKeys = [NSNumber numberWithInt:1]; [client listObjects:request completionHandler:^(OBSListObjectsResponse *response, NSError *error) { for (int i =0; i<response.contentsList.count; i++) { NSLog(@"%@ \n",response.contentsList[i].key); } }];
Listing Versioning Objects by Specifying a Prefix
Sample code:
static OBSClient *client; NSString *endPoint = @"your-endpoint"; // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey. // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/eu/usermanual-ca/ca_01_0003.html. char* ak_env = getenv("AccessKeyID"); char* sk_env = getenv("SecretAccessKey"); NSString *AK = [NSString stringWithUTF8String:ak_env]; NSString *SK = [NSString stringWithUTF8String:sk_env]; // Initialize identity authentication. OBSStaticCredentialProvider *credentialProvider = [[OBSStaticCredentialProvider alloc] initWithAccessKey:AK secretKey:SK]; //Initialize service configuration. OBSServiceConfiguration *conf = [[OBSServiceConfiguration alloc] initWithURLString:endPoint credentialProvider:credentialProvider]; // Initialize an instance of OBSClient. client = [[OBSClient alloc] initWithConfiguration:conf]; // List objects. OBSListObjectsRequest *request = [[OBSListObjectsRequest alloc] initWithBucketName:@"bucketname"]; // Set the prefix to testdir2. request.prefix = @"/testdir2/"; [client listObjects:request completionHandler:^(OBSListObjectsResponse *response, NSError *error) { for (int i =0; i<response.contentsList.count; i++) { NSLog(@"%@ \n",response.contentsList[i].key); } }];
Listing Versioning Objects by Specifying the Start Position
Sample code:
static OBSClient *client; NSString *endPoint = @"your-endpoint"; // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey. // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/eu/usermanual-ca/ca_01_0003.html. char* ak_env = getenv("AccessKeyID"); char* sk_env = getenv("SecretAccessKey"); NSString *AK = [NSString stringWithUTF8String:ak_env]; NSString *SK = [NSString stringWithUTF8String:sk_env]; // Initialize identity authentication. OBSStaticCredentialProvider *credentialProvider = [[OBSStaticCredentialProvider alloc] initWithAccessKey:AK secretKey:SK]; //Initialize service configuration. OBSServiceConfiguration *conf = [[OBSServiceConfiguration alloc] initWithURLString:endPoint credentialProvider:credentialProvider]; // Initialize an instance of OBSClient. client = [[OBSClient alloc] initWithConfiguration:conf]; // List objects. OBSListObjectsRequest *request = [[OBSListObjectsRequest alloc] initWithBucketName:@"bucketname"]; //Configure that objects whose names are following test in lexicographical order will be listed. request.marker = @"test"; [client listObjects:request completionHandler:^(OBSListObjectsResponse *response, NSError *error) { for (int i =0; i<response.contentsList.count; i++) { NSLog(@"%@ \n",response.contentsList[i].key); } }];
Listing All Objects in Paging Mode
Sample code:
static OBSClient *client; NSString *endPoint = @"your-endpoint"; // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey. // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/eu/usermanual-ca/ca_01_0003.html. char* ak_env = getenv("AccessKeyID"); char* sk_env = getenv("SecretAccessKey"); NSString *AK = [NSString stringWithUTF8String:ak_env]; NSString *SK = [NSString stringWithUTF8String:sk_env]; // Initialize identity authentication. OBSStaticCredentialProvider *credentialProvider = [[OBSStaticCredentialProvider alloc] initWithAccessKey:AK secretKey:SK]; //Initialize service configuration. OBSServiceConfiguration *conf = [[OBSServiceConfiguration alloc] initWithURLString:endPoint credentialProvider:credentialProvider]; // Initialize an instance of OBSClient. client = [[OBSClient alloc] initWithConfiguration:conf]; __block OBSListObjectsResponse *result; // List objects. OBSListObjectsRequest *request = [[OBSListObjectsRequest alloc] initWithBucketName:@"bucketname"]; request.maxKeys = [NSNumber numberWithInt:1]; //List all objects. do { dispatch_semaphore_t sema = dispatch_semaphore_create(0); [client listObjects:request completionHandler:^(OBSListObjectsResponse *response, NSError *error) { result = response; for (int i =0; i<response.contentsList.count; i++) { NSLog(@"%@ \n",response.contentsList[i].key); } request.marker = result.nextMarker; dispatch_semaphore_signal(sema); }]; dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); } while (result.isTruncated);
Listing All Objects in a Folder
There is no folder concept in OBS. All elements in buckets are objects. Folders are actually objects whose sizes are 0 and whose names end with a slash (/). When you set a folder name as the prefix, objects in this folder will be listed. Sample code is as follows:
static OBSClient *client; NSString *endPoint = @"your-endpoint"; // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey. // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/eu/usermanual-ca/ca_01_0003.html. char* ak_env = getenv("AccessKeyID"); char* sk_env = getenv("SecretAccessKey"); NSString *AK = [NSString stringWithUTF8String:ak_env]; NSString *SK = [NSString stringWithUTF8String:sk_env]; // Initialize identity authentication. OBSStaticCredentialProvider *credentialProvider = [[OBSStaticCredentialProvider alloc] initWithAccessKey:AK secretKey:SK]; //Initialize service configuration. OBSServiceConfiguration *conf = [[OBSServiceConfiguration alloc] initWithURLString:endPoint credentialProvider:credentialProvider]; // Initialize an instance of OBSClient. client = [[OBSClient alloc] initWithConfiguration:conf]; // List objects. OBSListObjectsRequest *request = [[OBSListObjectsRequest alloc] initWithBucketName:@"bucketname"]; request.prefix = @"file/"; __block OBSListObjectsResponse *result; do { dispatch_semaphore_t sema = dispatch_semaphore_create(0); [client listObjects:request completionHandler:^(OBSListObjectsResponse *response, NSError *error) { result = response;for (int i =0; i<response.contentsList.count; i++) { NSLog(@"%@ \n",response.contentsList[i].key); } request.marker = result.nextMarker; dispatch_semaphore_signal(sema); }]; dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);} while (result.isTruncated);
Listing All Folders in the root Directory
Sample code:
static OBSClient *client; NSString *endPoint = @"your-endpoint"; // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey. // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/eu/usermanual-ca/ca_01_0003.html. char* ak_env = getenv("AccessKeyID"); char* sk_env = getenv("SecretAccessKey"); NSString *AK = [NSString stringWithUTF8String:ak_env]; NSString *SK = [NSString stringWithUTF8String:sk_env]; // Initialize identity authentication. OBSStaticCredentialProvider *credentialProvider = [[OBSStaticCredentialProvider alloc] initWithAccessKey:AK secretKey:SK]; //Initialize service configuration. OBSServiceConfiguration *conf = [[OBSServiceConfiguration alloc] initWithURLString:endPoint credentialProvider:credentialProvider]; // Initialize an instance of OBSClient. client = [[OBSClient alloc] initWithConfiguration:conf]; // List objects. OBSListObjectsRequest *request = [[OBSListObjectsRequest alloc] initWithBucketName:@"bucketname"]; request.delimiter = @"/"; // List folders in the root directory. [client listObjects:request completionHandler:^(OBSListObjectsResponse *response, NSError *error) { for (int i =0; i<response.commonPrefixesList.count; i++) { NSLog(@"%@ \n",response.commonPrefixesList[i].prefix); } }];
Listing All Objects According to Folders in a Bucket
Sample code:
static OBSClient *client; NSString *endPoint = @"your-endpoint"; // Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables AccessKeyID and SecretAccessKey. // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/eu/usermanual-ca/ca_01_0003.html. char* ak_env = getenv("AccessKeyID"); char* sk_env = getenv("SecretAccessKey"); NSString *AK = [NSString stringWithUTF8String:ak_env]; NSString *SK = [NSString stringWithUTF8String:sk_env]; // Initialize identity authentication. OBSStaticCredentialProvider *credentialProvider = [[OBSStaticCredentialProvider alloc] initWithAccessKey:AK secretKey:SK]; //Initialize service configuration. OBSServiceConfiguration *conf = [[OBSServiceConfiguration alloc] initWithURLString:endPoint credentialProvider:credentialProvider]; // Initialize an instance of OBSClient. client = [[OBSClient alloc] initWithConfiguration:conf]; // List objects. OBSListObjectsRequest *request = [[OBSListObjectsRequest alloc] initWithBucketName:@"bucketname"]; request.delimiter = @"/"; // Objects in the root directory [client listObjects:request completionHandler:^(OBSListObjectsResponse *response, NSError *error) { [self listObjectsByPrefix:client request:request result:response]; for (int i =0; i<response.contentsList.count; i++) { NSLog(@"%@ \n",response.contentsList[i].key); } }];
listObjectsByPrefix function:
-(void) listObjectsByPrefix:(OBSClient*) client request:(OBSListObjectsRequest *) request result:(OBSListObjectsResponse*) result{ for (OBSCommonPrefix *prefix in result.commonPrefixesList){ request.prefix = prefix.prefix; [client listObjects:request completionHandler:^(OBSListObjectsResponse *response, NSError *error) { // Change asynchronization to synchronization. dispatch_semaphore_t sema = dispatch_semaphore_create(0); NSLog(@"Objects in folder [%@]:",prefix.prefix); for (int i=0; i<response.contentsList.count; i++) { NSLog(@"%@ \n",response.contentsList[i].key); } dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); [self listObjectsByPrefix:client request:request result:response]; }]; } }
- The sample code does not apply to scenarios where the number of objects in a folder exceeds 1000.
- Because objects and sub-folders in a folder are to be listed and all the object names end with a slash (/), delimiter is always a slash (/).
- result.commonPrefixesList contains the sub-folders of the requested folder.
- The listObjectsByPrefix function requires OBSListObjectsModel.h be imported.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.