Actions on Buckets

Last updated: 2020-02-28 18:30:23

PDF

Note

This document provides an overview of APIs and SDK sample codes related to the basic operations and the access control list (ACL) for a bucket.

Basic Operations

API Operation name pedagogical operation
GET Service Query Bucket list Query the list of all Bucket under the specified account
PUT Bucket Create Bucket Create a Bucket under the specified account
HEAD Bucket Search Bucket and his Permission Search whether Bucket exists and whether there is Permission and Access
DELETE Bucket Deleting Buckets Delete the empty Bucket under the specified account

Access control list

API Operation name pedagogical operation
PUT Bucket acl Set Bucket ACL Set up the specified Bucket Access Permission control list
GET Bucket acl Query Bucket ACL Query Bucket's Access control list

Basic Operations

Query Bucket list

Function description

Used to query the list of all Bucket under the specified account.

Method prototype

CosResult GetService(const GetServiceReq& request, GetServiceResp* response)

Request Sample

qcloud_cos::CosConfig config("./config.json");
qcloud_cos::CosAPI cos(config);

std::string bucket_name = "examplebucket-1250000000";

qcloud_cos::GetServiceReq req;
qcloud_cos::GetServiceResp resp;
qcloud_cos::CosResult result = cos.GetService(req, &resp);

// The call is successful. You can call the member function of resp to get the return content
if (result.IsSucc()) {
    const qcloud_cos::Owner& owner = resp.GetOwner();
    const std::vector<qcloud_cos::Bucket>& buckets = resp.GetBuckets();
    std::cout << "owner.m_id=" << owner.m_id << ", owner.display_name=" << owner.m_display_name << std::endl;               
     for (std::vector<qcloud_cos::Bucket>::const_iterator itr = buckets.begin(); itr != buckets.end(); ++itr) {
         const qcloud_cos::Bucket& bucket = *itr;
         std::cout << "Bucket name=" << bucket.m_name << ", location=" 
         << bucket.m_location << ", create_date=" << bucket.m_create_date << std::endl;                                  
     } 
} else {
    // You can call the member function of CosResult to output the error information such as requestID
} 

Parameter description

Parameters Parameter description
Req Request for GetServiceReq,GetService operation
Resp Return of GetServiceResp,GetService operation

GetServiceResp provides the following member functions to get the specific content in the XML format returned by GetService.

// Get bucket owner information
Owner GetOwner() const;
// Get list information of all buckets
std::vector<Bucket> GetBuckets() const

Owner is defined as follows:

struct Owner {
    std::string m_id;    // Bucket owner ID
    std::string m_display_name; // Bucket owner name
}; 

Bucket is defined as follows:

// Describe the information of a single bucket                                                                                                                                                                
struct Bucket {
    std::string m_name; // Bucket name
    std::string m_location; // Bucket region
    std::string m_create_date; // Bucket creation time in ISO8601 format, such as 2016-11-09T08:46:32.000Z
};

Create Bucket

Function description

This API (Put Bucket) is used to create a bucket.

Method prototype

CosResult PutBucket(const PutBucketReq& req, PutBucketResp* resp)

Request Sample

qcloud_cos::CosConfig config("./config.json");
qcloud_cos::CosAPI cos(config);

std::string bucket_name = "examplebucket-1250000000";

qcloud_cos::PutBucketReq req(bucket_name);
qcloud_cos::PutBucketResp resp;
qcloud_cos::CosResult result = cos.PutBucket(req, &resp);

// The call is successful. You can call the member function of resp to get the return content
if (result.IsSucc()) {
    // ...
} else {
    // You can call the member function of CosResult to output the error information such as requestID
} 

Parameter description

Parameters Parameter description
Req Request for PutBucketReq,PutBucket operation
Resp Return of PutBucketResp,PutBucket operation

PutBucketReq provides the following member functions:

// Define the ACL attribute of the bucket. Value range: private, public-read-write, public-read
// Default value: private
void SetXCosAcl(const std::string& str);

// Grant the grantee read permission in the format of x-cos-grant-read: id=" ",id=" ".
// When you need to authorize a sub-account, id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>"
// When you need to authorize a root account, id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>"
void SetXCosGrantRead(const std::string& str);

// Grant the grantee write permission in the format of x-cos-grant-write: id=" ",id=" "./
// When you need to authorize a sub-account, id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>"
// When you need to authorize a root account, id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>"
void SetXCosGrantWrite(const std::string& str);

// Grant the grantee read-write permission in the format of x-cos-grant-full-control: id=" ",id=" ".
// When you need to authorize a sub-account, id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>"
// When you need to authorize a root account, id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>"
void SetXCosGrantFullControl(const std::string& str);

Search Bucket and his Permission

Function description

Search whether Bucket exists and whether there is Permission and Access.

Method prototype

CosResult HeadBucket(const HeadBucketReq& req, HeadBucketResp* resp)

Request Sample

qcloud_cos::CosConfig config("./config.json");
qcloud_cos::CosAPI cos(config);

std::string bucket_name = "examplebucket-1250000000";

qcloud_cos::HeadBucketReq req(bucket_name);
qcloud_cos::HeadBucketResp resp;
qcloud_cos::CosResult result = cos.HeadBucket(req, &resp);

// The call is successful. You can call the member function of resp to get the return content
if (result.IsSucc()) {
    // ...
} else {
    // You can call the member function of CosResult to output the error information such as requestID
} 

Parameter description

Parameters Parameter description
Req Request for HeadBucketReq,HeadBucket operation
Resp Return of HeadBucketResp,HeadBucket operation

Deleting Buckets

Function description

Delete the empty Bucket under the specified account.

Method prototype

CosResult DeleteBucket(const DeleteBucketReq& req, DeleteBucketResp* resp)

