Updated on 2023-11-09 GMT+08:00

Listing Versioning Objects

You can call OBSListObjectsVersionsRequest to list versioning objects in a bucket.

The following table describes the parameters involved in this API.

Parameter

Description

bucketName

Bucket name

prefix

Name prefix that the objects to be listed must contain

keyMarker

Versioning object name to start with when listing versioning objects in a bucket. All versioning objects are listed in the lexicographical order.

maxKeys

Maximum number of versioning object names listed in the response body. The value ranges from 1 to 1000. If the value is not in this range, 1000 versioning objects are returned by default.

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

versionIDMarker

Object name to start with when listing versioning objects in a bucket. All versioning objects are listed in the lexicographical order by object name and version ID. This parameter must be used together with keyMarker.

  • If the value of versionIDMarker is not a version ID specified by keyMarker, versionIDMarker is invalid.
  • The returned result of OBSListObjectsVersionsRequest includes the versioning objects and delete markers.

Listing Versioning Objects in Simple Mode

The following sample code shows how to list versioning objects in simple mode. A maximum of 1000 versioning 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/intl/en-us/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 versioning objects.
OBSListObjectsVersionsRequest *request = [[OBSListObjectsVersionsRequest alloc] initWithBucketName:@"bucketname"];
    
[client listObjectsVersions:request completionHandler:^(OBSListObjectsVersionsResponse *response, NSError *error) {
     for (int i =0; i<response.versionList.count; i++) {
        NSLog(@"%@ \n",response.versionList[i].key);
    }
 }];
  • Information about a maximum of 1000 versioning objects can be listed each time. If a bucket contains more than 1000 objects and response.isTruncated is YES in the returned result, not all versioning objects are listed. In such cases, you can use response.nextKeyMarker and response.versionIdMarker to obtain the start position for next listing.
  • If you want to obtain all versioning 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/intl/en-us/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 versioning objects.
OBSListObjectsVersionsRequest *request = [[OBSListObjectsVersionsRequest alloc] initWithBucketName:@"bucketname"];
    
// Set the number of versioning objects to be listed to 100.
request.maxKeys = [NSNumber numberWithInt:100];
    
    
[client listObjectsVersions:request completionHandler:^(OBSListObjectsVersionsResponse *response, NSError *error) {
     for (int i =0; i<response.versionList.count; i++) {
        NSLog(@"%@ \n",response.versionList[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/intl/en-us/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 versioning objects.
OBSListObjectsVersionsRequest *request = [[OBSListObjectsVersionsRequest alloc] initWithBucketName:@"bucketname"];
    
// Set the prefix.
request.prefix = @"/";
    
[client listObjectsVersions:request completionHandler:^(OBSListObjectsVersionsResponse *response, NSError *error) {
     for (int i =0; i<response.versionList.count; i++) {
        NSLog(@"%@ \n",response.versionList[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/intl/en-us/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 versioning objects.
OBSListObjectsVersionsRequest *request = [[OBSListObjectsVersionsRequest alloc] initWithBucketName:@"bucketname"];
    
// Specify the start position for listing.
request.keyMarker = @"/";
    
[client listObjectsVersions:request completionHandler:^(OBSListObjectsVersionsResponse *response, NSError *error) {
     for (int i =0; i<response.versionList.count; i++) {
        NSLog(@"%@ \n",response.versionList[i].key);
    }
}];

Listing All Versioning 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/intl/en-us/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 versioning objects.
    
__block OBSListObjectsVersionsResponse *result;
    
    // List objects.
OBSListObjectsVersionsRequest *request = [[OBSListObjectsVersionsRequest alloc] initWithBucketName:@"bucketname"];
    
request.maxKeys = [NSNumber numberWithInt:1];
// List all versioning objects.
do {
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    [client listObjectsVersions:request completionHandler:^(OBSListObjectsVersionsResponse *response, NSError *error) {
        result = response;
     for (int i =0; i<response.versionList.count; i++) {
        NSLog(@"%@ \n",response.versionList[i].key);
    }
        request.keyMarker = result.nextKeyMarker;
        dispatch_semaphore_signal(sema);
   }];
   dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
} while (result.isTruncated);

Listing All Versioning 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/intl/en-us/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 versioning objects.
    
__block OBSListObjectsVersionsResponse *result;
    
// List objects.
OBSListObjectsVersionsRequest *request = [[OBSListObjectsVersionsRequest alloc] initWithBucketName:@"bucketname"];
    
request.delimiter = @"/";
// Objects in the root directory
[client listObjectsVersions:request completionHandler:^(OBSListObjectsVersionsResponse *response, NSError *error) {
        
    for (int i =0; i<response.versionList.count; i++) {
        NSLog(@"%@ \n",response.versionList[i].key);
    }
    [self listVersionObjectsByPrefix:client request:request result:response];
}];

listVersionObjectsByPrefix function:

-(void) listVersionObjectsByPrefix:(OBSClient*) client request:(OBSListObjectsVersionsRequest *) request result:            (OBSListObjectsVersionsResponse*) result{
    
    for (NSString * prefix in result.commonPrefixesList){
        NSLog(@"Objects in folder [%@]:",prefix);
        request.prefix = prefix;
        [client listObjectsVersions:request completionHandler:^(OBSListObjectsVersionsResponse *response, NSError *error) {
            for (int i =0; i<response.versionList.count; i++) {
                NSLog(@"%@ \n",response.versionList[i].key);
            }
            [self listVersionObjectsByPrefix:client request:request result:response];
        }];
    }
    
}
  • The previous sample code does not include 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 (/).
  • In the returned result of each recursion, ListVersionsResult.getVersions includes the versioning objects in the folder and ListVersionsResult.getCommonPrefixes includes the sub-folders in the folder.