Search in sources :

Example 1 with GetObjectRequest

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

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

the class GetObjectDemo method GetObjectToFileDemo.

public static void GetObjectToFileDemo() {
    // 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
    // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
    // 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    String key = "test/my_test.json";
    String bucketName = "mybucket-1251668577";
    boolean useTrafficLimit = false;
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
    if (useTrafficLimit) {
        getObjectRequest.setTrafficLimit(8 * 1024 * 1024);
    }
    File localFile = new File("my_test.json");
    ObjectMetadata objectMetadata = cosclient.getObject(getObjectRequest, localFile);
    System.out.println(objectMetadata.getContentLength());
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) 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 GetObjectRequest

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

the class PutObjectDemo method putObjectDemo.

static void putObjectDemo() {
    String bucketName = "examplebucket-1251668577";
    String key = "abc/abc.txt";
    String localPath = "abc.txt";
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setHeader("expires", new Date(1660000000000L));
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, new File(localPath));
    putObjectRequest.withMetadata(objectMetadata);
    PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
    System.out.println(putObjectResult.getRequestId());
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
    COSObject cosObject = cosClient.getObject(getObjectRequest);
    System.out.println(cosObject.getObjectMetadata().getRequestId());
    cosClient.shutdown();
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) COSObject(com.qcloud.cos.model.COSObject) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) File(java.io.File) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) Date(java.util.Date) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 4 with GetObjectRequest

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

the class PutGetDelTest method testGetObjectIfMatchWrongEtag.

@Test
public void testGetObjectIfMatchWrongEtag() 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);
        // 打乱一下,得到一个错误的etag
        String wrongEtag = fileEtag.substring(5) + fileEtag.substring(0, 5);
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
        List<String> eTagList = new ArrayList<>();
        eTagList.add(wrongEtag);
        getObjectRequest.setMatchingETagConstraints(eTagList);
        COSObject cosObject = cosclient.getObject(getObjectRequest);
        assertNull(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) Test(org.junit.Test)

Example 5 with GetObjectRequest

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

the class PutGetDelTest method testRequestSpecifiedKeyInfoPutGetDel.

@Test
public void testRequestSpecifiedKeyInfoPutGetDel() throws CosServiceException, IOException, InterruptedException {
    COSClient cosclient = new COSClient(new AnonymousCOSCredentials(), clientConfig);
    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) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) File(java.io.File) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) Test(org.junit.Test)

Aggregations

GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)32 File (java.io.File)26 CosServiceException (com.qcloud.cos.exception.CosServiceException)12 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)11 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)9 Test (org.junit.Test)9 COSObject (com.qcloud.cos.model.COSObject)8 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 CosClientException (com.qcloud.cos.exception.CosClientException)6 Region (com.qcloud.cos.region.Region)6 ArrayList (java.util.ArrayList)6 MultipleFileDownload (com.qcloud.cos.transfer.MultipleFileDownload)5 ExecutorService (java.util.concurrent.ExecutorService)5 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)4 UploadResult (com.qcloud.cos.model.UploadResult)4