use of com.qcloud.cos.transfer.Upload in project cos-java-sdk-v5 by tencentyun.
the class QRCodeDemo method identifyQrCodeWithTransferManager.
public static void identifyQrCodeWithTransferManager(TransferManager transferManager) throws InterruptedException {
// bucket名需包含appid
// api 请参考 https://cloud.tencent.com/document/product/436/54070
String bucketName = "examplebucket-1250000000";
String key = "qrcode.png";
File localFile = new File("E://qrcode.png");
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
PicOperations picOperations = new PicOperations();
picOperations.setIsPicInfo(1);
List<PicOperations.Rule> ruleList = new LinkedList<>();
PicOperations.Rule rule1 = new PicOperations.Rule();
rule1.setBucket(bucketName);
rule1.setFileId("qrcode-1.png");
rule1.setRule("QRcode/cover/1");
ruleList.add(rule1);
picOperations.setRules(ruleList);
putObjectRequest.setPicOperations(picOperations);
Upload upload = transferManager.upload(putObjectRequest);
UploadResult uploadResult = upload.waitForUploadResult();
CIUploadResult ciUploadResult = uploadResult.getCiUploadResult();
System.out.println(uploadResult.getRequestId());
System.out.println(ciUploadResult.getOriginalInfo().getEtag());
for (CIObject ciObject : ciUploadResult.getProcessResults().getObjectList()) {
System.out.println(ciObject.getLocation());
}
}
use of com.qcloud.cos.transfer.Upload in project jeesuite-libs by vakinge.
the class QcloudProvider method upload.
@Override
public CUploadResult upload(CUploadObject object) {
PutObjectRequest request;
String fileKey = object.getFileKey();
String bucketName = buildBucketName(object.getBucketName());
if (object.getFile() != null) {
request = new PutObjectRequest(bucketName, fileKey, object.getFile());
} else if (object.getBytes() != null) {
ByteArrayInputStream inputStream = new ByteArrayInputStream(object.getBytes());
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(object.getFileSize());
request = new PutObjectRequest(bucketName, fileKey, inputStream, objectMetadata);
} else if (object.getInputStream() != null) {
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(object.getFileSize());
request = new PutObjectRequest(bucketName, fileKey, object.getInputStream(), objectMetadata);
} else {
throw new IllegalArgumentException("upload object is NULL");
}
try {
if (object.getFileSize() > conf.getMaxAllowdSingleFileSize()) {
Upload upload = transferManager.upload(request);
com.qcloud.cos.model.UploadResult result = upload.waitForUploadResult();
return new CUploadResult(fileKey, getFullPath(object.getBucketName(), fileKey), result.getCrc64Ecma());
} else {
PutObjectResult result = cosclient.putObject(request);
return new CUploadResult(fileKey, getFullPath(object.getBucketName(), fileKey), result.getContentMd5());
}
} catch (Exception e) {
e.printStackTrace();
throw new JeesuiteBaseException(500, buildMessage(bucketName, e));
}
}
use of com.qcloud.cos.transfer.Upload in project jeesuite-libs by vakinge.
the class QcloudProvider method upload.
@Override
public CUploadResult upload(CUploadObject object) {
PutObjectRequest request;
String fileKey = object.getFileKey();
String bucketName = buildBucketName(object.getBucketName());
if (object.getFile() != null) {
request = new PutObjectRequest(bucketName, fileKey, object.getFile());
} else if (object.getBytes() != null) {
ByteArrayInputStream inputStream = new ByteArrayInputStream(object.getBytes());
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(object.getFileSize());
request = new PutObjectRequest(bucketName, fileKey, inputStream, objectMetadata);
} else if (object.getInputStream() != null) {
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(object.getFileSize());
request = new PutObjectRequest(bucketName, fileKey, object.getInputStream(), objectMetadata);
} else {
throw new IllegalArgumentException("upload object is NULL");
}
try {
if (object.getFileSize() > conf.getMaxAllowdSingleFileSize()) {
Upload upload = transferManager.upload(request);
com.qcloud.cos.model.UploadResult result = upload.waitForUploadResult();
return new CUploadResult(fileKey, getFullPath(object.getBucketName(), fileKey), result.getCrc64Ecma());
} else {
PutObjectResult result = cosclient.putObject(request);
return new CUploadResult(fileKey, getFullPath(object.getBucketName(), fileKey), result.getContentMd5());
}
} catch (Exception e) {
e.printStackTrace();
throw new JeesuiteBaseException(500, buildMessage(bucketName, e));
}
}
use of com.qcloud.cos.transfer.Upload in project cos-java-sdk-v5 by tencentyun.
the class KMSEncryptionClientDemo method transferManagerDemo.
static void transferManagerDemo() {
ExecutorService threadPool = Executors.newFixedThreadPool(32);
// 传入一个threadpool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池。
TransferManager transferManager = new TransferManager(cosClient, threadPool);
TransferManagerConfiguration transferManagerConfiguration = new TransferManagerConfiguration();
transferManagerConfiguration.setMultipartUploadThreshold(1024 * 1024);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, bigLocalFile);
try {
// 返回一个异步结果Upload, 可同步的调用waitForUploadResult等待upload结束, 成功返回UploadResult, 失败抛出异常.
Upload upload = transferManager.upload(putObjectRequest);
UploadResult uploadResult = upload.waitForUploadResult();
System.out.println(uploadResult.getRequestId());
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
try {
// 返回一个异步结果Upload, 可同步的调用waitForUploadResult等待download结束, 成功返回DownloadResult, 失败抛出异常.
Download download = transferManager.download(getObjectRequest, new File("downLen10m.txt"));
download.waitForCompletion();
System.out.println(download.getObjectMetadata().getRequestId());
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
transferManager.shutdownNow();
}
use of com.qcloud.cos.transfer.Upload in project cos-java-sdk-v5 by tencentyun.
the class AbstractCOSEncryptionClientTest method testTransferManagerUploadDownBigFile.
@Test
public void testTransferManagerUploadDownBigFile() throws IOException, CosServiceException, CosClientException, InterruptedException {
if (!judgeUserInfoValid()) {
return;
}
TransferManager transferManager = new TransferManager(cosclient);
File localFile = buildTestFile(1024 * 1024 * 10L);
File downFile = new File(localFile.getAbsolutePath() + ".down");
String key = "ut/" + localFile.getName();
String destKey = key + ".copy";
try {
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, localFile);
ObjectMetadata objectMetadata = new ObjectMetadata();
putObjectRequest.setMetadata(objectMetadata);
Upload upload = transferManager.upload(putObjectRequest);
UploadResult uploadResult = upload.waitForUploadResult();
assertTrue(uploadResult.getETag().contains("-"));
assertNotNull(uploadResult.getRequestId());
assertNotNull(uploadResult.getDateStr());
GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
Download download = transferManager.download(getObjectRequest, downFile);
download.waitForCompletion();
// check file
assertEquals(Md5Utils.md5Hex(localFile), Md5Utils.md5Hex(downFile));
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, key, bucket, destKey);
Copy copy = transferManager.copy(copyObjectRequest, cosclient, null);
copy.waitForCompletion();
} finally {
// delete file on cos
clearObject(key);
// delete file on cos
clearObject(destKey);
if (localFile.exists()) {
assertTrue(localFile.delete());
}
if (downFile.exists()) {
assertTrue(downFile.delete());
}
}
}
Aggregations