Listing Versioning Objects
If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see the API Reference.
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. |
- 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.
// 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 ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID. // 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. String ak = System.getenv("ACCESS_KEY_ID"); String sk = System.getenv("SECRET_ACCESS_KEY_ID"); String endPoint = "https://your-endpoint"; // 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()); }
- 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:
// 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 ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID. // 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. String ak = System.getenv("ACCESS_KEY_ID"); String sk = System.getenv("SECRET_ACCESS_KEY_ID"); String endPoint = "https://your-endpoint"; // 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()); }
Listing Versioning Objects by Specifying a Prefix
Sample code:
// 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 ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID. // 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. String ak = System.getenv("ACCESS_KEY_ID"); String sk = System.getenv("SECRET_ACCESS_KEY_ID"); String endPoint = "https://your-endpoint"; // Create an instance of ObsClient. ObsClient obsClient = new ObsClient(ak, sk, endPoint); // List 100 objects whose name prefix is prefix. ListVersionsRequest request = new ListVersionsRequest ("bucketname", 100); request.setPrefix("prefix"); ListVersionsResult result = obsClient.listVersions(request); for(VersionOrDeleteMarker v : result.getVersions()){ Log.i("ListVersions","\t" + v.getKey()); Log.i("ListVersions","\t" + v.getOwner()); Log.i("ListVersions","\t" + v.isDeleteMarker()); }
Listing Versioning Objects by Specifying the Start Position
Sample code:
// 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 ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID. // 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. String ak = System.getenv("ACCESS_KEY_ID"); String sk = System.getenv("SECRET_ACCESS_KEY_ID"); String endPoint = "https://your-endpoint"; // Create an instance of ObsClient. ObsClient obsClient = new ObsClient(ak, sk, endPoint); // List 100 versioning objects whose names following test in lexicographic order. ListVersionsRequest request = new ListVersionsRequest ("bucketname", 100); request.setKeyMarker("test"); ListVersionsResult result = obsClient.listVersions(request); for(VersionOrDeleteMarker v : result.getVersions()){ Log.i("ListVersions","\t" + v.getKey()); Log.i("ListVersions","\t" + v.getOwner()); Log.i("ListVersions","\t" + v.isDeleteMarker()); }
Listing All Versioning Objects in Paging Mode
Sample code:
// 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 ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID. // 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. String ak = System.getenv("ACCESS_KEY_ID"); String sk = System.getenv("SECRET_ACCESS_KEY_ID"); String endPoint = "https://your-endpoint"; // Create an instance of ObsClient. ObsClient obsClient = new ObsClient(ak, sk, endPoint); ListVersionsResult result; ListVersionsRequest request = new ListVersionsRequest ("bucketname", 100); do{ result = obsClient.listVersions(request); for(VersionOrDeleteMarker v : result.getVersions()){ Log.i("ListVersions","\t" + v.getKey()); Log.i("ListVersions","\t" + v.getOwner()); Log.i("ListVersions","\t" + v.isDeleteMarker()); } request.setKeyMarker(result.getNextKeyMarker()); request.setVersionIdMarker(result.getNextVersionIdMarker()); }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:
// 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 ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID. // 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. String ak = System.getenv("ACCESS_KEY_ID"); String sk = System.getenv("SECRET_ACCESS_KEY_ID"); String endPoint = "https://your-endpoint"; // Create an instance of ObsClient. ObsClient obsClient = new ObsClient(ak, sk, endPoint); ListVersionsResult result; ListVersionsRequest request = new ListVersionsRequest ("bucketname", 100); // Set the prefix of objects in the folder to dir/. request.setPrefix("dir/"); do{ result = obsClient.listVersions(request); for(VersionOrDeleteMarker v : result.getVersions()){ Log.i("ListVersions","\t" + v.getKey()); Log.i("ListVersions","\t" + v.getOwner()); Log.i("ListVersions","\t" + v.isDeleteMarker()); } request.setKeyMarker(result.getNextKeyMarker()); request.setVersionIdMarker(result.getNextVersionIdMarker()); }while(result.isTruncated());
Listing All Versioning Objects According to Folders in a Bucket
Sample code:
// 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 ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID. // 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. String ak = System.getenv("ACCESS_KEY_ID"); String sk = System.getenv("SECRET_ACCESS_KEY_ID"); String endPoint = "https://your-endpoint"; // Create an instance of ObsClient. ObsClient obsClient = new ObsClient(ak, sk, endPoint); ListVersionsRequest request = new ListVersionsRequest ("bucketname", 1000); request.setDelimiter("/"); ListVersionsResult result = obsClient.listVersions(request); 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);
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 + "]:"); ListVersionsRequest request = new ListVersionsRequest ("bucketname", 1000); request.setDelimiter("/"); request.setPrefix(prefix) result = obsClient.listVersions(request); 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); } }
- 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.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot