List files (Harmony SDK)
Updated at:
This topic describes how to list all objects in a bucket when versioning is enabled.
Notes
-
For more information about the regions and endpoints that OSS supports, see Regions and endpoints.
-
To list objects, you must have the
oss:ListObjectVersionspermission. For more information, see Grant custom access policies to RAM users.
Sample code
The following code shows how to list the versions of all objects, including delete markers, in a specified bucket.
import Client, { RequestError } from '@aliyun/oss';
// Create an OSS client instance.
const client = new Client({
// Replace with the Access Key ID of your STS temporary credentials.
accessKeyId: 'yourAccessKeyId',
// Replace with the Access Key Secret of your STS temporary credentials.
accessKeySecret: 'yourAccessKeySecret',
// Replace with the Security Token of your STS temporary credentials.
securityToken: 'yourSecurityToken',
// Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to oss-cn-hangzhou.
region: 'oss-cn-hangzhou',
});
// The bucket name.
const bucket = 'yourBucketName';
// The object name.
const key = 'yourObjectName';
// List the versions of objects in the bucket.
const listObjectVersions = async () => {
try {
// Call the listObjectVersions method to list the versions of objects in the bucket.
const res = await client.listObjectVersions({
bucket // Replace with your bucket name.
});
// Print the result, which contains the version information of the objects.
console.log(JSON.stringify(res));
} catch (err) {
// Catch and handle request errors.
if (err instanceof RequestError) {
console.log('Error code: ', err.code); // The error code.
console.log('Error message: ', err.message); // The error description.
console.log('Request ID: ', err.requestId); // The unique ID of the request.
console.log('HTTP status code: ', err.status); // The HTTP response status code.
console.log('Error category: ', err.ec); // The error category.
} else {
console.log('Unknown error: ', err); // An error that is not of the RequestError type.
}
}
};
// Call the function to list the versions of all objects.
listObjectVersions();
Scenarios
References
-
For more information about the API operation to list object versions, including delete markers, see ListObjectVersions (GetBucketVersions).
Is this page helpful?