Listing Object Versions in a Bucket (SDK for Node.js)
If you have any questions during development, post them on the Issues page of GitHub.
Function
This API lists some or all of the object versions in a bucket. When listing the object versions, you can specify the criteria such as the prefix, number, and start position. Returned object versions are listed in alphabetical order by object name.
Restrictions
- To list object versions in a bucket, you must be the bucket owner or have the required permission (obs:bucket:ListBucketVersions in IAM or ListBucketVersions in a bucket policy). For details, see Introduction to OBS Access Control, IAM Custom Policies, and Creating a Custom Bucket Policy.
- To learn about the mappings between OBS regions and endpoints, see Regions and Endpoints.
Method
ObsClient.listVersions(params)
Request Parameters
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
Bucket |
string |
Yes |
Explanation: Bucket name Restrictions:
Value range: The value can contain 3 to 63 characters. Default value: None |
KeyMarker |
string |
No |
Explanation: Object name to start with when listing object versions in a bucket. All object versions following the specified value are listed in lexicographical order by object name. Restrictions: This parameter is only available for listing objects with multiple versions. Value range: The value can contain 1 to 1,024 characters. Default value: None |
VersionIdMarker |
string |
No |
Explanation: Version ID you want to start listing from. All object versions following the value specified by this parameter are listed in alphabetical order by object name and version ID. This parameter must be used together with KeyMarker. KeyMarker specifies the object name, and VersionIdMarker specifies the version ID of the specified object. Restrictions:
Value range: A version ID of the object specified by KeyMarker Default value: None |
Prefix |
string |
No |
Explanation: Prefix that the object names to be listed must contain. Assume that you have the following objects: logs/day1, logs/day2, logs/day3, and ExampleObject.jpg. If you specify logs/ as the prefix, logs/day1, logs/day2, and logs/day3 will be returned. If this parameter is left blank and there are no other filtering criteria, all objects in the bucket will be returned. Restrictions: None Value range: The value can contain 1 to 1,024 characters. Default value: None |
MaxKeys |
number |
No |
Explanation: The maximum number of objects returned in the response in alphabetical order Restrictions: None Value range: The value ranges from 1 to 1000. If the specified value is greater than 1000, only 1,000 objects are returned. Default value: 1000 |
Delimiter |
string |
No |
Explanation: Object names are grouped by this parameter, which is often used with Prefix. If a prefix is specified, objects with the same string from the prefix to the first delimiter are grouped into one CommonPrefixes. If no prefix is specified, objects with the same string from the first character to the first delimiter are grouped into one CommonPrefixes. Assume that a bucket has objects abcd, abcde, and bbcde in it. If Delimiter is set to d and Prefix is set to a, objects abcd and abcde are grouped into a CommonPrefixes with abcd as the prefix. If only Delimiter is set to d, objects abcd and abcde are grouped into a CommonPrefixes with abcd as the prefix, and bbcde is placed separately into another CommonPrefixes with bbcd as the prefix. 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 multiple directory levels, and each directory level has a large number of objects. In such case, you are advised to configure [Delimiter=/] to list only the content in the current directory, without the content in subdirectories, to make the listing more efficient. Restrictions: None Value range: The value can contain 1 to 1,024 characters. Default value: None |
EncodingType |
string |
No |
Explanation: Encoding type for some elements in the response. If Delimiter, KeyMarker, Prefix, CommonPrefixes, NextKeyMarker, or Key contain any control characters that are not supported by the XML 1.0 standard, you can configure EncodingType to encode these parameters in the response. Restrictions: None Value range: url Default value: None. If you leave this parameter blank, encoding is not applied. |
Responses
Type |
Description |
---|---|
NOTE:
This API returns a Promise response, which requires the Promise or async/await syntax. |
Explanation: Returned results. For details, see Table 3. |
Parameter |
Type |
Description |
---|---|---|
CommonMsg |
Explanation: Common information generated after an API call is complete, including the HTTP status code and error code. For details, see Table 4. |
|
InterfaceResult |
Explanation: Results outputted for a successful call. For details, see Table 5. Restrictions: This parameter is not included if the value of Status is greater than 300. |
Parameter |
Type |
Description |
Status |
number |
Explanation: HTTP status code returned by the OBS server. Value range: A status code is a group of digits indicating the status of a response. It ranges from 2xx (indicating successes) to 4xx or 5xx (indicating errors). For details, see Status Codes. |
Code |
string |
Explanation: Error code returned by the OBS server. |
Message |
string |
Explanation: Error description returned by the OBS server. |
HostId |
string |
Explanation: Request server ID returned by the OBS server. |
RequestId |
string |
Explanation: Request ID returned by the OBS server. |
Id2 |
string |
Explanation: Request ID2 returned by the OBS server. |
Indicator |
string |
Explanation: Error code details returned by the OBS server. |
Parameter |
Type |
Description |
---|---|---|
RequestId |
string |
Explanation: Request ID returned by the OBS server. Default value: None |
Location |
string |
Explanation: Region where a bucket is located. |
Name |
string |
Explanation: Bucket name. |
Prefix |
string |
Explanation: Object name prefix, which is consistent with that set in the request. Assume that you have the following objects: logs/day1, logs/day2, logs/day3, and ExampleObject.jpg. If you specify logs/ as the prefix, logs/day1, logs/day2, and logs/day3 will be returned. If this parameter is left blank and there are no other filtering criteria, all objects in the bucket will be returned. |
KeyMarker |
string |
Explanation: Object name to start listing from. All object versions following the value specified by this parameter are listed in alphabetical order by object name. This parameter corresponds to KeyMarker in the request. |
VersionIdMarker |
string |
Explanation: Version ID of the key marker object. Version ID you want to start listing from. All object versions following the value specified by this parameter are listed in alphabetical order by object name and version ID. This parameter must be used together with KeyMarker. KeyMarker specifies the object name, and VersionIdMarker specifies the version ID of the specified object. |
Delimiter |
string |
Explanation: Object names are grouped by this parameter, which is often used with Prefix. If a prefix is specified, objects with the same string from the prefix to the first delimiter are grouped into one CommonPrefixes. If no prefix is specified, objects with the same string from the first character to the first delimiter are grouped into one CommonPrefixes. Assume that a bucket has objects abcd, abcde, and bbcde in it. If Delimiter is set to d and Prefix is set to a, objects abcd and abcde are grouped into a CommonPrefixes with abcd as the prefix. If only Delimiter is set to d, objects abcd and abcde are grouped into a CommonPrefixes with abcd as the prefix, and bbcde is placed separately into another CommonPrefixes with bbcd as the prefix. 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 multiple directory levels, and each directory level has a large number of objects. In such case, you are advised to configure [Delimiter=/] to list only the content in the current directory, without the content in subdirectories, to make the listing more efficient. |
MaxKeys |
number |
Explanation: The maximum number of objects returned in the response in alphabetical order. This parameter corresponds to MaxKeys in the request. |
IsTruncated |
boolean |
Explanation: Whether all results are returned in the response. A maximum of 1,000 objects can be listed at a time. If the number of objects is greater than 1,000, the objects beyond 1,000 cannot be returned. Value range:
|
NextKeyMarker |
string |
Explanation: Object name to start with in the next request for listing object versions. This parameter is returned if some results were not returned. You can set KeyMarker in the next request to the returned value to list the remaining results. |
NextVersionIdMarker |
string |
Explanation: Version ID to start with in the next request for listing object versions. It must be used together with NextKeyMarker. NextVersionIdMarker is returned when some results were not returned. You can set VersionIdMarker in the next request to the returned value to list the remaining results. |
Versions |
Version[] |
Explanation: List of object versions in the bucket. For details, see Table 7. |
DeleteMarkers |
Explanation: List of delete markers in the bucket. For details, see Table 8. |
|
CommonPrefixes |
Table 6[] |
Explanation: List of object name prefixes grouped based on the Delimiter parameter (if specified). For details, see CommonPrefix. |
EncodingType |
string |
Explanation: Encoding type for some elements in the response. If Delimiter, KeyMarker, Prefix, CommonPrefixes, NextKeyMarker, or Key contain any control characters that are not supported by the XML 1.0 standard, you can configure EncodingType to encode these parameters in the response. |
Parameter |
Type |
Description |
---|---|---|
Prefix |
string |
Explanation: Prefix of objects in the bucket. |
Parameter |
Type |
Description |
---|---|---|
Key |
string |
Explanation: Object name. An object is uniquely identified by an object name in a bucket. An object name is a complete path of the object that does not contain the bucket name. For example, if the address for accessing an object is examplebucket.obs.ap-southeast-1.myhuaweicloud.com/folder/test.txt, the object name is folder/test.txt. Value range: The value can contain 1 to 1,024 characters. Default value: None |
VersionId |
string |
Explanation: Object version ID. Value range: The value must contain 32 characters. Default value: None |
LastModified |
string |
Explanation: Time when the object was last modified, in UTC. Value range: UTC time Default value: None |
ETag |
string |
Explanation: Base64-encoded, 128-bit MD5 value of an object. It uniquely identifies the content of an object and can be used to check the object integrity. For example, if the ETag is A when an object is uploaded and is B when the object is downloaded, it indicates that the object content has been changed. The ETag reflects changes to the contents of the object, not its metadata. An object created by an upload or copy operation has a unique ETag. Restrictions: If an object is encrypted using server-side encryption, the ETag is not the MD5 value of the object. Value range: The value must contain 32 characters. Default value: None |
Size |
number |
Explanation: Object size, in bytes. Value range: The value ranges from 0 TB to 48.8 TB, in bytes. Default value: None |
Owner |
Explanation: Object owner. For details, see Table 9. |
|
StorageClass |
Explanation: Storage class of the object. For details, see Table 10. |
|
IsLatest |
boolean |
Explanation: Whether the object is the latest version. Value range:
Default value: false |
Parameter |
Type |
Description |
---|---|---|
Key |
string |
Explanation: Object name. An object is uniquely identified by an object name in a bucket. An object name is a complete path of the object that does not contain the bucket name. For example, if the address for accessing the object is examplebucket.obs.ap-southeast-1.myhuaweicloud.com/folder/test.txt, the object name is folder/test.txt. |
VersionId |
string |
Explanation: Object version ID. |
IsLatest |
boolean |
Explanation: Whether the object is the latest version. Value range:
|
LastModified |
string |
Explanation: Time when the object was last modified, in UTC. |
Owner |
Explanation: Object owner. This parameter contains the domain ID and name of the object owner. For details, see Table 9. |
|
StorageClass |
Explanation: Storage class of the object. For details, see Table 10. |
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
ID |
string |
Yes if used as a request parameter |
Explanation: Account (domain) ID of the owner Value range: To obtain the account ID, see How Do I Get My Account ID and User ID? (SDK for Node.js) Default value: None |
DisplayName |
string |
No |
Explanation: Account name of the owner Default value: None |
Constant |
Default Value |
Description |
---|---|---|
ObsClient.enums.StorageClassStandard |
STANDARD |
Standard storage class. Features low access latency and high throughput and is used for storing massive, frequently accessed (multiple times a month) or small objects (< 1 MB) requiring quick response. |
ObsClient.enums.StorageClassWarm |
WARM |
Infrequent Access storage class. Used for storing data that is semi-frequently accessed (fewer than 12 times a year) but becomes instantly available when needed. |
ObsClient.enums.StorageClassCold |
COLD |
Archive storage class. Used for storing rarely accessed (once a year) data. |
Code Examples: Simple Listing
This example lists object versions in a bucket. A maximum of 1,000 object versions can be returned.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
// Import the OBS library. // Use npm to install the client. const ObsClient = require("esdk-obs-nodejs"); // Use the source code to install the client. // var ObsClient = require('./lib/obs'); // Create an instance of ObsClient. const obsClient = new ObsClient({ // Obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. Using hard coding may result in leakage. // 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. access_key_id: process.env.ACCESS_KEY_ID, secret_access_key: process.env.SECRET_ACCESS_KEY, // (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding, which may result in information leakage. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. // security_token: process.env.SECURITY_TOKEN, // Enter the endpoint corresponding to the region where the bucket is located. CN-Hong Kong is used here in this example. Replace it with the one currently in use. server: "https://obs.ap-southeast-1.myhuaweicloud.com" }); async function listVersions() { try { const params = { // Specify the bucket name. Bucket: "examplebucket", }; // List object versions in the bucket. const result = await obsClient.listVersions(params); if (result.CommonMsg.Status <= 300) { console.log("List objects under the bucket(%s) successful!", params.Bucket); console.log("RequestId: %s", result.CommonMsg.RequestId); // Obtain object versions. for (let j = 0; j < result.InterfaceResult.Versions.length; j++) { const val = result.InterfaceResult.Versions[j]; console.log('Content[%d]-OwnerId:%s, ETag:%s, Key:%s, VersionId, LastModified:%s, Size:%d', j, val.Owner.ID, val.ETag, val.Key, val.VersionId, val.LastModified, val.Size); }; // Obtain delete markers. for (let i = 0; i < result.InterfaceResult.DeleteMarkers.length; i++) { const val = result.InterfaceResult.Versions[j]; console.log('Content[%d]-OwnerId:%s, Key:%s, VersionId, LastModified:%s', j, val.Owner.ID, val.Key, val.VersionId, val.LastModified); }; return; }; console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response."); console.log("Status: %d", result.CommonMsg.Status); console.log("Code: %s", result.CommonMsg.Code); console.log("Message: %s", result.CommonMsg.Message); console.log("RequestId: %s", result.CommonMsg.RequestId); } catch (error) { console.log("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network."); console.log(error); }; }; listVersions(); |
- A maximum of 1,000 object versions can be listed each time. If a bucket contains more than 1,000 object versions, InterfaceResult.IsTruncated in the response is true, indicating not all object versions were listed. In such case, you can use InterfaceResult.NextKeyMarker and InterfaceResult.NextVersionIdMarker to obtain the start position for the next listing.
- To obtain all object versions in a specified bucket at a time, refer to Code Examples: Using Pagination to List All Object Versions.
Code Examples: Listing a Specified Number of Object Versions
This example lists a specified number of object versions in a bucket.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
// Import the OBS library. // Use npm to install the client. const ObsClient = require("esdk-obs-nodejs"); // Use the source code to install the client. // var ObsClient = require('./lib/obs'); // Create an instance of ObsClient. const obsClient = new ObsClient({ // Obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. Using hard coding may result in leakage. // 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. access_key_id: process.env.ACCESS_KEY_ID, secret_access_key: process.env.SECRET_ACCESS_KEY, // (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding, which may result in information leakage. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. // security_token: process.env.SECURITY_TOKEN, // Enter the endpoint corresponding to the region where the bucket is located. CN-Hong Kong is used here in this example. Replace it with the one currently in use. server: "https://obs.ap-southeast-1.myhuaweicloud.com" }); async function listVersions() { try { const params = { // Specify the bucket name. Bucket: "examplebucket", // Specify the maximum number of objects to be returned in alphabetic order. The default value is 1000. 100 is used in this example. MaxKeys: 100, }; // List object versions in the bucket. const result = await obsClient.listVersions(params); if (result.CommonMsg.Status <= 300) { console.log("List version objects under the bucket(%s) successful!", params.Bucket); console.log("RequestId: %s", result.CommonMsg.RequestId); for (let j = 0; j < result.InterfaceResult.Contents.length; j++) { // Obtain object versions. for (let j = 0; j < result.InterfaceResult.Versions.length; j++) { const val = result.InterfaceResult.Versions[j]; console.log('Content[%d]-OwnerId:%s, ETag:%s, Key:%s, VersionId, LastModified:%s, Size:%d', j, val.Owner.ID, val.ETag, val.Key, val.VersionId, val.LastModified, val.Size); }; // Obtain delete markers. for (let i = 0; i < result.InterfaceResult.DeleteMarkers.length; i++) { const val = result.InterfaceResult.Versions[j]; console.log('Content[%d]-OwnerId:%s, Key:%s, VersionId, LastModified:%s', j, val.Owner.ID, val.Key, val.VersionId, val.LastModified); }; return; }; }; console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response."); console.log("Status: %d", result.CommonMsg.Status); console.log("Code: %s", result.CommonMsg.Code); console.log("Message: %s", result.CommonMsg.Message); console.log("RequestId: %s", result.CommonMsg.RequestId); } catch (error) { console.log("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network."); console.log(error); }; }; listVersions(); |
Code Examples: Listing Object Versions by Prefix
This example lists object versions with a specified prefix.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
// Import the OBS library. // Use npm to install the client. const ObsClient = require("esdk-obs-nodejs"); // Use the source code to install the client. // var ObsClient = require('./lib/obs'); // Create an instance of ObsClient. const obsClient = new ObsClient({ // Obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. Using hard coding may result in leakage. // 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. access_key_id: process.env.ACCESS_KEY_ID, secret_access_key: process.env.SECRET_ACCESS_KEY, // (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding, which may result in information leakage. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. // security_token: process.env.SECURITY_TOKEN, // Enter the endpoint corresponding to the region where the bucket is located. CN-Hong Kong is used here in this example. Replace it with the one currently in use. server: "https://obs.ap-southeast-1.myhuaweicloud.com" }); async function listVersions() { try { const params = { // Specify the bucket name. Bucket: "examplebucket", // Specify an object prefix (test/ in this example). Objects whose names contain the specified prefix will be listed. Prefix: "test/", // Specify the maximum number of objects to be returned in alphabetic order. The default value is 1000. 100 is used in this example. MaxKeys: 100, }; // List object versions in the bucket. const result = await obsClient.listVersions(params); if (result.CommonMsg.Status <= 300) { console.log("List version objects under the bucket(%s) successful!", params.Bucket); console.log("RequestId: %s", result.CommonMsg.RequestId); for (let j = 0; j < result.InterfaceResult.Contents.length; j++) { // Obtain object versions. for (let j = 0; j < result.InterfaceResult.Versions.length; j++) { const val = result.InterfaceResult.Versions[j]; console.log('Content[%d]-OwnerId:%s, ETag:%s, Key:%s, VersionId, LastModified:%s, Size:%d', j, val.Owner.ID, val.ETag, val.Key, val.VersionId, val.LastModified, val.Size); }; // Obtain delete markers. for (let i = 0; i < result.InterfaceResult.DeleteMarkers.length; i++) { const val = result.InterfaceResult.Versions[j]; console.log('Content[%d]-OwnerId:%s, Key:%s, VersionId, LastModified:%s', j, val.Owner.ID, val.Key, val.VersionId, val.LastModified); }; return; }; }; console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response."); console.log("Status: %d", result.CommonMsg.Status); console.log("Code: %s", result.CommonMsg.Code); console.log("Message: %s", result.CommonMsg.Message); console.log("RequestId: %s", result.CommonMsg.RequestId); } catch (error) { console.log("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network."); console.log(error); }; }; listVersions(); |
Code Examples: Specifying a Start Position for Listing
This example lists object versions from the specified position.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
// Import the OBS library. // Use npm to install the client. const ObsClient = require("esdk-obs-nodejs"); // Use the source code to install the client. // var ObsClient = require('./lib/obs'); // Create an instance of ObsClient. const obsClient = new ObsClient({ // Obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. Using hard coding may result in leakage. // 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. access_key_id: process.env.ACCESS_KEY_ID, secret_access_key: process.env.SECRET_ACCESS_KEY, // (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding, which may result in information leakage. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. // security_token: process.env.SECURITY_TOKEN, // Enter the endpoint corresponding to the region where the bucket is located. CN-Hong Kong is used here in this example. Replace it with the one currently in use. server: "https://obs.ap-southeast-1.myhuaweicloud.com" }); async function listVersions() { try { const params = { // Specify the bucket name. Bucket: "examplebucket", // Specify an object prefix (test/ in this example). Objects whose names contain the specified prefix will be listed. Prefix: "test/", // Specify the maximum number of objects to be returned in alphabetic order. The default value is 1000. 100 is used in this example. MaxKeys: 100, // Specify the position (test/test2 in this example) where the object listing starts. KeyMarker: "test/test2", }; // List object versions in the bucket. const result = await obsClient.listVersions(params); if (result.CommonMsg.Status <= 300) { console.log("List version objects under the bucket(%s) successful!", params.Bucket); console.log("RequestId: %s", result.CommonMsg.RequestId); for (let j = 0; j < result.InterfaceResult.Contents.length; j++) { // Obtain object versions. for (let j = 0; j < result.InterfaceResult.Versions.length; j++) { const val = result.InterfaceResult.Versions[j]; console.log('Content[%d]-OwnerId:%s, ETag:%s, Key:%s, VersionId, LastModified:%s, Size:%d', j, val.Owner.ID, val.ETag, val.Key, val.VersionId, val.LastModified, val.Size); }; // Obtain delete markers. for (let i = 0; i < result.InterfaceResult.DeleteMarkers.length; i++) { const val = result.InterfaceResult.Versions[j]; console.log('Content[%d]-OwnerId:%s, Key:%s, VersionId, LastModified:%s', j, val.Owner.ID, val.Key, val.VersionId, val.LastModified); }; return; }; }; console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response."); console.log("Status: %d", result.CommonMsg.Status); console.log("Code: %s", result.CommonMsg.Code); console.log("Message: %s", result.CommonMsg.Message); console.log("RequestId: %s", result.CommonMsg.RequestId); } catch (error) { console.log("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network."); console.log(error); }; }; listVersions(); |
Code Examples: Using Pagination to List All Object Versions
This example lists all object versions using pagination.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
// Import the OBS library. // Use npm to install the client. const ObsClient = require("esdk-obs-nodejs"); // Use the source code to install the client. // var ObsClient = require('./lib/obs'); // Create an instance of ObsClient. const obsClient = new ObsClient({ // Obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. Using hard coding may result in leakage. // 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. access_key_id: process.env.ACCESS_KEY_ID, secret_access_key: process.env.SECRET_ACCESS_KEY, // (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding, which may result in information leakage. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. // security_token: process.env.SECURITY_TOKEN, // Enter the endpoint corresponding to the region where the bucket is located. CN-Hong Kong is used here in this example. Replace it with the one currently in use. server: "https://obs.ap-southeast-1.myhuaweicloud.com" }); async function listVersions() { try { const params = { // Specify the bucket name. Bucket: "examplebucket", // Specify an encoding type. url is used in this example. If the objects to list contain special characters, this parameter must be passed. EncodingType: "url", }; while (true) { // List object versions in the bucket. const result = await obsClient.listVersions(params); if (result.CommonMsg.Status > 300) { console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response."); console.log("Status: %d", result.CommonMsg.Status); console.log("Code: %s", result.CommonMsg.Code); console.log("Message: %s", result.CommonMsg.Message); console.log("RequestId: %s", result.CommonMsg.RequestId); return; }; console.log("List version objects under the bucket(%s) successful!", params.Bucket); console.log("RequestId: %s", result.CommonMsg.RequestId); for (let j = 0; j < result.InterfaceResult.Contents.length; j++) { // Obtain object versions. for (let j = 0; j < result.InterfaceResult.Versions.length; j++) { const val = result.InterfaceResult.Versions[j]; console.log('Content[%d]-OwnerId:%s, ETag:%s, Key:%s, VersionId, LastModified:%s, Size:%d', j, val.Owner.ID, val.ETag, val.Key, val.VersionId, val.LastModified, val.Size); }; // Obtain delete markers. for (let i = 0; i < result.InterfaceResult.DeleteMarkers.length; i++) { const val = result.InterfaceResult.Versions[j]; console.log('Content[%d]-OwnerId:%s, Key:%s, VersionId, LastModified:%s', j, val.Owner.ID, val.Key, val.VersionId, val.LastModified); }; }; if (result.InterfaceResult.IsTruncated === "true") { params.KeyMarker = result.InterfaceResult.NextKeyMarker; params.VersionIdMarker = result.InterfaceResult.NextVersionIdMarker; } else { break; }; }; } catch (error) { console.log("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network."); console.log(error); }; }; listVersions(); |
Code Examples: Listing All Object Versions in a Folder
All elements stored in OBS buckets are called objects. Folders are objects whose sizes are 0 and whose names end with a slash (/). You can set a folder name as a prefix to list objects in this folder. This example lists object versions in a folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
// Import the OBS library. // Use npm to install the client. const ObsClient = require("esdk-obs-nodejs"); // Use the source code to install the client. // var ObsClient = require('./lib/obs'); // Create an instance of ObsClient. const obsClient = new ObsClient({ // Obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. Using hard coding may result in leakage. // 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. access_key_id: process.env.ACCESS_KEY_ID, secret_access_key: process.env.SECRET_ACCESS_KEY, // (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding, which may result in information leakage. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. // security_token: process.env.SECURITY_TOKEN, // Enter the endpoint corresponding to the region where the bucket is located. CN-Hong Kong is used here in this example. Replace it with the one currently in use. server: "https://obs.ap-southeast-1.myhuaweicloud.com" }); async function listVersions() { try { const params = { // Specify the bucket name. Bucket: "examplebucket", // Specify an object prefix (test/ in this example). Objects whose names contain the specified prefix will be listed. Prefix: "test/", // Specify an encoding type. url is used in this example. If the objects to list contain special characters, this parameter must be passed. EncodingType: "url", }; while (true) { // List object versions in the bucket. const result = await obsClient.listVersions(params); if (result.CommonMsg.Status > 300) { console.log("An ObsError was found, which means your request sent to OBS was rejected with an error response."); console.log("Status: %d", result.CommonMsg.Status); console.log("Code: %s", result.CommonMsg.Code); console.log("Message: %s", result.CommonMsg.Message); console.log("RequestId: %s", result.CommonMsg.RequestId); return; }; console.log("List version objects under the bucket(%s) successful!", params.Bucket); console.log("RequestId: %s", result.CommonMsg.RequestId); for (let j = 0; j < result.InterfaceResult.Contents.length; j++) { // Obtain object versions. for (let j = 0; j < result.InterfaceResult.Versions.length; j++) { const val = result.InterfaceResult.Versions[j]; console.log('Content[%d]-OwnerId:%s, ETag:%s, Key:%s, VersionId, LastModified:%s, Size:%d', j, val.Owner.ID, val.ETag, val.Key, val.VersionId, val.LastModified, val.Size); }; // Obtain delete markers. for (let i = 0; i < result.InterfaceResult.DeleteMarkers.length; i++) { const val = result.InterfaceResult.Versions[j]; console.log('Content[%d]-OwnerId:%s, Key:%s, VersionId, LastModified:%s', j, val.Owner.ID, val.Key, val.VersionId, val.LastModified); }; }; if (result.InterfaceResult.IsTruncated === "true") { params.KeyMarker = result.InterfaceResult.NextKeyMarker; params.VersionIdMarker = result.InterfaceResult.NextVersionIdMarker; } else { break; }; }; } catch (error) { console.log("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network."); console.log(error); }; }; listVersions(); |
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