Search in sources :

Example 26 with GetObjectRequest

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

the class PutGetDelTest method testGetObjectIfMatchContainRightEtag.

@Ignore
public void testGetObjectIfMatchContainRightEtag() throws IOException {
    if (!judgeUserInfoValid()) {
        return;
    }
    File localFile = buildTestFile(1024);
    String key = "ut/" + localFile.getName();
    cosclient.putObject(bucket, key, localFile);
    try {
        String fileEtag = Md5Utils.md5Hex(localFile);
        String wrongEtag = fileEtag.substring(5) + fileEtag.substring(0, 5);
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
        List<String> eTagList = new ArrayList<>();
        eTagList.add("\"" + wrongEtag + "\"");
        eTagList.add("\"" + fileEtag + "\"");
        getObjectRequest.setMatchingETagConstraints(eTagList);
        COSObject cosObject = cosclient.getObject(getObjectRequest);
        assertNotNull(cosObject);
    } catch (CosServiceException cse) {
        fail(cse.toString());
    } finally {
        cosclient.deleteObject(bucket, key);
        assertTrue(localFile.delete());
    }
}
Also used : CosServiceException(com.qcloud.cos.exception.CosServiceException) COSObject(com.qcloud.cos.model.COSObject) ArrayList(java.util.ArrayList) File(java.io.File) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) Ignore(org.junit.Ignore)

Example 27 with GetObjectRequest

use of com.qcloud.cos.model.GetObjectRequest 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 28 with GetObjectRequest

use of com.qcloud.cos.model.GetObjectRequest 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 29 with GetObjectRequest

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

the class KMSEncryptionClientDemo method getObjectDemo.

static void getObjectDemo() {
    // 下载文件
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
    File downloadFile = new File("downlen1m.txt");
    ObjectMetadata objectMetadata = cosClient.getObject(getObjectRequest, downloadFile);
    System.out.println(objectMetadata.getRequestId());
}
Also used : GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 30 with GetObjectRequest

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

the class AsymmetricKeyEncryptionClientDemo method getObjectDemo.

static void getObjectDemo() {
    // 下载文件
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
    File downloadFile = new File("downAsy.txt");
    ObjectMetadata objectMetadata = cosClient.getObject(getObjectRequest, downloadFile);
    System.out.println(objectMetadata.getRequestId());
}
Also used : GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Aggregations

GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)36 File (java.io.File)26 CosServiceException (com.qcloud.cos.exception.CosServiceException)16 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)14 COSObject (com.qcloud.cos.model.COSObject)12 CosClientException (com.qcloud.cos.exception.CosClientException)10 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)10 Test (org.junit.Test)9 TransferManager (com.qcloud.cos.transfer.TransferManager)8 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)7 COSCredentials (com.qcloud.cos.auth.COSCredentials)7 Download (com.qcloud.cos.transfer.Download)7 COSClient (com.qcloud.cos.COSClient)6 ClientConfig (com.qcloud.cos.ClientConfig)6 Region (com.qcloud.cos.region.Region)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)5 MultipleFileDownload (com.qcloud.cos.transfer.MultipleFileDownload)5 ExecutorService (java.util.concurrent.ExecutorService)5