tencent cloud

Feedback

Access Control

Last updated: 2022-01-24 12:37:09

    Overview

    This document provides an overview of APIs and SDK code samples related to the access control lists (ACLs) for buckets and objects.

    Bucket ACL

    API Operation Description
    PUT Bucket acl Setting a bucket ACL Sets an ACL for a bucket
    GET Bucket acl Querying a bucket ACL Queries the ACL of a bucket

    Object ACL

    API Operation Description
    PUT Object acl Setting an object ACL Sets an ACL for an object in a bucket
    GET Object acl Querying an object ACL Queries the ACL of an object

    SDK API References

    For the parameters and method descriptions of all the APIs in the SDK, see SDK API Reference.

    Bucket ACL

    Setting a bucket ACL

    Description

    This API is used to set an access control list (ACL) for a specified bucket.

    Sample code

    // 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";
    PutBucketACLRequest putBucketACLRequest = new PutBucketACLRequest(bucket);
    // Set the bucket's access permissions
    putBucketACLRequest.setXCOSACL("public-read");
    // Grant read permission.
    ACLAccount readACLS = new ACLAccount();
    readACLS.addAccount("100000000001", "100000000001");
    putBucketACLRequest.setXCOSGrantRead(readACLS);
    // Grant write permission.
    ACLAccount writeACLS = new ACLAccount();
    writeACLS.addAccount("100000000001", "100000000001");
    putBucketACLRequest.setXCOSGrantWrite(writeACLS);
    // Grant read and write permission
    ACLAccount writeandReadACLS = new ACLAccount();
    writeandReadACLS.addAccount("100000000001", "100000000001");
    putBucketACLRequest.setXCOSReadWrite(writeandReadACLS);
    cosXmlService.putBucketACLAsync(putBucketACLRequest,
           new CosXmlResultListener() {
       @Override
       public void onSuccess(CosXmlRequest request, CosXmlResult result) {
           PutBucketACLResult putBucketACLResult = (PutBucketACLResult) 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?
       @Override
       public void onFail(CosXmlRequest cosXmlRequest,
                          @Nullable CosXmlClientException clientException,
                          @Nullable CosXmlServiceException serviceException) {
           if (clientException != null) {
               clientException.printStackTrace();
           } else {
               serviceException.printStackTrace();
           }
       }
    });
    
    Note:

    For more samples, please visit GitHub.

    Querying a bucket ACL

    Description

    This API is used to query the access control list (ACL) of a specified bucket.

    Sample code

    // 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";
    GetBucketACLRequest getBucketACLRequest = new GetBucketACLRequest(bucket);
    cosXmlService.getBucketACLAsync(getBucketACLRequest,
           new CosXmlResultListener() {
       @Override
       public void onSuccess(CosXmlRequest request, CosXmlResult result) {
           GetBucketACLResult getBucketACLResult = (GetBucketACLResult) 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?
       @Override
       public void onFail(CosXmlRequest cosXmlRequest,
                          @Nullable CosXmlClientException clientException,
                          @Nullable CosXmlServiceException serviceException) {
           if (clientException != null) {
               clientException.printStackTrace();
           } else {
               serviceException.printStackTrace();
           }
       }
    });
    
    Note:

    For more samples, please visit GitHub.

    Object ACL

    Setting an object ACL

    Description

    This API is used to set the access control list (ACL) for an object in a bucket.

    Sample code

    // 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";
    String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object key
    PutObjectACLRequest putObjectACLRequest = new PutObjectACLRequest(bucket,
           cosPath);
    // Set the object's access permissions
    putObjectACLRequest.setXCOSACL("public-read");
    // Grant read permission.
    ACLAccount readACLS = new ACLAccount();
    readACLS.addAccount("100000000001", "100000000001");
    putObjectACLRequest.setXCOSGrantRead(readACLS);
    // Grant read and write permission
    ACLAccount writeandReadACLS = new ACLAccount();
    writeandReadACLS.addAccount("100000000001", "100000000001");
    putObjectACLRequest.setXCOSReadWrite(writeandReadACLS);
    cosXmlService.putObjectACLAsync(putObjectACLRequest,
           new CosXmlResultListener() {
       @Override
       public void onSuccess(CosXmlRequest request, CosXmlResult result) {
           PutObjectACLResult putObjectACLResult = (PutObjectACLResult) 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?
       @Override
       public void onFail(CosXmlRequest cosXmlRequest,
                          @Nullable CosXmlClientException clientException,
                          @Nullable CosXmlServiceException serviceException) {
           if (clientException != null) {
               clientException.printStackTrace();
           } else {
               serviceException.printStackTrace();
           }
       }
    });
    
    Note:

    For the complete sample, go to GitHub.

    Querying an object ACL

    Description

    This API is used to query the ACL of an object.

    Sample code

    // 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";
    String cosPath = "exampleobject"; // The location identifier of the object in the bucket, i.e., the object key
    GetObjectACLRequest getBucketACLRequest = new GetObjectACLRequest(bucket,
           cosPath);
    cosXmlService.getObjectACLAsync(getBucketACLRequest,
           new CosXmlResultListener() {
       @Override
       public void onSuccess(CosXmlRequest request, CosXmlResult result) {
           GetObjectACLResult getObjectACLResult = (GetObjectACLResult) 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?
       @Override
       public void onFail(CosXmlRequest cosXmlRequest,
                          @Nullable CosXmlClientException clientException,
                          @Nullable CosXmlServiceException serviceException) {
           if (clientException != null) {
               clientException.printStackTrace();
           } else {
               serviceException.printStackTrace();
           }
       }
    });
    
    Note:

    For the complete sample, go to GitHub.

    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support