Search in sources :

Example 16 with COSCredentials

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

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

Example 18 with COSCredentials

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

the class AsymmetricKeyEncryptionClientDemo method createCosClient.

static COSClient createCosClient(String region) {
    // 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxx", "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy");
    // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region(region));
    // 为防止请求头部被篡改导致的数据无法解密,强烈建议只使用 https 协议发起请求
    clientConfig.setHttpProtocol(HttpProtocol.https);
    KeyPair asymKeyPair = null;
    try {
        // 加载保存在文件中的秘钥, 如果不存在,请先使用buildAndSaveAsymKeyPair生成秘钥
        // buildAndSaveAsymKeyPair();
        asymKeyPair = loadAsymKeyPair();
    } catch (Exception e) {
        throw new CosClientException(e);
    }
    // 初始化 KMS 加密材料
    EncryptionMaterials encryptionMaterials = new EncryptionMaterials(asymKeyPair);
    // 使用AES/GCM模式,并将加密信息存储在文件元信息中.
    CryptoConfiguration cryptoConf = new CryptoConfiguration(CryptoMode.AesCtrEncryption).withStorageMode(CryptoStorageMode.ObjectMetadata);
    // // 如果 kms 服务的 region 与 cos 的 region 不一致,则在加密信息里指定 kms 服务的 region
    // cryptoConf.setKmsRegion(kmsRegion);
    // // 如果需要可以为 KMS 服务的 cmk 设置对应的描述信息。
    // encryptionMaterials.addDescription("kms-region", "guangzhou");
    // 生成加密客户端EncryptionClient, COSEncryptionClient是COSClient的子类, 所有COSClient支持的接口他都支持。
    // EncryptionClient覆盖了COSClient上传下载逻辑,操作内部会执行加密操作,其他操作执行逻辑和COSClient一致
    COSEncryptionClient cosEncryptionClient = new COSEncryptionClient(new COSStaticCredentialsProvider(cred), new StaticEncryptionMaterialsProvider(encryptionMaterials), clientConfig, cryptoConf);
    return cosEncryptionClient;
}
Also used : COSStaticCredentialsProvider(com.qcloud.cos.auth.COSStaticCredentialsProvider) KeyPair(java.security.KeyPair) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) EncryptionMaterials(com.qcloud.cos.internal.crypto.EncryptionMaterials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosClientException(com.qcloud.cos.exception.CosClientException) CryptoConfiguration(com.qcloud.cos.internal.crypto.CryptoConfiguration) Region(com.qcloud.cos.region.Region) StaticEncryptionMaterialsProvider(com.qcloud.cos.internal.crypto.StaticEncryptionMaterialsProvider) ClientConfig(com.qcloud.cos.ClientConfig) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) CosClientException(com.qcloud.cos.exception.CosClientException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) COSEncryptionClient(com.qcloud.cos.COSEncryptionClient)

Example 19 with COSCredentials

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

the class BucketDemo method SetGetDeleteBucketTagging.

// 使用 bucket tag
public static void SetGetDeleteBucketTagging() {
    // 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";
    List<TagSet> tagSetList = new LinkedList<TagSet>();
    TagSet tagSet = new TagSet();
    tagSet.setTag("age", "18");
    tagSet.setTag("name", "xiaoming");
    tagSetList.add(tagSet);
    BucketTaggingConfiguration bucketTaggingConfiguration = new BucketTaggingConfiguration();
    bucketTaggingConfiguration.setTagSets(tagSetList);
    SetBucketTaggingConfigurationRequest setBucketTaggingConfigurationRequest = new SetBucketTaggingConfigurationRequest(bucketName, bucketTaggingConfiguration);
    cosclient.setBucketTaggingConfiguration(setBucketTaggingConfigurationRequest);
    cosclient.getBucketTaggingConfiguration(bucketName);
    cosclient.deleteBucketTaggingConfiguration(bucketName);
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BucketTaggingConfiguration(com.qcloud.cos.model.BucketTaggingConfiguration) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) TagSet(com.qcloud.cos.model.TagSet) Region(com.qcloud.cos.region.Region) SetBucketTaggingConfigurationRequest(com.qcloud.cos.model.SetBucketTaggingConfigurationRequest) ClientConfig(com.qcloud.cos.ClientConfig) LinkedList(java.util.LinkedList)

Example 20 with COSCredentials

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

the class BucketDemo method SetBucketLogging.

// 开启日志存储
public static void SetBucketLogging() {
    // 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 = "examplebucket-1251668577";
    BucketLoggingConfiguration bucketLoggingConfiguration = new BucketLoggingConfiguration();
    // 设置日志存储的 bucket
    bucketLoggingConfiguration.setDestinationBucketName(bucketName);
    // 设置日志存储的前缀
    bucketLoggingConfiguration.setLogFilePrefix("logs/");
    SetBucketLoggingConfigurationRequest setBucketLoggingConfigurationRequest = new SetBucketLoggingConfigurationRequest(bucketName, bucketLoggingConfiguration);
    cosclient.setBucketLoggingConfiguration(setBucketLoggingConfigurationRequest);
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BucketLoggingConfiguration(com.qcloud.cos.model.BucketLoggingConfiguration) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Region(com.qcloud.cos.region.Region) SetBucketLoggingConfigurationRequest(com.qcloud.cos.model.SetBucketLoggingConfigurationRequest) 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