use of com.qcloud.cos.model.PutObjectRequest in project cos-java-sdk-v5 by tencentyun.
the class PutGetDelTest method testRequestSpecifiedTmpKeyInfoPutGetDel.
@Test
public void testRequestSpecifiedTmpKeyInfoPutGetDel() throws CosServiceException, IOException, InterruptedException {
COSClient cosclient = buildTemporyCredentialsCOSClient(1800L);
File localFile = buildTestFile(1024L);
COSCredentials cosCredentials = new BasicCOSCredentials(secretId, secretKey);
try {
String key = "ut/request-specified-key";
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, localFile);
putObjectRequest.setCosCredentials(cosCredentials);
cosclient.putObject(putObjectRequest);
GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
getObjectRequest.setCosCredentials(cosCredentials);
cosclient.getObject(getObjectRequest);
DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucket, key);
deleteObjectRequest.setCosCredentials(cosCredentials);
cosclient.deleteObject(deleteObjectRequest);
} finally {
localFile.delete();
cosclient.shutdown();
}
}
use of com.qcloud.cos.model.PutObjectRequest 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.model.PutObjectRequest in project cos-java-sdk-v5 by tencentyun.
the class CAMRoleDemo method SimpleUploadFileFromEMR.
public static void SimpleUploadFileFromEMR() {
InstanceMetadataCredentialsEndpointProvider endpointProvider = new InstanceMetadataCredentialsEndpointProvider(InstanceMetadataCredentialsEndpointProvider.Instance.EMR);
InstanceCredentialsFetcher instanceCredentialsFetcher = new InstanceCredentialsFetcher(endpointProvider);
COSCredentialsProvider cosCredentialsProvider = new InstanceCredentialsProvider(instanceCredentialsFetcher);
COSCredentials cred = cosCredentialsProvider.getCredentials();
System.out.println(cred.getCOSAccessKeyId());
System.out.println(cred.getCOSSecretKey());
System.out.println(cred.getCOSAppId());
ClientConfig clientConfig = new ClientConfig(new Region("ap-chongqing"));
COSClient cosClient = new COSClient(cosCredentialsProvider, clientConfig);
String bucketName = "aaa-125xxx";
String key = "test_emr.txt";
File localFile = new File("./test");
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
putObjectRequest.setStorageClass(StorageClass.Standard);
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
System.out.println("upload file etag: " + putObjectResult.getETag());
System.out.println("upload file requestId: " + putObjectResult.getRequestId());
ObjectMetadata getMeta = cosClient.getObjectMetadata(bucketName, key);
System.out.println("get file etag: " + getMeta.getETag());
cosClient.deleteObject(bucketName, key);
if (cosClient.doesObjectExist(bucketName, key)) {
System.out.println("delete failed");
} else {
System.out.println("delete successfully");
}
cosClient.shutdown();
}
use of com.qcloud.cos.model.PutObjectRequest in project cos-java-sdk-v5 by tencentyun.
the class AbstractCOSClientTest method putObjectFromLocalFile.
// 从本地上传
protected static PutObjectResult putObjectFromLocalFile(File localFile, String key, SSECustomerKey sseCKey, SSECOSKeyManagementParams params) {
if (!judgeUserInfoValid()) {
return null;
}
AccessControlList acl = new AccessControlList();
UinGrantee uinGrantee = new UinGrantee("734000014");
acl.grantPermission(uinGrantee, Permission.Read);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, localFile);
putObjectRequest.setStorageClass(StorageClass.Standard_IA);
if (sseCKey != null) {
putObjectRequest.setSSECustomerKey(sseCKey);
}
if (params != null) {
putObjectRequest.setSSECOSKeyManagementParams(params);
}
// putObjectRequest.setCannedAcl(CannedAccessControlList.PublicRead);
// putObjectRequest.setAccessControlList(acl);
PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
assertNotNull(putObjectResult.getRequestId());
assertNotNull(putObjectResult.getDateStr());
String etag = putObjectResult.getETag();
String expectEtag = null;
try {
expectEtag = Md5Utils.md5Hex(localFile);
} catch (IOException e) {
fail(e.toString());
}
if (useClientEncryption) {
assertEquals(false, expectEtag.equals(etag));
} else {
assertEquals(true, expectEtag.equals(etag));
}
return putObjectResult;
}
use of com.qcloud.cos.model.PutObjectRequest 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