import com.volcengine.tos.TOSV2;
import com.volcengine.tos.TOSV2ClientBuilder;
import com.volcengine.tos.TosClientException;
import com.volcengine.tos.TosServerException;
import com.volcengine.tos.comm.event.DownloadEventType;
import com.volcengine.tos.model.object.*;
public class DownloadFileWithEventListenerExample {
public static void main(String[] args) {
String endpoint = "your endpoint";
String region = "your region";
String accessKey = System.getenv("TOS_ACCESS_KEY");
String secretKey = System.getenv("TOS_SECRET_KEY");
String bucketName = "bucket-example";
String objectKey = "example_dir/example_object.txt";
String downloadFilePath = "example_dir/example_file.txt";
long partSize = 10 * 1024 * 1024;
boolean enableCheckpoint = true;
String checkpointFilePath = "the checkpoint file path";
TOSV2 tos = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);
HeadObjectV2Input head = new HeadObjectV2Input().setBucket(bucketName).setKey(objectKey);
DownloadEventListener listener = new DownloadEventListener() {
public void eventChange(DownloadEvent downloadEvent) {
if (downloadEvent.getDownloadEventType() == DownloadEventType.DownloadEventCreateTempFileSucceed) {
System.out.println("event change, createTempFile succeed");
if (downloadEvent.getDownloadEventType() == DownloadEventType.DownloadEventCreateTempFileFailed) {
System.out.println("event change, createTempFile failed");
if (downloadEvent.getDownloadEventType() == DownloadEventType.DownloadEventDownloadPartSucceed) {
System.out.println("event change, downloadPart succeed");
if (downloadEvent.getDownloadEventType() == DownloadEventType.DownloadEventDownloadPartFailed) {
System.out.println("event change, downloadPart failed");
if (downloadEvent.getDownloadEventType() == DownloadEventType.DownloadEventDownloadPartAborted) {
System.out.println("event change, downloadPart aborted");
if (downloadEvent.getDownloadEventType() == DownloadEventType.DownloadEventRenameTempFileSucceed) {
System.out.println("event change, renameTempFile succeed");
if (downloadEvent.getDownloadEventType() == DownloadEventType.DownloadEventRenameTempFileFailed) {
System.out.println("event change, renameTempFile failed");
DownloadFileInput input = new DownloadFileInput().setHeadObjectV2Input(head)
.setFilePath(downloadFilePath).setEnableCheckpoint(enableCheckpoint)
.setCheckpointFile(checkpointFilePath).setPartSize(partSize).setTaskNum(taskNum)
.setDownloadEventListener(listener);
DownloadFileOutput output = tos.downloadFile(input);
System.out.println("downloadFile succeed, object's etag is " + output.getEtag());
System.out.println("downloadFile succeed, object's crc64 is " + output.getHashCrc64ecma());
} catch (TosClientException e) {
System.out.println("downloadFile failed");
System.out.println("Message: " + e.getMessage());
if (e.getCause() != null) {
e.getCause().printStackTrace();
} catch (TosServerException e) {
System.out.println("downloadFile failed");
System.out.println("StatusCode: " + e.getStatusCode());
System.out.println("Code: " + e.getCode());
System.out.println("Message: " + e.getMessage());
System.out.println("RequestID: " + e.getRequestID());
System.out.println("downloadFile failed");
System.out.println("unexpected exception, message: " + t.getMessage());