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());
}
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());
}
}
Aggregations