Search in sources :

Example 1 with CopyObjectRequest

use of com.qcloud.cos.model.CopyObjectRequest in project cos-java-sdk-v5 by tencentyun.

the class CopyFileDemo method copySmallFileDemo.

// copyObject最大支持5G文件的copy, 5G以上的文件copy请参照TransferManagerDemo中的copy示例
public static void copySmallFileDemo() {
    // 1 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
    // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // 要拷贝的bucket region, 支持跨园区拷贝
    Region srcBucketRegion = new Region("ap-shanghai");
    // 源bucket, bucket名需包含appid
    String srcBucketName = "srcBucket-1251668577";
    // 要拷贝的源文件
    String srcKey = "aaa/bbb.txt";
    // 目的bucket, bucket名需包含appid
    String destBucketName = "destBucket-1251668577";
    // 要拷贝的目的文件
    String destKey = "ccc/ddd.txt";
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketRegion, srcBucketName, srcKey, destBucketName, destKey);
    try {
        CopyObjectResult copyObjectResult = cosclient.copyObject(copyObjectRequest);
        String crc64 = copyObjectResult.getCrc64Ecma();
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
    cosclient.shutdown();
}
Also used : COSClient(com.qcloud.cos.COSClient) CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosServiceException(com.qcloud.cos.exception.CosServiceException) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CopyObjectResult(com.qcloud.cos.model.CopyObjectResult) CosClientException(com.qcloud.cos.exception.CosClientException) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 2 with CopyObjectRequest

use of com.qcloud.cos.model.CopyObjectRequest in project cos-java-sdk-v5 by tencentyun.

the class CopyFileDemo method copyWithNewMetaDataDemo.

public static void copyWithNewMetaDataDemo() {
    // 1 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
    // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    ExecutorService threadPool = Executors.newFixedThreadPool(32);
    // 传入一个threadpool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池。
    TransferManager transferManager = new TransferManager(cosclient, threadPool);
    // 要拷贝的bucket region, 支持跨园区拷贝
    Region srcBucketRegion = new Region("ap-shanghai");
    // 源bucket, bucket名需包含appid
    String srcBucketName = "srcBucket-1251668577";
    // 要拷贝的源文件
    String srcKey = "aaa/bbb.txt";
    // 目的bucket, bucket名需包含appid
    String destBucketName = "destBucket-1251668577";
    // 要拷贝的目的文件
    String destKey = "ccc/ddd.txt";
    ClientConfig srcClientConfig = new ClientConfig(srcBucketRegion);
    COSClient srcCosclient = new COSClient(cred, srcClientConfig);
    GetObjectRequest getReq = new GetObjectRequest(srcBucketName, srcKey);
    File srcFile = new File("srcFile");
    srcCosclient.getObject(getReq, srcFile);
    String srcMD5 = "";
    try {
        srcMD5 = Md5Utils.md5Hex(srcFile);
    } catch (Exception e) {
        e.printStackTrace();
    }
    ObjectMetadata destMeta = new ObjectMetadata();
    destMeta.addUserMetadata("md5", srcMD5);
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketRegion, srcBucketName, srcKey, destBucketName, destKey);
    copyObjectRequest.setNewObjectMetadata(destMeta);
    try {
        CopyObjectResult copyObjectResult = cosclient.copyObject(copyObjectRequest);
        System.out.print(copyObjectResult.getRequestId());
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
    cosclient.shutdown();
}
Also used : TransferManager(com.qcloud.cos.transfer.TransferManager) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosClientException(com.qcloud.cos.exception.CosClientException) CosClientException(com.qcloud.cos.exception.CosClientException) CosServiceException(com.qcloud.cos.exception.CosServiceException) COSClient(com.qcloud.cos.COSClient) CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) CosServiceException(com.qcloud.cos.exception.CosServiceException) CopyObjectResult(com.qcloud.cos.model.CopyObjectResult) ExecutorService(java.util.concurrent.ExecutorService) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 3 with CopyObjectRequest

use of com.qcloud.cos.model.CopyObjectRequest in project cos-java-sdk-v5 by tencentyun.

the class KmsUploadDemo method CopyObjectWithKmsMeta.

public static void CopyObjectWithKmsMeta() {
    COSCredentials cred = new BasicCOSCredentials("SECRET_ID", "SECRET_KEY");
    // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
    // 设置使用https请求
    clientConfig.setHttpProtocol(HttpProtocol.https);
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String kmsKeyId = "your-kms-key-id";
    String encryptionContext = Base64.encodeAsString("{\"Ssekmstest\":\"Ssekmstest\"}".getBytes());
    // 要拷贝的bucket region, 支持跨园区拷贝
    Region srcBucketRegion = new Region("ap-guangzhou");
    // 源bucket, bucket名需包含appid
    String srcBucketName = "mybucket-1251668577";
    // 要拷贝的源文件
    String srcKey = "aaa/bbb.txt";
    // 目的bucket, bucket名需包含appid
    String destBucketName = "mybucket-1251668577";
    // 要拷贝的目的文件
    String destKey = "ccc/ddd.txt";
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketRegion, srcBucketName, srcKey, destBucketName, destKey);
    copyObjectRequest.setSSECOSKeyManagementParams(new SSECOSKeyManagementParams(kmsKeyId, encryptionContext));
    try {
        CopyObjectResult copyObjectResult = cosclient.copyObject(copyObjectRequest);
        String crc64 = copyObjectResult.getCrc64Ecma();
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
    // 关闭客户端
    cosclient.shutdown();
}
Also used : COSClient(com.qcloud.cos.COSClient) CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosServiceException(com.qcloud.cos.exception.CosServiceException) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CopyObjectResult(com.qcloud.cos.model.CopyObjectResult) CosClientException(com.qcloud.cos.exception.CosClientException) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) SSECOSKeyManagementParams(com.qcloud.cos.model.SSECOSKeyManagementParams)

