Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.qcloud.cos.demo.TransferManagerDemo Maven / Gradle / Ivy
package com.qcloud.cos .demo;
import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.qcloud.cos .COSClient;
import com.qcloud.cos .ClientConfig;
import com.qcloud.cos .auth.BasicCOSCredentials;
import com.qcloud.cos .auth.COSCredentials;
import com.qcloud.cos .exception.CosClientException;
import com.qcloud.cos .exception.CosServiceException;
import com.qcloud.cos .model.CopyObjectRequest;
import com.qcloud.cos .model.CopyResult;
import com.qcloud.cos .model.GetObjectRequest;
import com.qcloud.cos .model.PutObjectRequest;
import com.qcloud.cos .model.UploadResult;
import com.qcloud.cos .region.Region;
import com.qcloud.cos .transfer.Copy;
import com.qcloud.cos .transfer.Download;
import com.qcloud.cos .transfer.PersistableDownload;
import com.qcloud.cos .transfer.PersistableUpload;
import com.qcloud.cos .transfer.Transfer;
import com.qcloud.cos .transfer.TransferManager;
import com.qcloud.cos .transfer.TransferProgress;
import com.qcloud.cos .transfer.Upload;
import com.qcloud.cos .transfer.Transfer.TransferState;
public class TransferManagerDemo {
private static void showTransferProgress (Transfer transfer) {
System.out.println(transfer.getDescription());
do {
try {
Thread.sleep(2000 );
} catch (InterruptedException e) {
return ;
}
TransferProgress progress = transfer.getProgress();
long so_far = progress.getBytesTransferred();
long total = progress.getTotalBytesToTransfer();
double pct = progress.getPercentTransferred();
System.out.printf ("[%d / %d]\n" , so_far, total);
} while (transfer.isDone() == false );
System.out.println(transfer.getState());
}
public static void uploadFile () {
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX" , "1A2Z3YYYYYYYYYY" );
ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1" ));
COSClient cosclient = new COSClient(cred, clientConfig);
String bucketName = "mybucket-1251668577" ;
ExecutorService threadPool = Executors.newFixedThreadPool(32 );
TransferManager transferManager = new TransferManager(cosclient, threadPool);
String key = "aaa/bbb.txt" ;
File localFile = new File("src/test/resources/len30M.txt" );
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
try {
long startTime = System.currentTimeMillis();
Upload upload = transferManager.upload(putObjectRequest);
showTransferProgress(upload);
UploadResult uploadResult = upload.waitForUploadResult();
long endTime = System.currentTimeMillis();
System.out.println("used time: " + (endTime - startTime) / 1000 );
System.out.println(uploadResult.getETag());
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
transferManager.shutdownNow();
cosclient.shutdown();
}
public static void pauseUploadFileAndResume () {
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX" , "1A2Z3YYYYYYYYYY" );
ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1" ));
COSClient cosclient = new COSClient(cred, clientConfig);
String bucketName = "mybucket-1251668577" ;
ExecutorService threadPool = Executors.newFixedThreadPool(4 );
TransferManager transferManager = new TransferManager(cosclient, threadPool);
String key = "aaa/bbb.txt" ;
File localFile = new File("src/test/resources/len30M.txt" );
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
try {
Upload upload = transferManager.upload(putObjectRequest);
Thread.sleep(10000 );
PersistableUpload persistableUpload = upload.pause();
upload = transferManager.resumeUpload(persistableUpload);
showTransferProgress(upload);
UploadResult uploadResult = upload.waitForUploadResult();
System.out.println(uploadResult.getETag());
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
transferManager.shutdownNow();
cosclient.shutdown();
}
public static void downLoadFile () {
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX" , "1A2Z3YYYYYYYYYY" );
ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1" ));
COSClient cosclient = new COSClient(cred, clientConfig);
String bucketName = "mybucket-1251668577" ;
ExecutorService threadPool = Executors.newFixedThreadPool(32 );
TransferManager transferManager = new TransferManager(cosclient, threadPool);
String key = "aaa/bbb.txt" ;
File downloadFile = new File("src/test/resources/download.txt" );
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
try {
Download download = transferManager.download(getObjectRequest, downloadFile);
download.waitForCompletion();
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
transferManager.shutdownNow();
cosclient.shutdown();
}
public static void pauseDownloadFileAndResume () {
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX" , "1A2Z3YYYYYYYYYY" );
ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1" ));
COSClient cosclient = new COSClient(cred, clientConfig);
String bucketName = "mybucket-1251668577" ;
ExecutorService threadPool = Executors.newFixedThreadPool(32 );
TransferManager transferManager = new TransferManager(cosclient, threadPool);
String key = "aaa/bbb.txt" ;
File downloadFile = new File("src/test/resources/download.txt" );
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
try {
Download download = transferManager.download(getObjectRequest, downloadFile);
Thread.sleep(5000L );
PersistableDownload persistableDownload = download.pause();
download = transferManager.resumeDownload(persistableDownload);
showTransferProgress(download);
download.waitForCompletion();
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
transferManager.shutdownNow();
cosclient.shutdown();
}
public static void copyFileForDiffRegion () {
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX" , "1A2Z3YYYYYYYYYY" );
ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1" ));
COSClient cosclient = new COSClient(cred, clientConfig);
ExecutorService threadPool = Executors.newFixedThreadPool(32 );
TransferManager transferManager = new TransferManager(cosclient, threadPool);
Region srcBucketRegion = new Region("ap-shanghai" );
String srcBucketName = "srcBucket-1251668577" ;
String srcKey = "aaa/bbb.txt" ;
String destBucketName = "destBucket-1251668577" ;
String destKey = "ccc/ddd.txt" ;
COSClient srcCOSClient = new COSClient(cred, new ClientConfig(srcBucketRegion));
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketRegion, srcBucketName,
srcKey, destBucketName, destKey);
try {
Copy copy = transferManager.copy(copyObjectRequest, srcCOSClient, null);
CopyResult copyResult = copy.waitForCopyResult();
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
transferManager.shutdownNow();
srcCOSClient.shutdown();
cosclient.shutdown();
}
public static void copyFileForSameRegion () {
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX" , "1A2Z3YYYYYYYYYY" );
ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1" ));
COSClient cosclient = new COSClient(cred, clientConfig);
ExecutorService threadPool = Executors.newFixedThreadPool(32 );
TransferManager transferManager = new TransferManager(cosclient, threadPool);
Region srcBucketRegion = new Region("ap-beijing-1" );
String srcBucketName = "srcBucket-1251668577" ;
String srcKey = "aaa/bbb.txt" ;
String destBucketName = "destBucket-1251668577" ;
String destKey = "ccc/ddd.txt" ;
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketRegion, srcBucketName,
srcKey, destBucketName, destKey);
try {
Copy copy = transferManager.copy(copyObjectRequest);
CopyResult copyResult = copy.waitForCopyResult();
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
transferManager.shutdownNow();
cosclient.shutdown();
}
}