Search in sources :

Example 36 with PutObjectRequest

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();
    }
}
Also used : DeleteObjectRequest(com.qcloud.cos.model.DeleteObjectRequest) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) File(java.io.File) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) Test(org.junit.Test)

Example 37 with PutObjectRequest

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();
}
Also used : TransferManager(com.qcloud.cos.transfer.TransferManager) TransferManagerConfiguration(com.qcloud.cos.transfer.TransferManagerConfiguration) CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) ExecutorService(java.util.concurrent.ExecutorService) Upload(com.qcloud.cos.transfer.Upload) UploadResult(com.qcloud.cos.model.UploadResult) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) Download(com.qcloud.cos.transfer.Download) File(java.io.File) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 38 with PutObjectRequest

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();
}
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 39 with PutObjectRequest

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;
}
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 40 with PutObjectRequest

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());
        }
    }
}
Also used : TransferManager(com.qcloud.cos.transfer.TransferManager) CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) Copy(com.qcloud.cos.transfer.Copy) 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) Download(com.qcloud.cos.transfer.Download) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) Test(org.junit.Test)

Aggregations

PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)45 File (java.io.File)30 PutObjectResult (com.qcloud.cos.model.PutObjectResult)25 CosServiceException (com.qcloud.cos.exception.CosServiceException)17 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)17 CosClientException (com.qcloud.cos.exception.CosClientException)15 COSClient (com.qcloud.cos.COSClient)13 ClientConfig (com.qcloud.cos.ClientConfig)13 Region (com.qcloud.cos.region.Region)13 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)12 COSCredentials (com.qcloud.cos.auth.COSCredentials)12 Upload (com.qcloud.cos.transfer.Upload)12 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)10 UploadResult (com.qcloud.cos.model.UploadResult)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8 LinkedList (java.util.LinkedList)8 Test (org.junit.Test)8 CIObject (com.qcloud.cos.model.ciModel.persistence.CIObject)7 CIUploadResult (com.qcloud.cos.model.ciModel.persistence.CIUploadResult)7 PicOperations (com.qcloud.cos.model.ciModel.persistence.PicOperations)7