Search in sources :

Example 81 with ClientConfig

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

Example 82 with ClientConfig

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

the class GetObjectMetadataDemo method getObjectMetadataDemo.

// 将本地文件上传到COS
public static void getObjectMetadataDemo() {
    // 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";
    String key = "aaa/bbb.txt";
    ObjectMetadata objectMetadata = cosclient.getObjectMetadata(bucketName, key);
    System.out.println(objectMetadata.getCrc64Ecma());
    System.out.println(objectMetadata.getLastModified());
    // 关闭客户端
    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) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 83 with ClientConfig

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

the class BucketLifecycleDemo method COSBuilder.

public COSClient COSBuilder() {
    // 初始化用户身份信息(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-shanghai"));
    // 生成cos客户端
    return new COSClient(cred, clientConfig);
}
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)

Example 84 with ClientConfig

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

the class BucketLoggingDemo method SetGetBucketLoggingDemo.

public static void SetGetBucketLoggingDemo() {
    // 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";
    BucketLoggingConfiguration bucketLoggingConfiguration = new BucketLoggingConfiguration();
    bucketLoggingConfiguration.setDestinationBucketName(bucketName);
    bucketLoggingConfiguration.setLogFilePrefix("logs");
    SetBucketLoggingConfigurationRequest setBucketLoggingConfigurationRequest = new SetBucketLoggingConfigurationRequest(bucketName, bucketLoggingConfiguration);
    cosclient.setBucketLoggingConfiguration(setBucketLoggingConfigurationRequest);
    BucketLoggingConfiguration bucketLoggingConfiguration1 = cosclient.getBucketLoggingConfiguration(bucketName);
}
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)

Example 85 with ClientConfig

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

the class MultipartUploadDemo method copyPartUploadDemo.

// 分块copy, 表示该块的数据来自另外一个文件的某一范围, 支持跨园区, 跨bucket copy
public static void copyPartUploadDemo(String uploadId) {
    // 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";
    String key = "aaa/bbb.txt";
    CopyPartRequest copyPartRequest = new CopyPartRequest();
    // 要拷贝的源文件所在的region
    copyPartRequest.setSourceBucketRegion(new Region("ap-guangzhou"));
    // 要拷贝的源文件的bucket名称
    copyPartRequest.setSourceBucketName(bucketName);
    // 要拷贝的源文件的路径
    copyPartRequest.setSourceKey("aaa/ccc.txt");
    // 指定要拷贝的源文件的数据范围(类似content-range)
    copyPartRequest.setFirstByte(0L);
    copyPartRequest.setLastByte(1048575L);
    // 目的bucket名称
    copyPartRequest.setDestinationBucketName(bucketName);
    // 目的路径名称
    copyPartRequest.setDestinationKey(key);
    copyPartRequest.setPartNumber(1);
    // uploadId
    copyPartRequest.setUploadId(uploadId);
    try {
        CopyPartResult copyPartResult = cosclient.copyPart(copyPartRequest);
        PartETag partETag = copyPartResult.getPartETag();
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
    cosclient.shutdown();
}
Also used : COSClient(com.qcloud.cos.COSClient) CopyPartRequest(com.qcloud.cos.model.CopyPartRequest) 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) CopyPartResult(com.qcloud.cos.model.CopyPartResult) PartETag(com.qcloud.cos.model.PartETag)

Aggregations

ClientConfig (com.qcloud.cos.ClientConfig)107 COSClient (com.qcloud.cos.COSClient)103 Region (com.qcloud.cos.region.Region)103 COSCredentials (com.qcloud.cos.auth.COSCredentials)99 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)96 CosClientException (com.qcloud.cos.exception.CosClientException)40 CosServiceException (com.qcloud.cos.exception.CosServiceException)38 File (java.io.File)22 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)14 PutObjectResult (com.qcloud.cos.model.PutObjectResult)14 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)13 TransferManager (com.qcloud.cos.transfer.TransferManager)13 ExecutorService (java.util.concurrent.ExecutorService)13 AnonymousCOSCredentials (com.qcloud.cos.auth.AnonymousCOSCredentials)7 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)7 LinkedList (java.util.LinkedList)7 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)6 FileOperationException (run.halo.app.exception.FileOperationException)6 IOException (java.io.IOException)5 CopyResult (com.qcloud.cos.model.CopyResult)4