use of com.qcloud.cos.model.PutObjectResult in project hadoop-cos by tencentyun.
the class CosNativeFileSystemStore method storeFileWithRetry.
private void storeFileWithRetry(String key, InputStream inputStream, byte[] md5Hash, long length) throws IOException {
try {
ObjectMetadata objectMetadata = new ObjectMetadata();
if (null != md5Hash) {
objectMetadata.setContentMD5(Base64.encodeAsString(md5Hash));
}
objectMetadata.setContentLength(length);
if (crc32cEnabled) {
objectMetadata.setHeader(Constants.CRC32C_REQ_HEADER, Constants.CRC32C_REQ_HEADER_VAL);
}
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, inputStream, objectMetadata);
if (null != this.storageClass) {
putObjectRequest.setStorageClass(this.storageClass);
}
if (this.trafficLimit >= 0) {
putObjectRequest.setTrafficLimit(this.trafficLimit);
}
this.setEncryptionMetadata(putObjectRequest, objectMetadata);
PutObjectResult putObjectResult = (PutObjectResult) callCOSClientWithRetry(putObjectRequest);
LOG.debug("Store the file successfully. cos key: {}, ETag: {}.", key, putObjectResult.getETag());
} catch (CosServiceException cse) {
// 避免并发上传的问题
int statusCode = cse.getStatusCode();
if (statusCode == 409) {
// Check一下这个文件是否已经存在
FileMetadata fileMetadata = this.queryObjectMetadata(key);
if (null == fileMetadata) {
// 如果文件不存在,则需要抛出异常
handleException(cse, key);
}
LOG.warn("Upload the cos key [{}] concurrently", key);
} else {
// 其他错误都要抛出来
handleException(cse, key);
}
} catch (Exception e) {
String errMsg = String.format("Store the file failed, cos key: %s, exception: %s.", key, e);
handleException(new Exception(errMsg), key);
}
}
use of com.qcloud.cos.model.PutObjectResult in project cos-java-sdk-v5 by tencentyun.
the class PutObjectCopyTest method testCopySameRegionDiffSize.
private void testCopySameRegionDiffSize(long fileSize, ObjectMetadata newObjectMetaData) throws IOException {
if (!judgeUserInfoValid()) {
return;
}
File localFile = buildTestFile(fileSize);
String srcEtag = Md5Utils.md5Hex(localFile);
String srcKey = String.format("ut/src_len_%d.txt", fileSize);
String destKey = String.format("ut/dest_len_%d.txt", fileSize);
try {
PutObjectResult putObjectResult = putObjectFromLocalFile(localFile, srcKey);
CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, srcKey, bucket, destKey);
copyObjectRequest.setSourceVersionId(putObjectResult.getVersionId());
copyObjectRequest.setStorageClass(StorageClass.Standard_IA);
if (newObjectMetaData != null) {
copyObjectRequest.setNewObjectMetadata(newObjectMetaData);
}
CopyObjectResult copyObjectResult = cosclient.copyObject(copyObjectRequest);
assertNotNull(copyObjectResult.getRequestId());
assertNotNull(copyObjectResult.getDateStr());
// assertEquals(srcEtag, copyObjectResult.getETag());
headSimpleObject(srcKey, fileSize, srcEtag);
/*
ObjectMetadata destObjectMetadata = headSimpleObject(destKey, fileSize, srcEtag);
if (newObjectMetaData != null) {
checkMetaData(newObjectMetaData, destObjectMetadata);
}
*/
} finally {
// delete file on cos
clearObject(srcKey);
clearObject(destKey);
// delete local file
if (localFile.exists()) {
assertTrue(localFile.delete());
}
}
}
use of com.qcloud.cos.model.PutObjectResult 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.PutObjectResult 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.PutObjectResult in project cos-java-sdk-v5 by tencentyun.
the class BlindWatermarkDemo method addBlindWatermark.
public static void addBlindWatermark(COSClient cosClient) {
// bucket名需包含appid
// api 请参考:https://cloud.tencent.com/document/product/436/46782
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 rule = new PicOperations.Rule();
rule.setBucket(bucketName);
rule.setFileId("qrcode-watermark.png");
// 使用盲水印功能,水印图的宽高不得超过原图的1/8
rule.setRule("watermark/3/type/2/image/aHR0cDovL2V4YW1wbGVidWNrZXQtMTI1MDAwMDAwMC5jb3MuYXAtZ3Vhbmd6aG91Lm15cWNsb3VkLmNvbS9zaHVpeWluLnBuZw==");
ruleList.add(rule);
picOperations.setRules(ruleList);
putObjectRequest.setPicOperations(picOperations);
try {
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
CIUploadResult ciUploadResult = putObjectResult.getCiUploadResult();
System.out.println(putObjectResult.getRequestId());
System.out.println(ciUploadResult.getOriginalInfo().getEtag());
for (CIObject ciObject : ciUploadResult.getProcessResults().getObjectList()) {
System.out.println(ciObject.getLocation());
}
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
}
}
Aggregations