If you call GetObject to download an object in a bucket with versioning enabled or suspended, only the current version of the object is returned by default.

When you perform the GetObject operation on an object in a bucket:
  • If the current version of the object is a delete marker, the 4040 Not Found error is returned.
  • If you specify the versionId of the target object in the request, the specified version of the object is returned. For example, if the versionId is specified as null in the request, the version of which the versionId is null is returned.
  • If the version specified in the request is a delete marker, the 405 Method Not Allowed error is returned.
You can run the following code to download 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>";
String bucketName = "<yourBucketName>";
String objectName = "<yourObjectName>";
String versionid = "<yourObjectVersionid>";

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

// Encapsulates the GetObject request.
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, objectName);
// Specifies the version ID of the object.
// ossObject includes the bucket name, object name, object metadata, and an input stream.
OSSObject ossObject = ossClient.getObject(getObjectRequest);

// Views the version ID of the downloaded object.
System.out.println("Get Object versionid:" +  ossObject.getObjectMetadata().getVersionId());
// Reads the object content.
System.out.println("Object content:");
BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent()));
while (true) {
    String line = reader.readLine();
    if (line == null) break;
    System.out.println("\n" + line);

// Closes the OSSClient instance.

For more information about downloading objects, see GetObject.