Example 4 with CopyObjectRequest

use of com.qcloud.cos.model.CopyObjectRequest in project cos-java-sdk-v5 by tencentyun.

the class TransferManagerTest 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();
        objectMetadata.setServerSideEncryption("AES256");
        putObjectRequest.setMetadata(objectMetadata);
        Upload upload = transferManager.upload(putObjectRequest);
        UploadResult uploadResult = upload.waitForUploadResult();
        assertTrue(uploadResult.getETag().contains("-"));
        assertTrue(upload.isResumeableMultipartUploadAfterFailed());
        assertNotNull(upload.getResumeableMultipartUploadId());
        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());
        }
    }
}
Also used : TransferManager(com.qcloud.cos.transfer.TransferManager) CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) Copy(com.qcloud.cos.transfer.Copy) MultipleFileUpload(com.qcloud.cos.transfer.MultipleFileUpload) Upload(com.qcloud.cos.transfer.Upload) UploadResult(com.qcloud.cos.model.UploadResult) File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) MultipleFileDownload(com.qcloud.cos.transfer.MultipleFileDownload) Download(com.qcloud.cos.transfer.Download) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) Test(org.junit.Test)

Example 5 with CopyObjectRequest

use of com.qcloud.cos.model.CopyObjectRequest in project cos-java-sdk-v5 by tencentyun.

the class TransferManagerTest method testTransferManagerUploadDownCopySmallFile.

@Test
public void testTransferManagerUploadDownCopySmallFile() throws IOException, CosServiceException, CosClientException, InterruptedException {
    if (!judgeUserInfoValid()) {
        return;
    }
    TransferManager transferManager = new TransferManager(cosclient);
    File localFile = buildTestFile(1024 * 1024 * 2L);
    File downFile = new File(localFile.getAbsolutePath() + ".down");
    String key = "ut/" + localFile.getName();
    String destKey = key + ".copy";
    try {
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, localFile);
        Upload upload = transferManager.upload(putObjectRequest);
        UploadResult uploadResult = upload.waitForUploadResult();
        // head object
        headSimpleObject(key, localFile.length(), Md5Utils.md5Hex(localFile));
        assertEquals(Md5Utils.md5Hex(localFile), uploadResult.getETag());
        assertFalse(upload.isResumeableMultipartUploadAfterFailed());
        assertNull(upload.getResumeableMultipartUploadId());
        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 {
        // clear object
        clearObject(key);
        // clear dest object
        clearObject(destKey);
        // delete smallfile
        if (localFile.exists()) {
            assertTrue(localFile.delete());
        }
        if (downFile.exists()) {
            assertTrue(downFile.delete());
        }
    }
}
Also used : TransferManager(com.qcloud.cos.transfer.TransferManager) CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) Copy(com.qcloud.cos.transfer.Copy) MultipleFileUpload(com.qcloud.cos.transfer.MultipleFileUpload) Upload(com.qcloud.cos.transfer.Upload) UploadResult(com.qcloud.cos.model.UploadResult) File(java.io.File) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) MultipleFileDownload(com.qcloud.cos.transfer.MultipleFileDownload) Download(com.qcloud.cos.transfer.Download) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) Test(org.junit.Test)

Aggregations

CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)15 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)11 COSCredentials (com.qcloud.cos.auth.COSCredentials)11 Region (com.qcloud.cos.region.Region)11 Copy (com.qcloud.cos.transfer.Copy)11 TransferManager (com.qcloud.cos.transfer.TransferManager)8 COSClient (com.qcloud.cos.COSClient)7 ClientConfig (com.qcloud.cos.ClientConfig)7 CosClientException (com.qcloud.cos.exception.CosClientException)7 CosServiceException (com.qcloud.cos.exception.CosServiceException)7 CopyResult (com.qcloud.cos.model.CopyResult)7 File (java.io.File)5 ExecutorService (java.util.concurrent.ExecutorService)5 CopyObjectResult (com.qcloud.cos.model.CopyObjectResult)4 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)4 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)4 Ignore (org.junit.Ignore)4 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)3 UploadResult (com.qcloud.cos.model.UploadResult)3 Download (com.qcloud.cos.transfer.Download)3