Search in sources :

Example 71 with BasicCOSCredentials

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

the class BucketDemo method CreateBucketDemo.

// 创建bucket
public static void CreateBucketDemo() {
    // 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 = "publicreadbucket-1251668577";
    CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
    // 设置bucket的权限为PublicRead(公有读私有写), 其他可选有私有读写, 公有读私有写
    createBucketRequest.setCannedAcl(CannedAccessControlList.PublicRead);
    Bucket bucket = cosclient.createBucket(createBucketRequest);
    // 关闭客户端
    cosclient.shutdown();
}
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) Bucket(com.qcloud.cos.model.Bucket) CreateBucketRequest(com.qcloud.cos.model.CreateBucketRequest) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 72 with BasicCOSCredentials

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

the class BucketDomainDemo method SetGetDeleteBucketDomainDemo.

public static void SetGetDeleteBucketDomainDemo() {
    // 1 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
    // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-shanghai"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    BucketDomainConfiguration bucketDomainConfiguration = new BucketDomainConfiguration();
    DomainRule domainRule = new DomainRule();
    domainRule.setStatus(DomainRule.ENABLED);
    domainRule.setType(DomainRule.REST);
    domainRule.setName("test.com");
    // domainRule.setForcedReplacement(DomainRule.TXT);
    bucketDomainConfiguration.getDomainRules().add(domainRule);
    cosclient.setBucketDomainConfiguration(bucketName, bucketDomainConfiguration);
    BucketDomainConfiguration bucketDomainConfiguration1 = cosclient.getBucketDomainConfiguration(bucketName);
    System.out.println(bucketDomainConfiguration1.getDomainTxtVerification());
    for (DomainRule rule : bucketDomainConfiguration1.getDomainRules()) {
        System.out.println(rule.getName());
        System.out.println(rule.getStatus());
        System.out.println(rule.getType());
        System.out.println(rule.getClass());
    }
    cosclient.deleteBucketDomainConfiguration(bucketName);
    BucketDomainConfiguration bucketDomainConfiguration2 = cosclient.getBucketDomainConfiguration(bucketName);
    assert (bucketDomainConfiguration2 == null);
}
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) BucketDomainConfiguration(com.qcloud.cos.model.BucketDomainConfiguration) DomainRule(com.qcloud.cos.model.DomainRule)

Example 73 with BasicCOSCredentials

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

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

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

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