You need to enable JavaScript to run this app.
Torch Object Storage

Torch Object Storage

Copy page
Download PDF
Manage objects
Managing Object Metadata (C++ SDK)
Copy page
Download PDF
Managing Object Metadata (C++ SDK)
Object metadata refers to the attribute description of an object, comprising both HTTP standard attributes (HTTP Header) and user-defined metadata (User Meta).​
Set Custom Metadata​
warning​
  • By default, only the object owner has permission to set object metadata.​
  • When configuring user-defined metadata, the key for user-defined metadata using English letters supports only lowercase letters and does not allow spaces or other special characters.​
Setting Metadata Information During Standard Upload​
You can set object metadata to identify the purpose or attributes of an object. The following code is used to set object metadata.​
​
#include "TosClientV2.h"​
using namespace VolcengineTos;​
​
int main(void){​
​
​
std::string region = "Your Region";​
std::string accessKey = std::getenv("TOS_ACCESS_KEY");​
std::string secretKey = std::getenv("TOS_SECRET_KEY");​
​
std::string bucketName = "examplebucket";​
​
std::string objectName = "exampledir/exampleobject.txt";​
​
​
InitializeClient();​
​
TosClientV2 client(region, accessKey, secretKey);​
​
​
std::string data("Object data to be uploaded");​
auto ss = std::make_shared<std::stringstream>(data);​
PutObjectV2Input input(bucketName, objectName, ss);​
​
​
input.setAcl(ACLType::PublicRead);​
​
input.setStorageClass(StorageClassType::IA);​
​
input.setMeta({{"self-test", "yes"}});​
​
input.setContentType("application/json");​
​
auto output = client.putObject(input);​
if (!output.isSuccess()) {​
​
std::cout << "PutObjectWithMetaData failed." << output.error().String() << std::endl;​
​
CloseClient();​
return -1;​
}​
​
std::cout << "PutObjectWithMetaData success. the object etag:" << output.result().getETag() << std::endl;​
​
​
CloseClient();​
return 0;​
}​
​
Setting Object Metadata During Multipart Upload​
​
#include "TosClientV2.h"​
using namespace VolcengineTos;​
​
static int64_t getFileSize(const std::string& file)​
{​
std::fstream f(file, std::ios::in | std::ios::binary);​
f.seekg(0, f.end);​
int64_t size = f.tellg();​
f.close();​
return size;​
}​
​
int main(void){​
​
​
std::string region = "Your Region";​
std::string accessKey = std::getenv("TOS_ACCESS_KEY");​
std::string secretKey = std::getenv("TOS_SECRET_KEY");​
​
std::string bucketName = "examplebucket";​
​
std::string objectName = "exampledir/exampleobject.txt";​
​
​
InitializeClient();​
​
TosClientV2 client(region, accessKey, secretKey);​
​
​
CreateMultipartUploadInput input(bucketName, objectName);​
​
input.setAcl(ACLType::PublicRead);​
input.setStorageClass(StorageClassType::IA);​
input.setMeta({{"self-test", "yes"}});​
auto upload = client.createMultipartUpload(input);​
if (!upload.isSuccess()) {​
​
std::cout << "createMultipartUpload failed." << upload.error().String() << std::endl;​
​
CloseClient();​
return -1;​
}​
std::cout << "createMultipartUpload success." << std::endl;​
​
​
std::string fileToUpload = "yourLocalFilename";​
int64_t partSize = 100 * 1024;​
std::vector<UploadedPartV2> partResList;​
auto fileSize = getFileSize(fileToUpload);​
int partCount = static_cast<int>(fileSize / partSize);​
​
if (fileSize % partSize != 0) {​
partCount++;​
}​
​
​
auto uploadId = upload.result().getUploadId();​
for (int i = 1; i <= partCount; i++) {​
auto offset = partSize * (i - 1);​
auto size = (partSize < fileSize - offset) ? partSize : (fileSize - offset);​
std::shared_ptr<std::iostream> content = std::make_shared<std::fstream>(fileToUpload, std::ios::in|std::ios::binary);​
content->seekg(offset, std::ios::beg);​
​
UploadPartV2Input uploadPartInput(bucketName, objectName,uploadId, size,i,content);​
auto uploadPartOutput = client.uploadPart(uploadPartInput);​
if (!uploadPartOutput.isSuccess()) {​
std::cout << "uploadPart failed." << upload.error().String() << std::endl;​
​
CloseClient();​
return -1;​
}​
UploadedPartV2 part(i, uploadPartOutput.result().getETag());​
partResList.push_back(part);​
}​
​
​
CompleteMultipartUploadV2Input completeMultipartUploadInput(bucketName, objectName, uploadId, partResList);​
auto com = client.completeMultipartUpload(completeMultipartUploadInput);​
if (com.isSuccess()) {​
​
std::cout << "CompleteMultipartUpload failed." << upload.error().String() << std::endl;​
​
CloseClient();​
return -1;​
}​
std::cout << "CompleteMultipartUpload success." << com.result().getRequestInfo().getRequestId() << std::endl;​
CloseClient();​
return 0;​
}​
​
Set object metadata via the setObjectMeta interface.​
​
#include "TosClientV2.h"​
using namespace VolcengineTos;​
​
int main(void){​
​
​
std::string region = "Your Region";​
std::string accessKey = std::getenv("TOS_ACCESS_KEY");​
std::string secretKey = std::getenv("TOS_SECRET_KEY");​
​
std::string bucketName = "examplebucket";​
​
std::string objectName = "exampledir/exampleobject.txt";​
​
​
InitializeClient();​
​
TosClientV2 client(region, accessKey, secretKey);​
​
SetObjectMetaInput input(bucketName,objectName);​
​
input.setMeta({{"self-test", "yes"}});​
auto output = client.setObjectMeta(input);​
if (!output.isSuccess()) {​
​
std::cout << "SetObjectMeta failed." << output.error().String() << std::endl;​
​
CloseClient();​
return -1;​
}​
​
std::cout << "SetObjectMeta success." << std::endl;​
​
​
CloseClient();​
return 0;​
}​
​
Related Documents​
For API documentation on setting object metadata, refer to SetObjectMeta.​
Retrieving Object Metadata​
warning​
To obtain object metadata, you must have the tos:GetObject permission. For specific instructions, refer to IAM Policy Overview.​
Query Object Metadata​
You can obtain object metadata using headObject. The following code example demonstrates how to retrieve object metadata.​
​
#include "TosClientV2.h"​
using namespace VolcengineTos;​
​
int main(void){​
​
​
std::string region = "Your Region";​
std::string accessKey = std::getenv("TOS_ACCESS_KEY");​
std::string secretKey = std::getenv("TOS_SECRET_KEY");​
​
std::string bucketName = "examplebucket";​
​
std::string objectName = "exampledir/exampleobject.txt";​
​
​
InitializeClient();​
​
TosClientV2 client(region, accessKey, secretKey);​
​
HeadObjectV2Input input(bucketName,objectName);​
auto output = client.headObject(input);​
if (!output.isSuccess()) {​
​
std::cout << "HeadObject failed." << output.error().String() << std::endl;​
​
CloseClient();​
return -1;​
}​
​
std::cout << "HeadObject success." << std::endl;​
std::cout << output.result().getStringFormatStorageClass() << ","​
<< output.result().getContentDisposition() << ","​
<< output.result().getContentType() << ","​
<< output.result().getContentLength() << std::endl;​
for(const auto& meta : output.result().getMeta()){​
std::cout << meta.first << "," << meta.second << std::endl;​
}​
​
CloseClient();​
return 0;​
}​
​
Relevant Documentation​
For API documentation on querying object metadata, refer to HeadObject.​
Last updated: 2025.09.25 14:22:48