Search in sources :

Example 81 with COSCredentials

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

the class BucketIntelligentTierDemo method main.

public static void main(String[] args) {
    // 1 初始化用户身份信息(secretId, secretKey)
    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"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    BucketIntelligentTierConfiguration bucketIntelligentTierConfiguration = new BucketIntelligentTierConfiguration();
    bucketIntelligentTierConfiguration.setStatus(BucketIntelligentTierConfiguration.ENABLED);
    bucketIntelligentTierConfiguration.setTransition(new BucketIntelligentTierConfiguration.Transition(30));
    SetBucketIntelligentTierConfigurationRequest setBucketIntelligentTierConfigurationRequest = new SetBucketIntelligentTierConfigurationRequest();
    setBucketIntelligentTierConfigurationRequest.setBucketName(bucketName);
    setBucketIntelligentTierConfigurationRequest.setIntelligentTierConfiguration(bucketIntelligentTierConfiguration);
    cosclient.setBucketIntelligentTieringConfiguration(setBucketIntelligentTierConfigurationRequest);
    BucketIntelligentTierConfiguration bucketIntelligentTierConfiguration1 = cosclient.getBucketIntelligentTierConfiguration(bucketName);
    System.out.println(bucketIntelligentTierConfiguration1.getStatus());
    System.out.println(bucketIntelligentTierConfiguration1.getTransition().getDays());
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BucketIntelligentTierConfiguration(com.qcloud.cos.model.BucketIntelligentTierConfiguration) SetBucketIntelligentTierConfigurationRequest(com.qcloud.cos.model.SetBucketIntelligentTierConfigurationRequest) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 82 with COSCredentials

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

the class BucketInventoryDemo method SetGetDeleteBucketInventoryDemo.

public static void SetGetDeleteBucketInventoryDemo() {
    // 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-guangzhou"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    InventoryConfiguration inventoryConfiguration = new InventoryConfiguration();
    InventoryCosBucketDestination inventoryCosBucketDestination = new InventoryCosBucketDestination();
    // 设置清单的输出目标存储桶的格式和前缀等
    inventoryCosBucketDestination.setAccountId("2779643970");
    inventoryCosBucketDestination.setBucketArn("qcs::cos:ap-guangzhou::mybucket-1251668577");
    inventoryCosBucketDestination.setEncryption(new ServerSideEncryptionCOS());
    inventoryCosBucketDestination.setFormat(InventoryFormat.CSV);
    inventoryCosBucketDestination.setPrefix("inventory-output");
    InventoryDestination inventoryDestination = new InventoryDestination();
    inventoryDestination.setCosBucketDestination(inventoryCosBucketDestination);
    inventoryConfiguration.setDestination(inventoryDestination);
    // 设置清单的调度周期,扫描前缀和id等
    inventoryConfiguration.setEnabled(true);
    inventoryConfiguration.setId("1");
    InventorySchedule inventorySchedule = new InventorySchedule();
    inventorySchedule.setFrequency(InventoryFrequency.Daily);
    inventoryConfiguration.setSchedule(inventorySchedule);
    InventoryPrefixPredicate inventoryFilter = new InventoryPrefixPredicate("test/");
    inventoryConfiguration.setInventoryFilter(new InventoryFilter(inventoryFilter));
    inventoryConfiguration.setIncludedObjectVersions(InventoryIncludedObjectVersions.All);
    // 设置可选的输出字段
    List<String> optionalFields = new LinkedList<String>();
    optionalFields.add(InventoryOptionalField.Size.toString());
    optionalFields.add(InventoryOptionalField.LastModifiedDate.toString());
    inventoryConfiguration.setOptionalFields(optionalFields);
    SetBucketInventoryConfigurationRequest setBucketInventoryConfigurationRequest = new SetBucketInventoryConfigurationRequest();
    setBucketInventoryConfigurationRequest.setBucketName(bucketName);
    setBucketInventoryConfigurationRequest.setInventoryConfiguration(inventoryConfiguration);
    cosclient.setBucketInventoryConfiguration(setBucketInventoryConfigurationRequest);
    inventoryConfiguration.setId("2");
    inventorySchedule.setFrequency(InventoryFrequency.Weekly);
    cosclient.setBucketInventoryConfiguration(setBucketInventoryConfigurationRequest);
    // 获取指定id的清单配置
    GetBucketInventoryConfigurationResult getBucketInventoryConfigurationResult = cosclient.getBucketInventoryConfiguration(bucketName, "1");
    // 批量获取清单
    ListBucketInventoryConfigurationsRequest listBucketInventoryConfigurationsRequest = new ListBucketInventoryConfigurationsRequest();
    listBucketInventoryConfigurationsRequest.setBucketName(bucketName);
    ListBucketInventoryConfigurationsResult listBucketInventoryConfigurationsResult = cosclient.listBucketInventoryConfigurations(listBucketInventoryConfigurationsRequest);
    // 删除指定清单
    DeleteBucketInventoryConfigurationRequest deleteBucketInventoryConfigurationRequest = new DeleteBucketInventoryConfigurationRequest();
    deleteBucketInventoryConfigurationRequest.setBucketName(bucketName);
    deleteBucketInventoryConfigurationRequest.setId("1");
    cosclient.deleteBucketInventoryConfiguration(deleteBucketInventoryConfigurationRequest);
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) LinkedList(java.util.LinkedList) COSClient(com.qcloud.cos.COSClient) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 83 with COSCredentials

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

the class CopyFileDemo method copyBigFileDemo.

// 对于5G以上的文件,需要通过分块上传中的copypart来实现,步骤较多,实现较复杂。
// 因此在TransferManager中封装了一个copy接口,能根据文件大小自动的选择接口,既支持5G以下的文件copy, 也支持5G以上的文件copy。推荐使用该接口进行文件的copy。
public static void copyBigFileDemo() {
    // 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";
    // 生成用于获取源文件信息的srcCOSClient
    COSClient srcCOSClient = new COSClient(cred, new ClientConfig(srcBucketRegion));
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketRegion, srcBucketName, srcKey, destBucketName, destKey);
    try {
        Copy copy = transferManager.copy(copyObjectRequest, srcCOSClient, null);
        // 返回一个异步结果copy, 可同步的调用waitForCopyResult等待copy结束, 成功返回CopyResult, 失败抛出异常.
        CopyResult copyResult = copy.waitForCopyResult();
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    transferManager.shutdownNow();
    srcCOSClient.shutdown();
    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) COSClient(com.qcloud.cos.COSClient) CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) CosServiceException(com.qcloud.cos.exception.CosServiceException) Copy(com.qcloud.cos.transfer.Copy) ExecutorService(java.util.concurrent.ExecutorService) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) CopyResult(com.qcloud.cos.model.CopyResult)

