By default, when you perform the GetObject operation on an object in a versioned bucket, only the current version of the object is returned.

Usage notes

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.


Use the following code to download an object:

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;

public class Demo {
    public static void main(String[] args) {
        // In this example, the endpoint of the China (Hangzhou) region is used. Specify the actual endpoint. For more information about the endpoints of other regions, see Regions and endpoints. 
        String endpoint = "";
        // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access Object Storage Service (OSS) because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
        String accessKeyId = "yourAccessKeyId";
        String accessKeySecret = "yourAccessKeySecret";
        // Specify the name of the bucket. Example: examplebucket. 
        String bucketName = "examplebucket";
        // Specify the full path of the object. Example: exampledir/exampleobject.txt. The full path of the object cannot contain the bucket name. 
        String objectName = "exampledir/exampleobject.txt";
        // Specify the version ID of the object. Example: CAEQARiBgID8rumR2hYiIGUyOTAyZGY2MzU5MjQ5ZjlhYzQzZjNlYTAyZDE3****. For more information about how to obtain the version ID of an object, see GetBucketVersions(ListObjectVersions). 
        String versionid = "CAEQARiBgID8rumR2hYiIGUyOTAyZGY2MzU5MjQ5ZjlhYzQzZjNlYTAyZDE3****";

        // Create an OSSClient instance. 
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            // Encapsulate the GetObject request. 
            GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, objectName);            
            // ossObject specifies the bucket name, object name, object metadata, and an input stream. 
            OSSObject ossObject = ossClient.getObject(getObjectRequest);

            // View the version ID of the downloaded object. 
            System.out.println("Get Object versionid:" +  ossObject.getObjectMetadata().getVersionId());
            // View the content of the object that has the specified version ID. 
            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);

        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (Throwable ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            // Shut down the OSSClient instance. 
            if (ossClient != null) {

For more information about the API operation that you can call to download an object, see GetObject.