Search in sources :

Example 1 with BucketPolicy

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

the class BucketPolicyDemo method SetGetBucketPolicy.

public static void SetGetBucketPolicy() {
    // 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 = "examplebucket-1250000000";
    String bucketPolicyStr = "{" + "  \"Statement\": [" + "    {" + "      \"Principal\": {" + "        \"qcs\": [" + "          \"qcs::cam::uin/100000000001:uin/100000000011\"" + "        ]" + "      }," + "      \"Effect\": \"deny\"," + "      \"Action\": [" + "        \"name/cos:GetObject\"" + "      ]," + "      \"Resource\": [" + "        \"qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/test.txt\"" + "      ]" + "    }" + "  ]," + "  \"version\": \"2.0\"" + "}";
    cosclient.setBucketPolicy(bucketName, bucketPolicyStr);
    BucketPolicy bucketPolicy = cosclient.getBucketPolicy(bucketName);
    System.out.println(bucketPolicy.getPolicyText());
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BucketPolicy(com.qcloud.cos.model.BucketPolicy) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 2 with BucketPolicy

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

the class GetSetDelPolicyTest method setGetDelPolicyTest.

@Test
public void setGetDelPolicyTest() {
    try {
        String policyText = String.format("{\"Statement\": [  {   \"Action\": [    \"name/cos:*\"   ],   \"Condition\": {    \"ip_equal\": {     \"qcs:ip\": [      \"10.1.1.0/24\"     ]    },    \"string_equal\": {     \"qcs:sourceVpc\": [      \"vpc-123456\"     ]    }   },   \"Effect\": \"deny\",   \"Principal\": {    \"qcs\": [     \"qcs::cam::anyone:anyone\"    ]   },   \"Resource\": [    \"qcs::cos:%s:uid/%s:%s/*\"   ]  } ], \"version\": \"2.0\"}", region, appid, bucket);
        cosclient.setBucketPolicy(bucket, policyText);
        BucketPolicy bucketPolicy = cosclient.getBucketPolicy(bucket);
        assertNotNull(bucketPolicy.getPolicyText());
        assertFalse(bucketPolicy.getPolicyText().isEmpty());
        cosclient.deleteBucketPolicy(bucket);
    } catch (Exception e) {
        fail(e.toString());
    }
}
Also used : BucketPolicy(com.qcloud.cos.model.BucketPolicy) Test(org.junit.Test)

Aggregations

BucketPolicy (com.qcloud.cos.model.BucketPolicy)2 COSClient (com.qcloud.cos.COSClient)1 ClientConfig (com.qcloud.cos.ClientConfig)1 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)1 COSCredentials (com.qcloud.cos.auth.COSCredentials)1 Region (com.qcloud.cos.region.Region)1 Test (org.junit.Test)1