Example 84 with COSCredentials

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

the class DelFileDemo method BatchDelFile.

// 批量删除文件(不带版本号, 即bucket未开启多版本)
public static void BatchDelFile() {
    // 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名需包含appid
    String bucketName = "mybucket-1251668577";
    DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName);
    // 设置要删除的key列表, 最多一次删除1000个
    ArrayList<KeyVersion> keyList = new ArrayList<>();
    // 传入要删除的文件名
    keyList.add(new KeyVersion("aaa.txt"));
    keyList.add(new KeyVersion("bbb.mp4"));
    keyList.add(new KeyVersion("ccc/ddd.jpg"));
    deleteObjectsRequest.setKeys(keyList);
    // 批量删除文件
    try {
        DeleteObjectsResult deleteObjectsResult = cosclient.deleteObjects(deleteObjectsRequest);
        List<DeletedObject> deleteObjectResultArray = deleteObjectsResult.getDeletedObjects();
    } catch (MultiObjectDeleteException mde) {
        // 如果部分产出成功部分失败, 返回MultiObjectDeleteException
        List<DeletedObject> deleteObjects = mde.getDeletedObjects();
        List<DeleteError> deleteErrors = mde.getErrors();
    } catch (CosServiceException e) {
        // 如果是其他错误, 比如参数错误, 身份验证不过等会抛出CosServiceException
        e.printStackTrace();
    } catch (CosClientException e) {
        // 如果是客户端错误,比如连接不上COS
        e.printStackTrace();
    }
    // 关闭客户端
    cosclient.shutdown();
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) KeyVersion(com.qcloud.cos.model.DeleteObjectsRequest.KeyVersion) CosClientException(com.qcloud.cos.exception.CosClientException) ArrayList(java.util.ArrayList) DeleteObjectsResult(com.qcloud.cos.model.DeleteObjectsResult) DeleteObjectsRequest(com.qcloud.cos.model.DeleteObjectsRequest) COSClient(com.qcloud.cos.COSClient) CosServiceException(com.qcloud.cos.exception.CosServiceException) MultiObjectDeleteException(com.qcloud.cos.exception.MultiObjectDeleteException) Region(com.qcloud.cos.region.Region) ArrayList(java.util.ArrayList) List(java.util.List) ClientConfig(com.qcloud.cos.ClientConfig) DeletedObject(com.qcloud.cos.model.DeleteObjectsResult.DeletedObject)

Example 85 with COSCredentials

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

the class DelFileDemo method DelSingleFile.

// 删除单个文件(不带版本号, 即bucket未开启多版本)
public static void DelSingleFile() {
    // 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名需包含appid
    String bucketName = "mybucket-1251668577";
    try {
        // 空key值
        String key = "";
        cosclient.deleteObject(bucketName, key);
    } catch (CosServiceException e) {
        // 如果是其他错误, 比如参数错误, 身份验证不过等会抛出CosServiceException
        e.printStackTrace();
    } catch (CosClientException e) {
        // 如果是客户端错误,比如连接不上COS
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // 该测试用例的预期结果
        e.printStackTrace();
    }
    try {
        String key = "aaa/bbb.txt";
        cosclient.deleteObject(bucketName, key);
    } catch (CosServiceException e) {
        // 如果是其他错误, 比如参数错误, 身份验证不过等会抛出CosServiceException
        e.printStackTrace();
    } catch (CosClientException e) {
        // 如果是客户端错误,比如连接不上COS
        e.printStackTrace();
    }
    // 关闭客户端
    cosclient.shutdown();
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosServiceException(com.qcloud.cos.exception.CosServiceException) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosClientException(com.qcloud.cos.exception.CosClientException) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Aggregations

COSCredentials (com.qcloud.cos.auth.COSCredentials)112 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)105 Region (com.qcloud.cos.region.Region)105 ClientConfig (com.qcloud.cos.ClientConfig)99 COSClient (com.qcloud.cos.COSClient)96 CosClientException (com.qcloud.cos.exception.CosClientException)42 CosServiceException (com.qcloud.cos.exception.CosServiceException)38 File (java.io.File)22 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 AnonymousCOSCredentials (com.qcloud.cos.auth.AnonymousCOSCredentials)10 Copy (com.qcloud.cos.transfer.Copy)8 CopyResult (com.qcloud.cos.model.CopyResult)7 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)7 Date (java.util.Date)7 LinkedList (java.util.LinkedList)7