Request Sample

qcloud_cos::CosConfig config("./config.json");
qcloud_cos::CosAPI cos(config);

std::string bucket_name = "examplebucket-1250000000";

// // The constructor of DeleteBucketReq requires bucket_name to be passed in
qcloud_cos::DeleteBucketReq req(bucket_name);
qcloud_cos::DeleteBucketResp resp;
qcloud_cos::CosResult result = cos.DeleteBucket(req, &resp);

// The call is successful. You can call the member function of resp to get the return content
if (result.IsSucc()) {
    // ...
} else {
    // You can call the member function of CosResult to output the error information such as requestID
} 

Parameter description

Parameters Parameter description
Req Request for DeleteBucketReq,DeleteBucket operation
Resp Return of DeletBucketResp,DeletBucket operation

Access control list

Set Bucket ACL

Function description

Set the specified Bucket Access Permission control list.

Method prototype

CosResult PutBucketACL(const PutBucketACLReq& req, PutBucketACLResp* resp)

Request Sample

qcloud_cos::CosConfig config("./config.json");
qcloud_cos::CosAPI cos(config);

std::string bucket_name = "examplebucket-1250000000";

// The constructor of PutBucketACLReq requires bucket_name to be passed in
qcloud_cos::PutBucketACLReq req(bucket_name);
qcloud_cos::ACLRule rule;
rule.m_id = "123";
rule.m_allowed_headers.push_back("x-cos-meta-test");
rule.m_allowed_origins.push_back("http://www.qq.com");
rule.m_allowed_origins.push_back("http://cloud.tentent.com");
rule.m_allowed_methods.push_back("PUT");
rule.m_allowed_methods.push_back("GET");
rule.m_max_age_secs = "600";
rule.m_expose_headers.push_back("x-cos-expose");
req.AddRule(rule);

qcloud_cos::PutBucketACLResp resp;
qcloud_cos::CosResult result = cos.PutBucketACL(req, &resp);

// The call is successful. You can call the member function of resp to get the return content
if (result.IsSucc()) {
    // ...
} else {
    // Set the ACL. You can call the member function of CosResult to output the error information such as requestID
} 

Parameter description

Parameters Parameter description
Req Request for PutBucketACLReq,PutBucketACL operation
Resp Return of PutBucketACLResp,PutBucketACL operation

PutBucketACLReq provides the following member functions:

// Define the ACL attribute of the bucket. Value range: private, public-read-write, public-read
// Default value: private
void SetXCosAcl(const std::string& str);

// Grant the grantee read permission in the format of x-cos-grant-read: id=" ",id=" ".
// When you need to authorize a sub-account, id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>"
// When you need to authorize a root account, id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>"
void SetXCosGrantRead(const std::string& str);

// Grant the grantee write permission in the format of x-cos-grant-write: id=" ",id=" "./
// When you need to authorize a sub-account, id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>"
// When you need to authorize a root account, id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>"
void SetXCosGrantWrite(const std::string& str);

// Grant the grantee read-write permission in the format of x-cos-grant-full-control: id=" ",id=" ".
// When you need to authorize a sub-account, id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>"
// When you need to authorize a root account, id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>"
void SetXCosGrantFullControl(const std::string& str);

// Bucket owner ID
void SetOwner(const Owner& owner);

// Set the information of grantee and permission
void SetAccessControlList(const std::vector<Grant>& grants);

// Add the permission information of a single bucket
void AddAccessControlList(const Grant& grant);

SetXCosAcl, SetXCosGrantRead, SetXCosGrantWrite, SetXCosGrantFullControl and other APIs cannot be used at the same time as SetAccessControlList and AddAccessControlList. Because the former is actually achieved by setting up HTTP Header, while the latter is adding content in XML format to Body, the two can only choose one of the two. The first category is preferred within SDK.

ACLRule is defined as follows:

struct Grantee {
    // The type can be RootAccount or SubAccount
    // If the type is RootAccount, you can enter an account ID in the uin field in the ID or enter anyone (representing all types of users) in place of uin/<OwnerUin> and uin/<SubUin>
    // If the type is RootAccount, uin represents the root account, while Subaccount represents the sub-account
    std::string m_type; 
    std::string m_id; // qcs::cam::uin/<OwnerUin>:uin/<SubUin>
    std::string m_display_name; // Optional
    std::string m_uri;
};

struct Grant {
    Grantee m_grantee; // Resource information of the grantee
    std::string m_perm; // Specify the permission granted to the grantee. Enumerated values: READ, WRITE, FULL_CONTROL
};

Query Bucket ACL

Function description

Query Bucket's Access control list.

Method prototype

CosResult GetBucketACL(const GetBucketACLReq& req, GetBucketACLResp* resp)

Request Sample

qcloud_cos::CosConfig config("./config.json");
qcloud_cos::CosAPI cos(config);

std::string bucket_name = "examplebucket-1250000000";

// The constructor of GetBucketACLReq requires bucket_name to be passed in
qcloud_cos::GetBucketACLReq req(bucket_name);
qcloud_cos::GetBucketACLResp resp;
qcloud_cos::CosResult result = cos.GetBucketACL(req, &resp);

// The call is successful. You can call the member function of resp to get the return content
if (result.IsSucc()) {
    // ...
} else {
    // Failed to get the ACL. You can call the member function of CosResult to output the error information such as requestID
} 

Parameter description

Parameters Parameter description
Req Request for GetBucketACLReq,GetBucketACL operation
Resp Return of GetBucketACLResp,GetBucketACL operation

GetBucketACLResp provides the following member functions:

std::string GetOwnerID();
std::string GetOwnerDisplayName();
std::vector<Grant> GetAccessControlList();