Search in sources :

Example 31 with PutObjectResult

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);
    }
}
Also used : CosServiceException(com.qcloud.cos.exception.CosServiceException) PutObjectResult(com.qcloud.cos.model.PutObjectResult) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) ResponseNotCompleteException(com.qcloud.cos.exception.ResponseNotCompleteException) CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) IOException(java.io.IOException)

Example 32 with PutObjectResult

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());
        }
    }
}
Also used : CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) PutObjectResult(com.qcloud.cos.model.PutObjectResult) CopyObjectResult(com.qcloud.cos.model.CopyObjectResult) File(java.io.File)

Example 33 with PutObjectResult

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();
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) COSClient(com.qcloud.cos.COSClient) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 34 with PutObjectResult

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;
}
Also used : AccessControlList(com.qcloud.cos.model.AccessControlList) UinGrantee(com.qcloud.cos.model.UinGrantee) PutObjectResult(com.qcloud.cos.model.PutObjectResult) IOException(java.io.IOException) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 35 with 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();
    }
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) CosClientException(com.qcloud.cos.exception.CosClientException) PicOperations(com.qcloud.cos.model.ciModel.persistence.PicOperations) CIUploadResult(com.qcloud.cos.model.ciModel.persistence.CIUploadResult) LinkedList(java.util.LinkedList) CosServiceException(com.qcloud.cos.exception.CosServiceException) File(java.io.File) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) CIObject(com.qcloud.cos.model.ciModel.persistence.CIObject)

Aggregations

PutObjectResult (com.qcloud.cos.model.PutObjectResult)37 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)25 File (java.io.File)20 COSClient (com.qcloud.cos.COSClient)14 ClientConfig (com.qcloud.cos.ClientConfig)14 CosServiceException (com.qcloud.cos.exception.CosServiceException)14 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)14 Region (com.qcloud.cos.region.Region)14 CosClientException (com.qcloud.cos.exception.CosClientException)12 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)11 COSCredentials (com.qcloud.cos.auth.COSCredentials)11 IOException (java.io.IOException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 CIObject (com.qcloud.cos.model.ciModel.persistence.CIObject)4 CIUploadResult (com.qcloud.cos.model.ciModel.persistence.CIUploadResult)4 PicOperations (com.qcloud.cos.model.ciModel.persistence.PicOperations)4 InputStream (java.io.InputStream)4 LinkedList (java.util.LinkedList)4 Test (org.junit.Test)4 FileOperationException (run.halo.app.exception.FileOperationException)3