Search in sources :

Example 61 with BasicCOSCredentials

use of com.qcloud.cos.auth.BasicCOSCredentials 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 62 with BasicCOSCredentials

use of com.qcloud.cos.auth.BasicCOSCredentials in project cos-java-sdk-v5 by tencentyun.

the class TransferManagerTest method testTransferManagerCopySmallFileFromDiffRegion.

// transfer manager对不同园区5G以下文件进行使用put object copy
@Ignore
public void testTransferManagerCopySmallFileFromDiffRegion() throws CosServiceException, CosClientException, InterruptedException {
    if (!judgeUserInfoValid()) {
        return;
    }
    COSCredentials srcCred = new BasicCOSCredentials(secretId, secretKey);
    String srcRegion = "ap-guangzhou";
    ClientConfig srcClientConfig = new ClientConfig(new Region(srcRegion));
    COSClient srcCOSClient = new COSClient(srcCred, srcClientConfig);
    String srcBucketName = "chengwus3gz-1251668577";
    String srcKey = "ut_copy/len1G.txt";
    String destKey = "ut_copy_dest/len1G.txt";
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(new Region(srcRegion), srcBucketName, srcKey, bucket, destKey);
    Copy copy = transferManager.copy(copyObjectRequest, srcCOSClient, null);
    CopyResult copyResult = copy.waitForCopyResult();
    assertNotNull(copyResult.getRequestId());
    assertNotNull(copyResult.getDateStr());
    clearObject(destKey);
}
Also used : CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Copy(com.qcloud.cos.transfer.Copy) Region(com.qcloud.cos.region.Region) CopyResult(com.qcloud.cos.model.CopyResult) Ignore(org.junit.Ignore)

Example 63 with BasicCOSCredentials

use of com.qcloud.cos.auth.BasicCOSCredentials in project cos-java-sdk-v5 by tencentyun.

the class TransferManagerTest method testTransferManagerCopyBigFileFromSameRegion.

// transfer manager对相同园区使用put object copy
@Ignore
public void testTransferManagerCopyBigFileFromSameRegion() throws CosServiceException, CosClientException, InterruptedException {
    if (!judgeUserInfoValid()) {
        return;
    }
    COSCredentials srcCred = new BasicCOSCredentials(secretId, secretKey);
    String srcRegion = region;
    ClientConfig srcClientConfig = new ClientConfig(new Region(srcRegion));
    COSClient srcCOSClient = new COSClient(srcCred, srcClientConfig);
    String srcBucketName = bucket;
    String srcKey = "ut_copy/len10G_1.txt";
    String destKey = "ut_copy_dest/len10G_2.txt";
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(new Region(srcRegion), srcBucketName, srcKey, bucket, destKey);
    Copy copy = transferManager.copy(copyObjectRequest, srcCOSClient, null);
    CopyResult copyResult = copy.waitForCopyResult();
    assertNotNull(copyResult.getRequestId());
    assertNotNull(copyResult.getDateStr());
}
Also used : CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Copy(com.qcloud.cos.transfer.Copy) Region(com.qcloud.cos.region.Region) CopyResult(com.qcloud.cos.model.CopyResult) Ignore(org.junit.Ignore)

Example 64 with BasicCOSCredentials

use of com.qcloud.cos.auth.BasicCOSCredentials in project cos-java-sdk-v5 by tencentyun.

the class KMSEncryptionClientDemo method createCosClient.

