Search in sources :

Example 11 with BasicCOSCredentials

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

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

Example 13 with BasicCOSCredentials

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

the class SelectObjectContentDemo method selectJsonContentDemo.

public static void selectJsonContentDemo() throws Exception {
    // 初始化用户身份信息(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";
    String csvContent = "{\"name\":\"xiaoming\",\"mathScore\":89,\"musicScore\":92}\n" + "{\"name\":\"xiaowang\",\"mathScore\":93,\"musicScore\":85}\n" + "{\"name\":\"xiaoli\",\"mathScore\":82,\"musicScore\":95}\n";
    cosclient.putObject(bucketName, key, csvContent);
    String query = "select * from COSObject s where mathScore > 85'";
    SelectObjectContentRequest request = new SelectObjectContentRequest();
    request.setBucketName(bucketName);
    request.setKey(key);
    request.setExpression(query);
    request.setExpressionType(ExpressionType.SQL);
    InputSerialization inputSerialization = new InputSerialization();
    JSONInput jsonInput = new JSONInput();
    jsonInput.setType(JSONType.LINES);
    inputSerialization.setJson(jsonInput);
    inputSerialization.setCompressionType(CompressionType.NONE);
    request.setInputSerialization(inputSerialization);
    OutputSerialization outputSerialization = new OutputSerialization();
    outputSerialization.setJson(new JSONOutput());
    request.setOutputSerialization(outputSerialization);
    final AtomicBoolean isResultComplete = new AtomicBoolean(false);
    SelectObjectContentResult result = cosclient.selectObjectContent(request);
    InputStream resultInputStream = result.getPayload().getRecordsInputStream(new SelectObjectContentEventVisitor() {

        @Override
        public void visit(SelectObjectContentEvent.StatsEvent event) {
            System.out.println("Received Stats, Bytes Scanned: " + event.getDetails().getBytesScanned() + " Bytes Processed: " + event.getDetails().getBytesProcessed());
        }

        @Override
        public void visit(SelectObjectContentEvent.EndEvent event) {
            isResultComplete.set(true);
            System.out.println("Received End Event. Result is complete.");
        }
    });
    BufferedReader reader = new BufferedReader(new InputStreamReader(resultInputStream));
    StringBuffer stringBuffer = new StringBuffer();
    String line;
    while ((line = reader.readLine()) != null) {
        stringBuffer.append(line).append("\n");
    }
    System.out.println(stringBuffer.toString());
    // 检查结果是否接受完整
    if (!isResultComplete.get()) {
        throw new Exception("result was incomplete");
    }
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) COSClient(com.qcloud.cos.COSClient) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 14 with BasicCOSCredentials

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

the class SelectObjectContentDemo method selectCsvContentDemo.

public static void selectCsvContentDemo() throws Exception {
    // 初始化用户身份信息(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.csv";
    String bucketName = "mybucket-1251668577";
    String csvContent = "HuNan,ChangSha\nSiChuan,ChengDu\nGuiZhou,GuiYang\n";
    cosclient.putObject(bucketName, key, csvContent);
    String query = "select s._1 from COSObject s";
    SelectObjectContentRequest request = new SelectObjectContentRequest();
    request.setBucketName(bucketName);
    request.setKey(key);
    request.setExpression(query);
    request.setExpressionType(ExpressionType.SQL);
    InputSerialization inputSerialization = new InputSerialization();
    CSVInput csvInput = new CSVInput();
    csvInput.setFieldDelimiter(",");
    csvInput.setRecordDelimiter("\n");
    inputSerialization.setCsv(csvInput);
    inputSerialization.setCompressionType(CompressionType.NONE);
    request.setInputSerialization(inputSerialization);
    OutputSerialization outputSerialization = new OutputSerialization();
    outputSerialization.setCsv(new CSVOutput());
    request.setOutputSerialization(outputSerialization);
    final AtomicBoolean isResultComplete = new AtomicBoolean(false);
    SelectObjectContentResult result = cosclient.selectObjectContent(request);
    InputStream resultInputStream = result.getPayload().getRecordsInputStream(new SelectObjectContentEventVisitor() {

        @Override
        public void visit(SelectObjectContentEvent.StatsEvent event) {
            System.out.println("Received Stats, Bytes Scanned: " + event.getDetails().getBytesScanned() + " Bytes Processed: " + event.getDetails().getBytesProcessed());
        }

        @Override
        public void visit(SelectObjectContentEvent.EndEvent event) {
            isResultComplete.set(true);
            System.out.println("Received End Event. Result is complete.");
        }
    });
    BufferedReader reader = new BufferedReader(new InputStreamReader(resultInputStream));
    StringBuffer stringBuffer = new StringBuffer();
    String line;
    while ((line = reader.readLine()) != null) {
        stringBuffer.append(line).append("\n");
    }
    System.out.println(stringBuffer.toString());
    // 检查结果是否接受完整
    if (!isResultComplete.get()) {
        throw new Exception("result was incomplete");
    }
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) COSClient(com.qcloud.cos.COSClient) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 15 with BasicCOSCredentials

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

the class SetGetBucketAclDemo method setGetBucketAclDemo.

public static void setGetBucketAclDemo() {
    // 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-shanghai"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    // bucket名需包含appid
    String bucketName = "mybucket-1251668577";
    AccessControlList aclGet = cosclient.getBucketAcl(bucketName);
    System.out.println("bucket acl:" + aclGet.getOwner());
    System.out.println("bucket acl:" + aclGet.getCannedAccessControl());
    for (Grant grant : aclGet.getGrantsAsList()) {
        System.out.println(grant.getGrantee().getIdentifier());
        System.out.println(grant.getGrantee().getTypeIdentifier());
        System.out.println(grant.getPermission());
    }
    // public-read-write bucket acl
    // cosclient.setBucketAcl(bucketName, CannedAccessControlList.PublicReadWrite);
    // public-read bucket acl
    // cosclient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
    // private bucket acl
    AccessControlList aclSet = new AccessControlList();
    Owner owner = new Owner();
    owner.setId("qcs::cam::uin/2000000000:uin/2000000000");
    aclSet.setOwner(owner);
    UinGrantee uinGrantee = new UinGrantee("qcs::cam::uin/100000000000:uin/100000000000");
    aclSet.grantPermission(uinGrantee, Permission.Read);
    // 设置公有读
    aclSet.grantPermission(GroupGrantee.AllUsers, Permission.Read);
    cosclient.setBucketAcl(bucketName, aclSet);
    cosclient.setBucketAcl(bucketName, CannedAccessControlList.Private);
    System.out.println("===========================");
    aclGet = cosclient.getBucketAcl(bucketName);
    System.out.println("bucket acl:" + aclGet.getOwner());
    System.out.println("bucket acl:" + aclGet.getCannedAccessControl());
    for (Grant grant : aclGet.getGrantsAsList()) {
        System.out.println(grant.getGrantee().getIdentifier());
        System.out.println(grant.getGrantee().getTypeIdentifier());
        System.out.println(grant.getPermission());
    }
}
Also used : COSClient(com.qcloud.cos.COSClient) CannedAccessControlList(com.qcloud.cos.model.CannedAccessControlList) AccessControlList(com.qcloud.cos.model.AccessControlList) UinGrantee(com.qcloud.cos.model.UinGrantee) Grant(com.qcloud.cos.model.Grant) Owner(com.qcloud.cos.model.Owner) 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)

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