• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Android SDK)
  4. Versioning Management
  5. Listing Versioning Objects

Listing Versioning Objects

You can call ObsClient.listVersions to list versioning objects.

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

Object name to start with when listing versioning objects in a bucket. All versioning objects whose names follow this parameter are listed in the lexicographical order.

maxKeys

Maximum number of versioning objects listed. The value ranges from 1 to 1000. If the value is not in this range, 1000 versioning objects are listed 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

All versioning objects are listed in the lexicographical order by object name and version ID. This parameter must be used together with keyMarker.

NOTE:
  • If the value of versionIdMarker is not a version ID specified by keyMarker, versionIdMarker is ineffective.
  • The returned result of ObsClient.listVersions 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.

String endPoint = "yourdomainname";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

ListVersionsResult result = obsClient.listVersions("bucketname");

for(VersionOrDeleteMarker v : result.getVersions()){
    Log.i("ListVersions", "\t" + v.getKey());
    Log.i("ListVersions","\t" + v.getOwner());
    Log.i("ListVersions","\t" + v.isDeleteMarker());
}

// Close obsClient.
obsClient.close();
NOTE:
  • A maximum of 1000 versioning objects can be listed each time. If a bucket contains more than 1000 objects and ListVersionsResult.isTruncated is true in the returned result, not all versioning objects are listed. In such cases, you can use ListVersionsResult.getNextKeyMarker and ListVersionsResult.getNextVersionIdMarker 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:

String endPoint = "yourdomainname";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

ListVersionsResult result = obsClient.listVersions("bucketname", 100);
for(VersionOrDeleteMarker v : result.getVersions()){
    Log.i("ListVersions","\t" + v.getKey());
    Log.i("ListVersions","\t" + v.getOwner());
    Log.i("ListVersions","\t" + v.isDeleteMarker());
}

// Close obsClient.
obsClient.close();

Listing Versioning Objects by Specifying a Prefix

Sample code:

String endPoint = "yourdomainname";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

// Set the prefix to prefix and the number to 100. 
ListVersionsResult result = obsClient.listVersions("bucketname", "prefix", null,null, null, 100);
for(VersionOrDeleteMarker v : result.getVersions()){
    Log.i("ListVersions","\t" + v.getKey());
    Log.i("ListVersions","\t" + v.getOwner());
    Log.i("ListVersions","\t" + v.isDeleteMarker());
}

// Close obsClient.
obsClient.close();

Listing Versioning Objects by Specifying the Start Position

Sample code:

String endPoint = "yourdomainname";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

// List 100 versioning objects whose names following test in lexicographic order.
ListVersionsResult result = obsClient.listVersions("bucketname", null, null, "test", null, 100);

for(VersionOrDeleteMarker v : result.getVersions()){
    Log.i("ListVersions","\t" + v.getKey());
    Log.i("ListVersions","\t" + v.getOwner());
    Log.i("ListVersions","\t" + v.isDeleteMarker());
}

// Close obsClient.
obsClient.close();

Listing All Versioning Objects in Paging Mode

Sample code:

String endPoint = "yourdomainname";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

ListVersionsResult result;
String keyMarker = null;
String versionIdMarker = null;

do{
    result = obsClient.listVersions("bucketname", null, null, keyMarker, versionIdMarker, 100);
    for(VersionOrDeleteMarker v : result.getVersions()){
        Log.i("ListVersions","\t" + v.getKey());
        Log.i("ListVersions","\t" + v.getOwner());
        Log.i("ListVersions","\t" + v.isDeleteMarker());
    }
    keyMarker = result.getNextKeyMarker();
    versionIdMarker = result.getNextVersionIdMarker();
}while(result.isTruncated());

// Close obsClient.
obsClient.close();

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:

String endPoint = "yourdomainname";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

ListVersionsResult result;
String keyMarker = null;
String versionIdMarker = null;

do{
// Set the prefix of the folders to dir/.
    result = obsClient.listVersions("bucketname", "dir/", null, keyMarker, versionIdMarker, 1000);
    for(VersionOrDeleteMarker v : result.getVersions()){
        Log.i("ListVersions","\t" + v.getKey());
        Log.i("ListVersions","\t" + v.getOwner());
        Log.i("ListVersions","\t" + v.isDeleteMarker());
    }
    keyMarker = result.getNextKeyMarker();
    versionIdMarker = result.getNextVersionIdMarker();
}while(result.isTruncated());

// Close obsClient.
obsClient.close();

Listing All Versioning Objects According to Folders in a Bucket

Sample code:

String endPoint = "yourdomainname";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);

ListVersionsResult result = obsClient.listVersions("bucketname", null, "/", null, null, 1000);
Log.i("ListVersions","Objects in the root directory:");
for(VersionOrDeleteMarker v : result.getVersions()){
    Log.i("ListVersions","\t" + v.getKey());
    Log.i("ListVersions","\t" + v.getOwner());
    Log.i("ListVersions","\t" + v.isDeleteMarker());
}

listVersionsByPrefix(obsClient, result);

// Close obsClient.
obsClient.close();

The following is the sample code of the listVersionsByPrefix function, which is used to recursively list objects in sub-folders.

static void listVersionsByPrefix(ObsClient obsClient, ListVersionsResult result) throws ObsException{
    for(String prefix : result.getCommonPrefixes()){
          Log.i("ListVersions","Objects in folder [" + prefix + "]:");
         result = obsClient.listVersions("bucketname", prefix, "/", null, null, 1000);
         for(VersionOrDeleteMarker v : result.getVersions()){
             Log.i("ListVersions","\t" + v.getKey());
             Log.i("ListVersions","\t" + v.getOwner());
             Log.i("ListVersions","\t" + v.isDeleteMarker());
         }
         listVersionsByPrefix(obsClient, result);
     }
}
NOTE:
  • 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 objects 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.