API | Operation | Description |
Setting a bucket policy | Sets a permission policy for a bucket | |
Querying a bucket policy | Queries the permission policy of a bucket | |
Deleting a bucket policy | Deletes the permission policy of a bucket |
// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.String bucket = "examplebucket-1250000000";// Permission policy. For more information, visit https://www.tencentcloud.com/document/product/436/12469?from_cn_redirect=1#.E7.AD.96.E7.95.A5.E8.AF.AD.E6.B3.95.String policy = "{\\n" +" \\"Statement\\": [\\n" +" {\\n" +" \\"Principal\\": {\\n" +" \\"qcs\\": [\\n" +" \\"qcs::cam::uin/100000000001:uin/100000000011\\"\\n" +" ]\\n" +" },\\n" +" \\"Effect\\": \\"allow\\",\\n" +" \\"Action\\": [\\n" +" \\"name/cos:GetBucket\\"\\n" +" ],\\n" +" \\"Resource\\": [\\n" +" \\"qcs::cos:ap-guangzhou:uid/1250000000:examplebucket-1250000000/*\\"\\n" +" ]\\n" +" }\\n" +" ],\\n" +" \\"version\\": \\"2.0\\"\\n" +"}";PutBucketPolicyRequest putBucketPolicyRequest =new PutBucketPolicyRequest(bucket, policy);cosXmlService.putBucketPolicyAsync(putBucketPolicyRequest,new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {// For detailed fields, see the API documentation or SDK source code.PutBucketPolicyResult putBucketPolicyResult =(PutBucketPolicyResult) result;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.String bucket = "examplebucket-1250000000";final GetBucketPolicyRequest getBucketPolicyRequest =new GetBucketPolicyRequest(bucket);cosXmlService.getBucketPolicyAsync(getBucketPolicyRequest,new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {// For detailed fields, see the API documentation or SDK source code.GetBucketPolicyResult getBucketPolicyResult =(GetBucketPolicyResult) result;String policy = getBucketPolicyResult.policy;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
// Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.tencentcloud.com/cos5/bucket.String bucket = "examplebucket-1250000000";DeleteBucketPolicyRequest deleteBucketPolicyRequest =new DeleteBucketPolicyRequest(bucket);cosXmlService.deleteBucketPolicyAsync(deleteBucketPolicyRequest,new CosXmlResultListener() {@Overridepublic void onSuccess(CosXmlRequest request, CosXmlResult result) {// For detailed fields, see the API documentation or SDK source code.DeleteBucketPolicyResult deleteBucketPolicyResult =(DeleteBucketPolicyResult) result;}// If you use the Kotlin language to call this, please note that the exception in the callback method is nullable; otherwise, the onFail method will not be called back, that is:// clientException is of type CosXmlClientException? and serviceException is of type CosXmlServiceException?@Overridepublic void onFail(CosXmlRequest cosXmlRequest,@Nullable CosXmlClientException clientException,@Nullable CosXmlServiceException serviceException) {if (clientException != null) {clientException.printStackTrace();} else {serviceException.printStackTrace();}}});
Apakah halaman ini membantu?