static COSClient createCosClient(String region) {
    // 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
    // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region(region));
    // 为防止请求头部被篡改导致的数据无法解密,强烈建议只使用 https 协议发起请求
    clientConfig.setHttpProtocol(HttpProtocol.https);
    // 初始化 KMS 加密材料
    KMSEncryptionMaterials encryptionMaterials = new KMSEncryptionMaterials(cmk);
    // 使用AES/CTR模式,并将加密信息存储在文件元信息中.
    // 如果想要此次加密的对象被 COS 其他的 SDK 解密下载,则必须选择 AesCtrEncryption 模式
    CryptoConfiguration cryptoConf = new CryptoConfiguration(CryptoMode.AesCtrEncryption).withStorageMode(CryptoStorageMode.ObjectMetadata);
    // // 如果 kms 服务的 region 与 cos 的 region 不一致,则在加密信息里指定 kms 服务的 region
    // cryptoConf.setKmsRegion(kmsRegion);
    // // 如果需要可以为 KMS 服务的 cmk 设置对应的描述信息。
    // encryptionMaterials.addDescription("kms-region", "guangzhou");
    // 生成加密客户端EncryptionClient, COSEncryptionClient是COSClient的子类, 所有COSClient支持的接口他都支持。
    // EncryptionClient覆盖了COSClient上传下载逻辑,操作内部会执行加密操作,其他操作执行逻辑和COSClient一致
    COSEncryptionClient cosEncryptionClient = new COSEncryptionClient(new COSStaticCredentialsProvider(cred), new KMSEncryptionMaterialsProvider(encryptionMaterials), clientConfig, cryptoConf);
    return cosEncryptionClient;
}
Also used : COSStaticCredentialsProvider(com.qcloud.cos.auth.COSStaticCredentialsProvider) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) KMSEncryptionMaterialsProvider(com.qcloud.cos.internal.crypto.KMSEncryptionMaterialsProvider) CryptoConfiguration(com.qcloud.cos.internal.crypto.CryptoConfiguration) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) KMSEncryptionMaterials(com.qcloud.cos.internal.crypto.KMSEncryptionMaterials) COSEncryptionClient(com.qcloud.cos.COSEncryptionClient)

Example 65 with BasicCOSCredentials

use of com.qcloud.cos.auth.BasicCOSCredentials in project cos-java-sdk-v5 by tencentyun.

the class ListObjectsDemo method listObjectsDemo.

public static void listObjectsDemo() {
    // 1 初始化用户身份信息(appid, 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名称, 需包含appid
    String bucketName = "mybucket-1251668577";
    ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
    // 设置bucket名称
    listObjectsRequest.setBucketName(bucketName);
    // prefix表示列出的object的key以prefix开始
    listObjectsRequest.setPrefix("");
    // 设置最大遍历出多少个对象, 一次listobject最大支持1000
    listObjectsRequest.setMaxKeys(1000);
    // listObjectsRequest.setDelimiter("/");
    ObjectListing objectListing = null;
    try {
        objectListing = cosclient.listObjects(listObjectsRequest);
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
    // common prefix表示表示被delimiter截断的路径, 如delimter设置为/, common prefix则表示所有子目录的路径
    List<String> commonPrefixs = objectListing.getCommonPrefixes();
    // object summary表示所有列出的object列表
    List<COSObjectSummary> cosObjectSummaries = objectListing.getObjectSummaries();
    for (COSObjectSummary cosObjectSummary : cosObjectSummaries) {
        // 文件的路径key
        String key = cosObjectSummary.getKey();
        // 文件的etag
        String etag = cosObjectSummary.getETag();
        // 文件的长度
        long fileSize = cosObjectSummary.getSize();
        // 文件的存储类型
        String storageClasses = cosObjectSummary.getStorageClass();
        System.out.println("key: " + key);
    }
    cosclient.shutdown();
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) COSObjectSummary(com.qcloud.cos.model.COSObjectSummary) CosClientException(com.qcloud.cos.exception.CosClientException) ObjectListing(com.qcloud.cos.model.ObjectListing) COSClient(com.qcloud.cos.COSClient) ListObjectsRequest(com.qcloud.cos.model.ListObjectsRequest) CosServiceException(com.qcloud.cos.exception.CosServiceException) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Aggregations

BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)103 COSCredentials (com.qcloud.cos.auth.COSCredentials)103 Region (com.qcloud.cos.region.Region)99 ClientConfig (com.qcloud.cos.ClientConfig)94 COSClient (com.qcloud.cos.COSClient)91 CosClientException (com.qcloud.cos.exception.CosClientException)40 CosServiceException (com.qcloud.cos.exception.CosServiceException)38 File (java.io.File)21 TransferManager (com.qcloud.cos.transfer.TransferManager)13 ExecutorService (java.util.concurrent.ExecutorService)13 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)12 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)12 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)11 PutObjectResult (com.qcloud.cos.model.PutObjectResult)11 Copy (com.qcloud.cos.transfer.Copy)8 CopyResult (com.qcloud.cos.model.CopyResult)7 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)7 LinkedList (java.util.LinkedList)7 FileOperationException (run.halo.app.exception.FileOperationException)6 AnonymousCOSCredentials (com.qcloud.cos.auth.AnonymousCOSCredentials)5