API | Operation | Description |
Querying objects | Queries some or all the objects in a bucket. | |
Uploading an object in whole | Uploads an object in whole to a bucket. | |
Querying object metadata | Queries the metadata of an object. | |
Downloading an object | Downloads an object to the local file system. | |
Copying an object | Copies an object to a destination path. | |
Deleting an object | Deletes a specified object from a bucket. | |
Deleting multiple objects | Deletes multiple objects from a bucket. | |
Restoring an archived object | Restores an archived object for access. |
API | Operation | Description |
Querying multipart uploads | Queries in-progress multipart uploads. | |
Initializing a multipart upload | Initializes a multipart upload. | |
Uploading parts | Uploads a file in parts. | |
Copying a part | Copies an object as a part. | |
Querying uploaded parts | Queries the uploaded parts of a multipart upload. | |
Completing a multipart upload | Completes the multipart upload of a file. | |
Aborting a multipart upload | Aborts a multipart upload and deletes the uploaded parts. |
CosResult GetBucket(const GetBucketReq& req, GetBucketResp* resp)
qcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);std::string bucket_name = "examplebucket-1250000000"; // 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. Replace it with your bucket name.qcloud_cos::GetBucketReq req(bucket_name);req.SetPrefix("image/"); // List objects in the `image/` directoryqcloud_cos::GetBucketResp resp;qcloud_cos::CosResult result = cos.GetBucket(req, &resp);// The call is successful. You can call the resp member functions to get the return content.if (result.IsSucc()) {std::vector<Content> contents = get_bucket_resp.GetContents();// Traverse the listed objectsfor (auto &content: contents) {// do something}} else {std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl;std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl;std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl;std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl;std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl;}
#include "cos_api.h"#include "cos_sys_config.h"#include "cos_defines.h"int main(int argc, char *argv[]) {// 1. Specify the path to the configuration file and initialize CosConfigqcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);// 2. Construct a request to query the object liststd::string bucket_name = "examplebucket-1250000000"; // 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. Replace it with your bucket name.qcloud_cos::GetBucketReq req(bucket_name);qcloud_cos::CosResult result;bool is_truncated = false;// 3. Traverse all objectsdo {qcloud_cos::GetBucketResp resp;result = cos.GetBucket(req, &resp);if (result.IsSucc()) {std::vector<Content> contents = get_bucket_resp.GetContents();for (auto &content: contents) {// do something}req.SetMarker(resp.GetNextMarker()); // Set the start key for the next listingis_truncated = resp.IsTruncated();}} while (result.IsSucc() && is_truncated);}
Parameter | Description | Type | Required |
req | Request of the GetBucket operation | GetBucketReq | Yes |
resp | Response of the GetBucket operation | GetBucketResp | Yes |
GetBucketReq
provides the following member functions: /// \\@brief Set a prefix, specifying the addresses of the returned filesvoid SetPrefix(const std::string& prefix);/// \\brief Set a delimiter, specifying that paths that start with the specified prefix and end with the first occurrence of the delimiter will be returnedvoid SetDelimiter(const std::string& delimiter);/// \\brief Encoding type of returned values. Valid value: `url`void SetEncodingType(const std::string& encoding_type);/// \\brief By default, entries are listed in UTF-8 binary order. The list starts after the marker.void SetMarker(const std::string& marker);/// \\brief Maximum number of entries returned at a time. Default: 1000void SetMaxKeys(uint64_t max_keys);
GetBucketResp
provides the following member functions: /// \\brief Get the metadata of an object in a bucket.std::vector<Content> GetContents() const;/// \\brief Bucket namestd::string GetName() const;/// \\brief Get the delimiter.std::string GetDelimiter() const;/// \\brief Encoding formatstd::string GetEncodingType() const;/// \\brief Prefix of the returned filestd::string GetPrefix() const;/// \\brief By default, entries are listed in UTF-8 binary order. The list starts after the marker.std::string GetMarker() const;/// \\brief Maximum number of entries returned in a single responseuint64_t GetMaxKeys() const;// \\brief Whether the returned list is truncated. The parameter is a boolean (true or false).bool IsTruncated() const;// \\brief If the returned list is truncated, "NextMarker" represents the object after which the next returned list begins.std::string GetNextMarker() const;
Content
is defined as follows:struct Content {std::string m_key; // Key of the objectstd::string m_last_modified; // Time when the object was last modifiedstd::string m_etag; // MD5 checksum of the filestd::string m_size; // File size, in bytesstd::vector<std::string> m_owner_ids; // Information about the bucket ownerstd::string m_storage_class; // Storage class of the object. Enumerated values: STANDARD, STANDARD_IA};
/// Upload using a stream.CosResult PutObject(const PutObjectByStreamReq& req, PutObjectByStreamResp* resp)/// Upload a local file.CosResult PutObject(const PutObjectByFileReq& req, PutObjectByFileResp* resp)
qcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);std::string bucket_name = "examplebucket-1250000000"; // 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. Replace it with your bucket name.std::string object_name = "object_name";std::istringstream iss("put object");// istream is required in the request constructor.qcloud_cos::PutObjectByStreamReq req(bucket_name, object_name, iss);// Call the Set method to set metadata, ACL, etc.req.SetXCosStorageClass("STANDARD_IA");// Disable MD5 checksum. To enable it, use "req.TurnOnComputeConentMd5()". MD5 checksum is enabled by default.req.TurnOffComputeConentMd5();qcloud_cos::PutObjectByStreamResp resp;qcloud_cos::CosResult result = cos.PutObject(req, &resp);if (result.IsSucc()) {// The call is successful. You can call the resp member functions to get the return content.} else {// The call failed. You can call the result member functions to get the error information.std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl;std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl;std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl;std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl;std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl;}
qcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);std::string bucket_name = "examplebucket-1250000000"; // 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. Replace it with your bucket name.std::string object_name = "object_name";// The local file path is required in the request constructor.qcloud_cos::PutObjectByFileReq req(bucket_name, object_name, "/path/to/local/file");// Call the Set method to set metadata, ACL, etc.req.SetXCosStorageClass("STANDARD_IA");// Disable MD5 checksum. To enable it, use "req.TurnOnComputeConentMd5()". MD5 checksum is enabled by default.req.TurnOffComputeConentMd5();qcloud_cos::PutObjectByFileResp resp;qcloud_cos::CosResult result = cos.PutObject(req, &resp);if (result.IsSucc()) {// The call is successful. You can call the resp member functions to get the return content.} else {// The call failed. You can call the result member functions to get the error information.std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl;std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl;std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl;std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl;std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl;}
qcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);std::string bucket_name = "examplebucket-1250000000"; // 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. Replace it with your bucket name.std::string object_name = "object_name";// The local file path is required in the request constructor.qcloud_cos::PutObjectByFileReq req(bucket_name, object_name, "/path/to/local/file");// The speed range is 819200 to 838860800 (in bit/s), that is, 100 KB/s to 100 MB/s.req.SetTrafficLimitByHeader(1048576);qcloud_cos::PutObjectByFileResp resp;qcloud_cos::CosResult result = cos.PutObject(req, &resp);if (result.IsSucc()) {// The call is successful. You can call the resp member functions to get the return content.} else {// The call failed. You can call the result member functions to get the error information.}
Parameter | Description | Type | Required |
req | Request of the PutObject operation | PutObjectByStreamReq/PutObjectByFileReq | Yes |
resp | Response of the PutObject operation | PutObjectByStreamResp/PutObjectByFileResp | Yes |
req
parameter contains the following member functions:// Cache-Control: cache policy defined in RFC 2616, which is saved as object metadatavoid SetCacheControl(const std::string& str);// Content-Disposition: filename defined in RFC 2616, which is stored as object metadatavoid SetContentDisposition(const std::string& str);// Content-Encoding: encoding format defined in RFC 2616, which is stored as object metadatavoid SetContentEncoding(const std::string& str);// Content-Type: content type (MIME) defined in RFC 2616, which is stored as object metadatavoid SetContentType(const std::string& str);// Expect: If `Expect: 100-continue` is used, the requested content will be sent only after confirmation from the server is received.void SetExpect(const std::string& str);// Expires: expiration time defined in RFC 2616, which is stored as object metadatavoid SetExpires(const std::string& str);// Customizable headers, which will be returned as object metadata of up to 2 KBvoid SetXCosMeta(const std::string& key, const std::string& value);// x-cos-storage-class: the storage class of an object. Enumerated values: STANDARD, STANDARD_IA, ARCHIVE// Default: STANDARDvoid SetXCosStorageClass(const std::string& storage_class);// Define the ACL attribute of the object. Valid values: private, public-read// Default: privatevoid SetXcosAcl(const std::string& str);// Grant read permission in the format of x-cos-grant-read: id=" ",id=" ".// To authorize a sub-account, use id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>".// To authorize the root account, use id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>".void SetXcosGrantRead(const std::string& str);// Grant read-write permission in the format of x-cos-grant-full-control: id=" ",id=" ".// To authorize a sub-account, use id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>".// To authorize the root account, use id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>".void SetXcosGrantFullControl(const std::string& str);/// Set the algorithm for server-side encryption. Currently, AES-256 is supported.void SetXCosServerSideEncryption(const std::string& str);
resp
parameter contains the following member functions:/// Get the object’s version number. If versioning is not enabled for the bucket, an empty string will be returned.std::string GetVersionId();/// Get the algorithm for server-side encryption.std::string GetXCosServerSideEncryption();
CosResult HeadObject(const HeadObjectReq& req, HeadObjectResp* resp)
qcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);std::string bucket_name = "examplebucket-1250000000"; // 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. Replace it with your bucket name.std::string object_name = "object_name";qcloud_cos::HeadObjectReq req(bucket_name, object_name);qcloud_cos::HeadObjectResp resp;qcloud_cos::CosResult result = cos.HeadObject(req, &resp);if (result.IsSucc()) {// The query is successful. You can call the HeadObjectResp member functions.} else {// The query failed. You can call the CosResult member functions to get the error information, such as requestID.}
Parameter | Description | Type | Required |
req | Request of the HeadObject operation | HeadObjectReq | Yes |
resp | Response of the HeadObject operation | HeadObjectResp | Yes |
HeadObjectResp
provides the following member functions:std::string GetXCosObjectType();std::string GetXCosStorageClass();// Get the custom metadata. The parameter can be the asterisk (*) in "x-cos-meta-*".std::string GetXCosMeta(const std::string& key);// Return all custom meta in the form of a map. The key of a map does not contain the "x-cos-meta-" prefix.std::map<std::string, std::string> GetXCosMetas();// Get the server-side encryption algorithmstd::string GetXCosServerSideEncryption();
// Download the object to a local fileCosResult GetObject(const GetObjectByFileReq& req, GetObjectByFileResp* resp)// Download the object to a streamCosResult GetObject(const GetObjectByStreamReq& req, GetObjectByStreamResp* resp)// Download the object to a local file (multi-threaded)CosResult GetObject(const MultiGetObjectReq& req, MultiGetObjectResp* resp)
qcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);std::string bucket_name = "examplebucket-1250000000"; // 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. Replace it with your bucket name.std::string object_name = "object_name";std::string local_path = "/tmp/object_name";// appid, bucketname, object, and a local path (including filename) are required for the request.qcloud_cos::GetObjectByFileReq req(bucket_name, object_name, local_path);qcloud_cos::GetObjectByFileResp resp;qcloud_cos::CosResult result = cos.GetObject(req, &resp);if (result.IsSucc()) {// The download is successful. You can call the GetObjectByFileResp member functions.} else {// You can call the CosResult member functions to get the error information, such as requestID.}
qcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);std::string bucket_name = "examplebucket-1250000000"; // 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. Replace it with your bucket name.std::string object_name = "object_name";std::string local_path = "/tmp/object_name";// appid, bucketname, object, and output stream are required for the request.std::ostringstream os;qcloud_cos::GetObjectByStreamReq req(bucket_name, object_name, os);qcloud_cos::GetObjectByStreamResp resp;qcloud_cos::CosResult result = cos.GetObject(req, &resp);if (result.IsSucc()) {// The download is successful. You can call the GetObjectByStreamResp member functions.} else {// The download failed. You can call the CosResult member functions to get the error information, such as requestID.}
qcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);std::string bucket_name = "examplebucket-1250000000";std::string object_name = "object_name";std::string local_path = "/tmp/object_name";// appid, bucketname, object, and local path (including the filename) are required for the requestqcloud_cos::MultiGetObjectReq req(bucket_name, object_name, local_path);qcloud_cos::MultiGetObjectResp resp;qcloud_cos::CosResult result = cos.GetObject(req, &resp);if (result.IsSucc()) {// The download is successful. You can call the MultiGetObjectResp member functions.} else {// The download failed. You can call the CosResult member functions to get the error information, such as requestID.}
Parameter | Description | Type | Required |
req | Request of the GetObject operation | GetObjectByFileReq/GetObjectByStreamReq/MultiGetObjectReq | Yes |
resp | Response of the GetObject operation | GetObjectByFileResp/GetObjectByStreamResp/MultiGetObjectResp | Yes |
// Set the Content-Type parameter in the response header.void SetResponseContentType(const std::string& str);// Set the Content-Language parameter in the response header.void SetResponseContentLang(const std::string& str);// Set the Content-Expires parameter in the response header.void SetResponseExpires(const std::string& str);// Set the Cache-Control parameter in the response header.void SetResponseCacheControl(const std::string& str);// Set the Content-Disposition parameter in the response header.void SetResponseContentDisposition(const std::string& str);// Set the Content-Encoding parameter in the response header.void SetResponseContentEncoding(const std::string& str);
GetObjectResp
provides the following member functions:// Get the time an object was last modified, a date in string format, such as "Wed, 28 Oct 2014 20:30:00 GMT"std::string GetLastModified();// Obtain the object type, which indicates whether the object is appendable. Enumerated values: normal, appendablestd::string GetXCosObjectType();// Obtain the storage class of the object. Enumerated values: STANDARD, STANDARD_IAstd::string GetXCosStorageClass();// Return all custom meta in the form of a map. The key of a map does not contain the "x-cos-meta-" prefix.std::map<std::string, std::string> GetXCosMetas();// Get the custom metadata. The parameter can be the asterisk (*) in "x-cos-meta-*".std::string GetXCosMeta(const std::string& key);// Get the server-side encryption algorithm.std::string GetXCosServerSideEncryption();
PUT Object - Copy
) is used to copy a file to the destination path.CosResult PutObjectCopy(const PutObjectCopyReq& req, PutObjectCopyResp* resp)
qcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);std::string bucket_name = "examplebucket-1250000000"; // 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. Replace it with your bucket name.std::string object_name = "sevenyou";qcloud_cos::PutObjectCopyReq req(bucket_name, object_name);req.SetXCosCopySource("sevenyousouthtest-12345656.cn-south.myqcloud.com/sevenyou_source_obj");qcloud_cos::PutObjectCopyResp resp;qcloud_cos::CosResult result = cos.PutObjectCopy(req, &resp);
qcloud_cos::PutObjectCopyReq req(bucket_name, object_name);req.SetXCosMeta("key1", "val1"); // Custom metadatareq.SetXCosMeta("key2", "val2"); // Custom metadatareq.SetXCosMetadataDirective("Replaced"); // This line must be addedqcloud_cos::PutObjectCopyResp resp;qcloud_cos::CosResult result = cos.PutObjectCopy(req, &resp);
qcloud_cos::PutObjectCopyReq req(bucket_name, object_name);req.SetXCosStorageClass("STANDARD_IA");req.SetXCosMetadataDirective("Replaced"); // This line must be addedqcloud_cos::PutObjectCopyResp resp;qcloud_cos::CosResult result = cos.PutObjectCopy(req, &resp);
Parameter | Description | Type | Required |
req | Request of the PutObjectCopy operation | PutObjectCopyReq | Yes |
resp | Response of the PutObjectCopy operation | PutObjectCopyResp | Yes |
PutObjectCopyReq
contains the following member functions:// URL path of the source file. A previous version can be specified by using the `versionid` subresource.void SetXCosCopySource(const std::string& str);// Whether to copy the metadata. Enumerated values: Copy (default), Replaced// If this field is set to "Copy", the user-defined metadata in the header will be ignored and the metadata will be copied directly.// If this field is set to "Replaced", the metadata will be modified based on the header information.// If the destination path and the source path are the same (that is, the user attempts to modify the metadata), set this field to "Replaced".void SetXCosMetadataDirective(const std::string& str);// If the object has been modified after the specified time, the operation is performed; otherwise, error code 412 is returned.// It can be used together with "x-cos-copy-source-If-None-Match". Using it together with other conditions can cause a conflict.void SetXCosCopySourceIfModifiedSince(const std::string& str);// If the object has not been modified after the specified time, the operation is performed; otherwise, error code 412 is returned.// It can be used together with "x-cos-copy-source-If-Match". Using it together with other conditions can cause a conflict.void SetXCosCopySourceIfUnmodifiedSince(const std::string& str);// If the Etag of the object is the same as the specified one, the operation is performed; otherwise, error code 412 is returned.// It can be used together with "x-cos-copy-source-If-Unmodified-Since". Using it together with other conditions can cause a conflict.void SetXCosCopySourceIfMatch(const std::string& str);// If the Etag of the object is different from the specified one, the operation is performed; otherwise, error code 412 is returned.// It can be used together with "x-cos-copy-source-If-Modified-Since". Using it together with other conditions can cause a conflict.void SetXCosCopySourceIfNoneMatch(const std::string& str);// x-cos-storage-class: the storage class of an object. Enumerated values: STANDARD, STANDARD_IA// Default: STANDARDvoid SetXCosStorageClass(const std::string& storage_class);// Define the ACL attribute of the object. Valid values: private, public-read// Default: privatevoid SetXCosAcl(const std::string& str);// Grant read permission in the format of id="[OwnerUin]".void SetXCosGrantRead(const std::string& str);// Grant full permission in the format of id="[OwnerUin]".void SetXCosGrantFullControl(const std::string& str);// Customizable headers, which will be returned as object metadata of up to 2 KBvoid SetXCosMeta(const std::string& key, const std::string& value);/// Set the algorithm for server-side encryption. Currently, AES-256 is supported.void SetXCosServerSideEncryption(const std::string& str);
PutObjectCopyResp
contains the following member functions:// Return the MD5 checksum of the file. The value of ETag can be used to check whether the object content has changed.std::string GetEtag();// Return the time the file was last modified, in GMT.std::string GetLastModified();// Return the version ID.std::string GetVersionId();/// Get the algorithm for server-side encryption.std::string GetXCosServerSideEncryption();
CosResult DeleteObject(const DeleteObjectReq& req, DeleteObjectResp* resp)
qcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);std::string bucket_name = "examplebucket-1250000000"; // 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. Replace it with your bucket name.std::string object_name = "test_object";qcloud_cos::DeleteObjectReq req(bucket_name, object_name);qcloud_cos::DeleteObjectResp resp;qcloud_cos::CosResult result = cos.DeleteObject(req, &resp);// The call is successful. You can call the resp member functions to get the return content.if (result.IsSucc()) {// ...} else {// You can call the CosResult member functions to get the error information, such as requestID.}
DeleteObject
operation | DeleteObjectReq | Yes |
| resp | Response of the DeleteObject
operation | DeletObjectResp | Yes |CosResult DeleteObjects(const DeleteObjectsReq& req, DeleteObjectsResp* resp)
qcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);std::string bucket_name = "examplebucket-1250000000"; // 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. Replace it with your bucket name.std::vector<std::string> objects;std::vector<ObjectVersionPair> to_be_deleted;objects.push_back("batch_delete_test_00");objects.push_back("batch_delete_test_01");objects.push_back("batch_delete_test_02");objects.push_back("batch_delete_test_03");for (size_t idx = 0; idx < objects.size(); ++idx) {ObjectVersionPair pair;pair.m_object_name = objects[idx];to_be_deleted.push_back(pair);}qcloud_cos::DeleteObjectsReq req(bucket_name, to_be_deleted);qcloud_cos::DeleteObjectsResp resp;qcloud_cos::CosResult result = cos.DeleteObjects(req, &resp);// The call is successful. You can call the resp member functions to get the return content.if (result.IsSucc()) {// ...} else {// You can call the CosResult member functions to get the error information, such as requestID.}
Parameter | Description | Type | Required |
req | Request of the DeleteObjects operation | DeleteObjectsReq | Yes |
resp | Response of the DeleteObjects operation | DeletObjectsResp | Yes |
DeleteObjectsReq
contains the following member functions:// Add an object and specify the version.void AddObjectVersion(const std::string& object, const std::string& version)// Add an object with no versioning information.void AddObject(const std::string& object)
DeleteObjectsResp
contains the following member functions:// Get the information of successfully deleted objects.std::vector<DeletedInfo> GetDeletedInfos() const// Get the information of objects that failed to be deleted.std::vector<ErrorInfo> GetErrorMsgs() const
DeletedInfo
and ErrorInfo
are as follows:struct DeletedInfo{std::string m_key; // object keystd::string m_version_id; //version_idbool m_delete_marker; // is delete markerstd::string m_delete_marker_version_id; // if delete marker, so version id}struct ErrorInfo{std::string m_key; // object keystd::string m_code; // error codestd::string m_message; // error messagestd::string m_version_id; // version id}
CosResult PostObjectRestore(const PostObjectRestoreReq& req, PostObjectRestoreResp* resp)
qcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);std::string bucket_name = "examplebucket-1250000000"; // 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. Replace it with your bucket name.std::string object_name = "sevenyou";{qcloud_cos::PostObjectRestoreReq req(bucket_name, object_name);req.SetExiryDays(30);req.SetTier("Standard");qcloud_cos::PostObjectRestoreResp resp;qcloud_cos::CosResult result = cos.PostObjectRestore(req, &resp);// The call is successful. You can call the resp member functions to get the return content.if (result.IsSucc()) {// ...} else {// You can call the CosResult member functions to get the error information, such as requestID.}}
Parameter | Description | Type | Required |
req | Request of the PostObjectRestore operation | PostObjectRestoreReq | Yes |
resp | Response of the PostObjectRestore operation | PostObjectRestoreResp | Yes |
PostObjectRestoreReq
contains the following member functions:// Set the expiration time of the temporary copy.void SetExiryDays(uint64_t days);// Enumerated values: Expedited, Standard (default), Bulkvoid SetTier(const std::string& tier);
List Multipart Uploads
) is used to query in-progress multipart uploads in a specified bucket.CosResult CosAPI::ListMultipartUpload(const ListMultipartUploadReq& request, ListMultipartUploadResp* response)
qcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);std::string bucket_name = "examplebucket-1250000000"; // 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. Replace it with your bucket name.std::string object_name = "test_object";qcloud_cos::ListMultipartUploadReq req(bucket_name, object_name);qcloud_cos::ListMultipartUploadResp resp;qcloud_cos::CosResult result = cos.ListMultipartUpload(req, &resp);for (std::vector<qcloud_cos::Upload>::const_iterator itr = rst.begin(); itr != rst.end(); ++itr) {const qcloud_cos::Upload& upload = *itr;std::cout << "key = " << upload.m_key << ", uploadid= " << upload.m_uploadid << ", storagen class = " << upload.m_storage_class << ", m_initiated= " << upload.m_initiated << std::endl;}// The call is successful. You can call the resp member functions to get the return content.if (result.IsSucc()) {// ...} else {// You can call the CosResult member functions to get the error information, such as requestID.}
Parameter | Description | Type | Required |
req | Request of the ListMultipartUpload operation | ListMultipartUploadReq | Yes |
resp | Response of the ListMultipartUpload operation | ListMultipartUploadResp | Yes |
ListMultipartUploadReq
contains the following member functions:// Specify the prefix that the returned object keys must have. Note that when you query by prefix, the returned keys will contain the prefix.void SetPrefix(const std::string& prefix);// Set the delimiter. Objects with identical strings between the specified prefix and the first occurrence of the delimiter are grouped together and defined as common prefixes. If there is no prefix, the string between the start of the path and the first delimiter is compared.void SetDelimiter(const std::string& delimiter);// Specify the encoding format for the returned value. Valid value: urlvoid SetEncodingType(const std::string& encoding_type);// This field is used together with "upload-id-marker". If "upload-id-marker" is not specified, multipart uploads whose "ObjectName" is lexicographically greater than "key-marker" will be listed. If "upload-id-marker" is specified, multipart uploads whose "ObjectName" is lexicographically greater than "key-marker" will be listed, and multipart uploads whose "ObjectName" is lexicographically equal to "key-marker" with "UploadID" greater than "upload-id-marker" will be listed.void SetKeyMarker(const std::string& marker);// Set the maximum number of parts to return. Value range: 1-1000 (default: 1000)void SetMaxUploads(const std::string& max_uploads);// This field is used together with "key-marker". If "key-marker" is not specified, "upload-id-marker" will be ignored. If "key-marker" is specified, multipart uploads whose "ObjectName" is lexicographically greater than "key-marker" will be listed, and multipart uploads whose "ObjectName" is lexicographically equal to "key-marker" with "UploadID" greater than "upload-id-marker" will be listed.void SetUploadIdMarker(const std::string& upload_id_marker);
ListMultipartUploadResp
contains the following member functions:// Get the metadata of an object in a bucket.std::vector<Upload> GetUpload();// Bucket namestd::string GetName();// Encoding formatstd::string GetEncodingType() const;// By default, entries are listed in UTF-8 binary order. The list starts after the marker.std::string GetMarker() const;// The entry list starts after UploadId.std::string GetUploadIdMarker() const;// If the returned list is truncated, "NextKeyMarker" represents the key after which the next returned list begins.std::string GetNextKeyMarker() const;// If the returned list is truncated, "UploadId" represents the upload ID after which the next returned list begins.std::string GetNextUploadIdMarker() const;// The maximum number of returned parts allowed. Value range: 0-1000std::string GetMaxUploads () const;// Whether the returned list is truncated. The parameter is a boolean (true or false).bool IsTruncated();// The prefix of returned objectsstd::string GetPrefix() const;// The delimiterstd::string GetDelimiter() const;// Objects with identical paths between the specified prefix and the delimiter are grouped together and defined as common prefixes.std::vector<std::string> GetCommonPrefixes() const
Initiate Multipart Upload
) is used to initialize a multipart upload and obtain its uploadId
.CosResult InitMultiUpload(const InitMultiUploadReq& req, InitMultiUploadResp* resp)
qcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);std::string bucket_name = "examplebucket-1250000000"; // 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. Replace it with your bucket name.std::string object_name = "object_name";qcloud_cos::InitMultiUploadReq req(bucket_name, object_name);qcloud_cos::InitMultiUploadResp resp;qcloud_cos::CosResult result = cos.InitMultiUpload(req, &resp);std::string upload_id = "";if (result.IsSucc()) {upload_id = resp.GetUploadId();}
Parameter | Description | Type | Required |
req | Request of the InitMultiUpload operation | InitMultiUploadReq | Yes |
resp | Response of the InitMultiUpload operation | InitMultiUploadResp | Yes |
InitMultiUploadReq
member functions are as follows:// Cache-Control: cache policy defined in RFC 2616, which is saved as object metadatavoid SetCacheControl(const std::string& str);// Content-Disposition: filename defined in RFC 2616, which is stored as object metadatavoid SetContentDisposition(const std::string& str);// Content-Encoding: encoding format defined in RFC 2616, which is stored as object metadatavoid SetContentEncoding(const std::string& str);// Content-Type: content type (MIME) defined in RFC 2616, which is stored as object metadatavoid SetContentType(const std::string& str);// Expires: expiration time defined in RFC 2616, which is stored as object metadatavoid SetExpires(const std::string& str);// Customizable headers, which will be returned as object metadata of up to 2 KBvoid SetXCosMeta(const std::string& key, const std::string& value);// x-cos-storage-class: the storage class of an object. Enumerated values: STANDARD, STANDARD_IA, ARCHIVE// Default: STANDARDvoid SetXCosStorageClass(const std::string& storage_class);// Define the ACL attribute of the object. Valid values: private, public-read// Default: privatevoid SetXcosAcl(const std::string& str);// Grant read permission in the format of x-cos-grant-read: id=" ",id=" ".// To authorize a sub-account, use id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>".// To authorize the root account, use id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>".void SetXcosGrantRead(const std::string& str);// Grant read-write permission in the format of x-cos-grant-full-control: id=" ",id=" ".// To authorize a sub-account, use id="qcs::cam::uin/<OwnerUin>:uin/<SubUin>".// To authorize the root account, use id="qcs::cam::uin/<OwnerUin>:uin/<OwnerUin>".void SetXcosGrantFullControl(const std::string& str);/// Set the algorithm for server-side encryption. Currently, AES-256 is supported.void SetXCosServerSideEncryption(const std::string& str);
bucket
, key
, and uploadId
, which represent the destination bucket of the multipart upload, the object name, and the upload ID (required for the subsequent multipart upload) respectively.InitMultiUploadResp
member functions are as follows:std::string GetBucket();std::string GetKey();std::string GetUploadId();// Algorithm for server-side encryptionstd::string GetXCosServerSideEncryption();
Upload Part
) is used to upload parts.CosResult UploadPartData(const UploadPartDataReq& request, UploadPartDataResp* response)
qcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);std::string bucket_name = "examplebucket-1250000000"; // 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. Replace it with your bucket name.std::string object_name = "test_object";// Upload the first part.{std::fstream is("demo_5M.part1");qcloud_cos::UploadPartDataReq req(bucket_name, object_name, upload_id, is);req.SetPartNumber(1);// Disable MD5 checksum. To enable it, use "req.TurnOnComputeConentMd5()". MD5 checksum is enabled by default.req.TurnOffComputeConentMd5();qcloud_cos::UploadPartDataResp resp;qcloud_cos::CosResult result = cos.UploadPartData(req, &resp);// After the upload is successful, you should record the part number and the returned ETagif (result.IsSucc()) {etags.push_back(resp.GetEtag());part_numbers.push_back(1);}is.close();}// Upload the second part.{std::fstream is("demo_5M.part2");qcloud_cos::UploadPartDataReq req(bucket_name, object_name,upload_id, is);req.SetPartNumber(2);qcloud_cos::UploadPartDataResp resp;qcloud_cos::CosResult result = cos.UploadPartData(req, &resp);// After the upload is successful, you should record the part number and the returned ETagif (result.IsSucc()) {etags.push_back(resp.GetEtag());part_numbers.push_back(2);}is.close();}
Parameter | Description | Type | Required |
req | Request of the UploadPartData operation | UploadPartDataReq | Yes |
resp | Response of the UploadPartData operation | UploadPartDataResp | Yes |
UploadPartDataReq
, you need to specify the APPID
, bucket
, object
, UploadId
, which you can get from the response of initialization, as well as the data stream to upload (which you should close after the call is completed).UploadPartDataReq(const std::string& bucket_name,const std::string& object_name, const std::string& upload_id,std::istream& in_stream);
void SetPartNumber(uint64_t part_number);
UploadPartDataResp
contains the following member function:/// Algorithm for server-side encryptionstd::string GetXCosServerSideEncryption();
CosResult UploadPartCopyData(const UploadPartCopyDataReq& request,UploadPartCopyDataResp* response)
qcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);std::string bucket_name = "examplebucket-1250000000"; // 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. Replace it with your bucket name.std::string object_name = "test_object";std::string upload_id;std::vector<uint64_t> numbers;std::vector<std::string> etags;std::string etag1 = "", etag2 = "";InitMultiUpload(cos, bucket_name, object_name, &upload_id);// First partqcloud_cos::UploadPartCopyDataReq req(bucket_name, object_name, upload_id, 1);req.SetXCosCopySource("sevenyousouth-1251668577.cos.ap-guangzhou.myqcloud.com/seven_10G.tmp");req.SetXCosCopySourceRange("bytes=0-1048576000");qcloud_cos::UploadPartCopyDataResp resp;qcloud_cos::CosResult result = cos.UploadPartCopyData(req, &resp);if (result.IsSucc()) {etag1 = resp.GetEtag();}numbers.push_back(1);etags.push_back(etag1);// Second partqcloud_cos::UploadPartCopyDataReq req2(bucket_name, object_name, upload_id, 2);req2.SetXCosCopySource("sevenyoutest-7319456.cos.cn-north.myqcloud.com/sevenyou_2G_part");req2.SetXCosCopySourceRange("bytes=1048576000-2097152000");qcloud_cos::UploadPartCopyDataResp resp2;qcloud_cos::CosResult result = cos.UploadPartCopyData(req2, &resp2);if (result.IsSucc()) {etag2 = resp2.GetEtag();}numbers.push_back(2);etags.push_back(etag2);CompleteMultiUpload(cos, bucket_name, object_name, upload_id, etags, numbers);
Parameter | Description | Type | Required |
req | Request of the UploadPartCopyData operation | UploadPartCopyDataReq | Yes |
resp | Response of the UploadPartCopyData operation | UploadPartCopyDataResp | Yes |
/// Set the ID of the multipart copy.void SetUploadId(const std::string& upload_id)/// Set the part number.void SetPartNumber(uint64_t part_number)/// Set the source file URL for the multipart copy. A previous version can be specified using the versionid sub-resource.void SetXCosCopySource(const std::string& src)/// Set the byte range of the source file to copy in the format of bytes=first-last.void SetXCosCopySourceRange(const std::string& range)/// If the object has been modified after the specified time, the operation is performed; otherwise, error code 412 is returnedvoid SetXCosCopySourceIfModifiedSince(const std::string& date)/// If the object has not been modified after the specified time, the operation is performed; otherwise, error code 412 is returnedvoid SetXCosCopySourceIfUnmodifiedSince(const std::string& date)/// If the Etag of the object is the same as the specified one, the operation is performed; otherwise, error code 412 is returnedvoid SetXCosCopySourceIfMatch(const std::string& etag)/// If the Etag of the object is different from the specified one, the operation is performed; otherwise, error code 412 is returnedvoid SetXCosCopySourceIfNoneMatch(const std::string& etag)
/// Get the MD5 checksum of the returned file.std::string GetEtag() const/// Get the time the file was last modified, in GMT.std::string GetLastModified() const/// Get the algorithm for server-side encryption.std::string GetXCosServerSideEncryption() const
CosResult ListParts(const ListPartsReq& req, ListPartsResp* resp)
qcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);std::string bucket_name = "examplebucket-1250000000"; // 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. Replace it with your bucket name.std::string object_name = "test_object";// uploadId is obtained by calling InitMultiUpload.qcloud_cos::ListPartsReq req(bucket_name, object_name, upload_id);req.SetMaxParts(1);req.SetPartNumberMarker("1");qcloud_cos::ListPartsResp resp;qcloud_cos::CosResult result = cos.ListParts(req, &resp);// The call is successful. You can call the resp member functions to get the return content.if (result.IsSucc()) {// ...} else {// You can call the CosResult member functions to get the error information, such as requestID.}
Parameter | Description | Type | Required |
req | Request of the ListParts operation | ListPartsReq | Yes |
resp | Response of the ListParts operation | ListPartsResp | Yes |
ListPartsReq
contains the following member functions:// Constructor: bucket name, object name, and multipart upload IDListPartsReq(const std::string& bucket_name,const std::string& object_name,const std::string& upload_id);// \\brief The encoding format of the returned valuevoid SetEncodingType(const std::string& encoding_type);// \\brief Maximum number of entries returned at a time. Default: 1000void SetMaxParts(uint64_t max_parts);// \\brief By default, entries are listed in UTF-8 binary order. The list starts after the marker.void SetPartNumberMarker(const std::string& part_number_marker);
ListPartsResp
contains the following member functions:// Destination bucket for the multipart uploadstd::string GetBucket();// Encoding format for the returned valuestd::string GetEncodingType();// Object namestd::string GetKey();// ID of the multipart uploadstd::string GetUploadId();// The upload initiatorInitiator GetInitiator();// Part ownerOwner GetOwner();// By default, entries are listed in UTF-8 binary order. The list starts after the marker.uint64_t GetPartNumberMarker();// Get the information of each part.std::vector<Part> GetParts();// If the returned list is truncated, "NextMarker" represents the object after which the next returned list begins.uint64_t GetNextPartNumberMarker();// Storage class of the parts. Enumerated values: Standard, Standard_IA, Archivestd::string GetStorageClass();// Maximum number of entries that can be returned at a timeuint64_t GetMaxParts();// Whether the returned list is truncated. It is a boolean (TRUE or FALSE).bool IsTruncated();
Initiator
, Owner
, and Part
are defined as follows:struct Initiator {std::string m_id; // Initiator’s unique IDstd::string m_display_name; // Initiator name};struct Owner {std::string m_id; // Owner’s unique IDstd::string m_display_name; // Owner name};struct Part {uint64_t m_part_num; // Part numberuint64_t m_size; // Part size, in bytesstd::string m_etag; // MD5 checksum of the partstd::string m_last_modified; // Time the part was last modified};
CosResult CompleteMultiUpload(const CompleteMultiUploadReq& request, CompleteMultiUploadResp* response)
qcloud_cos::CompleteMultiUploadReq req(bucket_name, object_name, upload_id);qcloud_cos::CompleteMultiUploadResp resp;req.SetEtags(etags);req.SetPartNumbers(part_numbers);qcloud_cos::CosResult result = cos.CompleteMultiUpload(req, &resp);
Parameter | Description | Type | Required |
req | Request of the CompleteMultiUpload operation | CompleteMultiUploadReq | Yes |
resp | Response of the CompleteMultiUpload operation | CompleteMultiUploadResp | Yes |
CompleteMultiUploadReq
, you need to specify APPID
, bucket
, object
, and UploadId
, which can be obtained from the response of initialization.CompleteMultiUploadReq(const std::string& bucket_name,const std::string& object_name, const std::string& upload_id)
// When calling the following methods, make sure that the part numbers and ETag are listed in the same order.void SetPartNumbers(const std::vector<uint64_t>& part_numbers);void SetEtags(const std::vector<std::string>& etags) ;// Add a part number and ETag.void AddPartEtagPair(uint64_t part_number, const std::string& etag);// Set the algorithm for server-side encryption. Currently, AES-256 is supported.void SetXCosServerSideEncryption(const std::string& str);
CompleteMultiUploadResp
includes Location
, Bucket
, Key
, and ETag
, which represent the public network access domain name of the created object, the destination bucket for the multipart upload, the name of the object, and the MD5 checksum of the merged file respectively. You can call the following member functions to access the response.std::string GetLocation();std::string GetKey();std::string GetBucket();std::string GetEtag();// Algorithm for server-side encryptionstd::string GetXCosServerSideEncryption();
CosResult AbortMultiUpload(const AbortMultiUploadReq& request, AbortMultiUploadResp* response)
qcloud_cos::AbortMultiUploadReq req(bucket_name, object_name, upload_id);qcloud_cos::AbortMultiUploadResp resp;qcloud_cos::CosResult result = cos.AbortMultiUpload(req, &resp);
Parameter | Description | Type | Required |
req | Request of the AbortMultiUpload operation | AbortMultiUploadReq | Yes |
resp | Response of the AbortMultiUpload operation | AbortMultiUploadResp | Yes |
AbortMultiUploadReq
, you need to specify Bucket
, Object
, and Upload_id
.AbortMultiUploadReq(const std::string& bucket_name,const std::string& object_name, const std::string& upload_id);
BaseResp
member functions to get the common header.CosResult MultiUploadObject(const MultiUploadObjectReq& request, MultiUploadObjectResp* response)
qcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);std::string bucket_name = "examplebucket-1250000000"; // 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. Replace it with your bucket name.std::string object_name = "exampleobject";std::string local_file = "./test"qcloud_cos::MultiUploadObjectReq req(bucket_name, object_name, local_file);// To keep the chunks alive inside the completion API, we recommend setting a longer timeout period.req.SetRecvTimeoutInms(1000 * 60);qcloud_cos::MultiUploadObjectResp resp;qcloud_cos::CosResult result = cos.MultiUploadObject(req, &resp);// The call is successful. You can call the resp member functions to get the return content.if (result.IsSucc()) {// ...} else {// You can call the CosResult member functions to get the error information, such as requestID.}
Parameter | Description | Type | Required |
req | Request of the MultiUploadObject operation | MultiUploadObjectReq | Yes |
resp | Response of the MultiUploadObject operation | MultiUploadObjectResp | Yes |
MultiUploadObjectReq
contains the following member functions:// Set the part size. If it is set to less than 1 MB, 1 MB will be used. If it is set to greater than 5 GB, 5 GB will be used.void SetPartSize(uint64_t bytes)// Customizable headers, which will be returned as object metadata of up to 2 KBvoid SetXCosMeta(const std::string& key, const std::string& value)// Set the algorithm for server-side encryption. Currently, AES-256 is supported.void SetXCosServerSideEncryption(const std::string& str)// Set the internal thread pool size.void SetThreadPoolSize(int size)
MultiUploadObjectResp
contains the following member functions:std::string GetRespTag()/// Algorithm for server-side encryptionstd::string GetXCosServerSideEncryption() const
CosResult GetObject(const MultiGetObjectReq& request, MultiGetObjectResp* response)
qcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);std::string bucket_name = "examplebucket-1250000000"; // 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. Replace it with your bucket name.std::string object_name = "exampleobject";std::string file_path = "./test";qcloud_cos::MultiGetObjectReq req(bucket_name, object_name, file_path);qcloud_cos::MultiGetObjectResp resp;qcloud_cos::CosResult result = cos.GetObject(req, &resp);// The call is successful. You can call the resp member functions to get the return content.if (result.IsSucc()) {// ...} else {// You can call the CosResult member functions to get the error information, such as requestID.}
Parameter | Description | Type | Required |
req | Request of the MultiGetObject operation | MultiGetObjectReq | Yes |
resp | Response of the MultiGetObject operation | MultiGetObjectResp | Yes |
MultiGetObjectReq
contains the following member functions:// Set the part size.void SetSliceSize(uint64_t bytes)// Set the thread pool size.void SetThreadPoolSize(int size)
MultiGetObjectResp
contains the following member functions:/// Algorithm for server-side encryptionstd::string GetXCosServerSideEncryption() const
CosResult ResumableGetObject(const MultiGetObjectReq& request, MultiGetObjectResp* response)
qcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);std::string bucket_name = "examplebucket-1250000000"; // 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. Replace it with your bucket name.std::string object_name = "exampleobject";std::string file_path = "./test";qcloud_cos::MultiGetObjectReq req(bucket_name, object_name, file_path);qcloud_cos::MultiGetObjectResp resp;qcloud_cos::CosResult result = cos.ResumableGetObject(req, &resp);// The call is successful. You can call the resp member functions to get the return content.if (result.IsSucc()) {// ...} else {// You can call the CosResult member functions to get the error information, such as requestID.}
Parameter | Description | Type | Required |
req | Request of the MultiGetObject operation | MultiGetObjectReq | Yes |
resp | Response of the MultiGetObject operation | MultiGetObjectResp | Yes |
qcloud_cos::CosConfig config("./config.json");qcloud_cos::CosAPI cos(config);std::string bucket_name = "examplebucket-1250000000"; // 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. Replace it with your bucket name.std::string server_dir_name = "image/"; // COS directorystd::string local_dir_name = "/data/"; // Local directoryqcloud_cos::GetBucketReq get_bucket_req(bucket_name);get_bucket_req.SetPrefix(server_dir_name);qcloud_cos::CosResult get_bucket_result;bool is_truncated = false;do {qcloud_cos::GetBucketResp get_bucket_resp;// List objectsget_bucket_result = cos.GetBucket(get_bucket_req, &get_bucket_resp);if (get_bucket_result.IsSucc()) {std::vector<Content> contents = get_bucket_resp.GetContents();for (auto& content : contents) {std::cout << "====process " << content.m_key << std::endl;std::string local_file_name = local_dir_name + content.m_key;if (StringUtil::StringEndsWith(local_file_name, "/")) {continue;}// Create a local directorysize_t found_dir = local_file_name.find_last_of("/");if (found_dir) {std::string dirname = local_file_name.substr(0, found_dir);char* p_dirname = const_cast<char*>(dirname.c_str());struct stat buffer;if (stat(p_dirname, &buffer) != 0) {std::cout << "====mkdir " << dirname << std::endl;std::string mkdir_cmd = "mkdir -p " + dirname;system(mkdir_cmd.c_str());}}// Download an objectGetObjectByFileReq get_req(bucket_name, content.m_key, local_file_name);GetObjectByFileResp get_resp;CosResult get_result = cos.GetObject(get_req, &get_resp);if (get_result.IsSucc()) {std::cout << "====download " << content.m_key << " to "<< local_file_name << " succeed" << std::endl;} else {std::cout << "====download " << content.m_key << " to "<< local_file_name << " failed" << std::endl;}}get_bucket_req.SetMarker(get_bucket_resp.GetNextMarker()); // Set the start key for the next listingis_truncated = get_bucket_resp.IsTruncated();}} while (get_bucket_result.IsSucc() && is_truncated);
Was this page helpful?