Downloading large files can fail due to an unstable network or program exceptions. Sometimes, the download fails even after multiple retries. To solve this issue, Object Storage Service (OSS) offers a resumable download feature. The resumable download feature splits a file into parts, downloads each part separately, and then combines them into a complete file.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see OSS regions and endpoints.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Configuration examples for common scenarios.
To perform a resumable download, you must have the
oss:GetObjectpermission. For more information, see Attach a custom policy to a RAM user.
Sample code
The following code provides an example of how to perform a resumable download.
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;
public class Demo {
public static void main(String[] args) throws Exception {
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Specify the bucket name. Example: examplebucket.
String bucketName = "examplebucket";
// Specify the full path of the object. Example: exampledir/exampleobject.txt. The full path cannot contain the bucket name.
String objectName = "exampledir/exampleobject.txt";
// Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.
String region = "cn-hangzhou";
// Create an OSSClient instance.
// When the OSSClient instance is no longer needed, call the shutdown method to release its resources.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// Download the file using 10 concurrent tasks.
DownloadFileRequest downloadFileRequest = new DownloadFileRequest(bucketName, objectName);
// Specify the full path of the local file to which you want to download the object. Example: D:\\localpath\\examplefile.txt.
downloadFileRequest.setDownloadFile("D:\\localpath\\examplefile.txt");
// Set the part size in bytes. The value must be in the range of 100 KB to 5 GB. The default value is 100 KB.
downloadFileRequest.setPartSize(1 * 1024 * 1024);
// Set the number of concurrent parts to download. The default value is 1.
downloadFileRequest.setTaskNum(10);
// Enable resumable download. This feature is disabled by default.
downloadFileRequest.setEnableCheckpoint(true);
// Specify the full path of the checkpoint file. Example: D:\\localpath\\examplefile.txt.dcp.
// A checkpoint file is generated when a download is interrupted. To resume the download, you must specify the full path of the checkpoint file. The checkpoint file is deleted after the download is complete.
//downloadFileRequest.setCheckpointFile("D:\\localpath\\examplefile.txt.dcp");
// Download the file.
DownloadFileResult downloadRes = ossClient.downloadFile(downloadFileRequest);
// If the download is successful, the object metadata is returned.
ObjectMetadata objectMetadata = downloadRes.getObjectMetadata();
System.out.println(objectMetadata.getETag());
System.out.println(objectMetadata.getLastModified());
System.out.println(objectMetadata.getUserMetadata().get("meta"));
} 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 {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}Related documents
For the complete sample code for resumable download, see the GitHub example.
For more information about the API operation for resumable download, see GetObject.