In a bucket with versioning enabled or suspended, the HeadObject operation only obtains the metadata of the current version of an object.

Note In a bucket with versioning enabled, the HeadObject operation only obtains the metadata of the current version of an object.
You can run the following code to obtain the metadata of an object:
// This example uses the China East 1 (Hangzhou) endpoint. Specify the actual endpoint based on your requirements.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// It is highly risky to log on with the AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM user account, log on to https://ram.console.aliyun.com.
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";

// Creates an OSSClient instance.
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// Obtains a part of metadata of the specified version of the object.
GenericRequest getSimplifiedObjectMetaRequest = new GenericRequest("<yourBucketName>", "<yourObjectName>", "<yourVersionId>");
SimplifiedObjectMeta objectMeta = ossClient.getSimplifiedObjectMeta(getSimplifiedObjectMetaRequest);
System.out.println(objectMeta.getSize());
System.out.println(objectMeta.getETag());
System.out.println(objectMeta.getLastModified());

// Obtains all metadata of the specified version of the object.
GenericRequest getObjectMetadataRequest = new GenericRequest("<yourBucketName>", "<yourObjectName>", "<yourObjectVersionid>");
ObjectMetadata metadata = ossClient.getObjectMetadata(getObjectMetadataRequest);
System.out.println(metadata.getContentType());
System.out.println(metadata.getLastModified());
System.out.println(metadata.getExpirationTime());

// Closes the OSSClient instance.
ossClient.shutdown();

For more information about obtaining object metadata, see HeadObject and GetObjectMeta.