Search in sources :

Example 11 with ClientConfig

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

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

the class GetObjectURLDemo method getObjectUrlWithEndpoint.

public static void getObjectUrlWithEndpoint() {
    // getObjectUrl 不需要验证身份信息
    COSCredentials cred = new AnonymousCOSCredentials();
    // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
    // 设置生成的 url 的协议名
    clientConfig.setHttpProtocol(HttpProtocol.https);
    // 设置自定义的域名
    UserSpecifiedEndpointBuilder endpointBuilder = new UserSpecifiedEndpointBuilder("test.endpoint.com", "service.cos.myqcloud.com");
    clientConfig.setEndpointBuilder(endpointBuilder);
    // 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    String key = "test/my_test中文.json";
    String bucketName = "mybucket-1251668577";
    String versionId = "xxxyyyzzz111222333";
    System.out.println(cosclient.getObjectUrl(bucketName, key, versionId));
}
Also used : COSClient(com.qcloud.cos.COSClient) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) COSCredentials(com.qcloud.cos.auth.COSCredentials) UserSpecifiedEndpointBuilder(com.qcloud.cos.endpoint.UserSpecifiedEndpointBuilder) Region(com.qcloud.cos.region.Region) AnonymousCOSCredentials(com.qcloud.cos.auth.AnonymousCOSCredentials) ClientConfig(com.qcloud.cos.ClientConfig)

Example 13 with ClientConfig

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

the class RestoreObjectDemo method restoreObjectDemo.

public static void restoreObjectDemo() {
    // 初始化用户身份信息(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_data.txt";
    String bucketName = "mybucket-1251668577";
    // 上传一个类型为归档的文件
    File localFile = new File("test/my_data.txt");
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setHeader("x-cos-storage-class", "Archive");
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    putObjectRequest.setMetadata(metadata);
    PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
    // 设置restore得到的临时副本过期天数为1天
    RestoreObjectRequest restoreObjectRequest = new RestoreObjectRequest(bucketName, key, 1);
    // 设置恢复模式为Standard,其他的可选模式包括Expedited和Bulk,三种恢复模式在费用和速度上不一样
    CASJobParameters casJobParameters = new CASJobParameters();
    casJobParameters.setTier(Tier.Standard);
    restoreObjectRequest.setCASJobParameters(casJobParameters);
    cosclient.restoreObject(restoreObjectRequest);
}
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) File(java.io.File)

Example 14 with ClientConfig

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

the class SSECustomerDemo method SSECustomerDownload.

public static void SSECustomerDownload() {
    // 1 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("COS_SECRETID", "COS_SECRETKEY");
    // 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
    clientConfig.setHttpProtocol(HttpProtocol.https);
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "examplebucket-1250000000";
    String key = "aaa/bbb.txt";
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
    SSECustomerKey sseCustomerKey = new SSECustomerKey("MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=");
    getObjectRequest.setSSECustomerKey(sseCustomerKey);
    try {
        COSObject cosObject = cosclient.getObject(getObjectRequest);
        COSObjectInputStream cosObjectInputStream = cosObject.getObjectContent();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(cosObjectInputStream));
        System.out.println(bufferedReader.readLine());
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // 关闭客户端
    cosclient.shutdown();
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) InputStreamReader(java.io.InputStreamReader) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosClientException(com.qcloud.cos.exception.CosClientException) IOException(java.io.IOException) COSClient(com.qcloud.cos.COSClient) CosServiceException(com.qcloud.cos.exception.CosServiceException) BufferedReader(java.io.BufferedReader) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 15 with ClientConfig

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

the class SSECustomerDemo method SSECustomerUpload.

public static void SSECustomerUpload() {
    // 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("COS_SECRETID", "COS_SECRETKEY");
    // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
    // 要求http协议
    clientConfig.setHttpProtocol(HttpProtocol.https);
    // 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "examplebucket-1250000000";
    String key = "aaa/bbb.txt";
    File localFile = new File("test.txt");
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    SSECustomerKey sseCustomerKey = new SSECustomerKey("MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=");
    putObjectRequest.setSSECustomerKey(sseCustomerKey);
    // 设置存储类型, 默认是标准(Standard), 低频(standard_ia)
    putObjectRequest.setStorageClass(StorageClass.Standard_IA);
    try {
        PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
        // putobjectResult会返回文件的etag, 该md5值根据s3语义不是对象的md5,只是唯一性标志
        String etag = putObjectResult.getETag();
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        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) File(java.io.File)

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