Object Storage Service (OSS) stores data as objects in a flat namespace — there is no real directory hierarchy. To organize objects, OSS simulates folders using a key-naming convention: an object whose key ends with a forward slash (/) acts as a folder marker. Operations such as listing with a prefix or deleting by prefix let you treat that prefix as if it were a directory.
Prerequisites
Before you begin, make sure that:
The OSS C++ SDK is installed and initialized in your project
The
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables are set with valid access credentialsThe target bucket exists in the region you specify
The examples on this page use the public endpoint of the China (Hangzhou) region. To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. For endpoint details, see Regions and endpoints. To create an OSSClient instance using a custom domain name or Security Token Service (STS), see Create an OSSClient instance.
Create a folder
Creating a folder means uploading a zero-byte object whose key ends with /. The following example creates a folder named exampledir/ in examplebucket.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
// 1. Set the endpoint and region for the bucket.
// Example endpoint: https://oss-cn-hangzhou.aliyuncs.com
// Example region: cn-hangzhou
std::string Endpoint = "yourEndpoint";
std::string Region = "yourRegion";
// 2. Specify the bucket and the folder to create.
// The folder key must end with a forward slash (/).
std::string BucketName = "examplebucket";
std::string DirName = "exampledir/";
// 3. Initialize SDK network resources.
InitializeSdk();
// 4. Build the OSSClient with Signature Version 4 and environment-variable credentials.
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
// 5. Upload a zero-byte object to create the folder marker.
std::shared_ptr<std::iostream> content = std::make_shared<std::stringstream>();
PutObjectRequest request(BucketName, DirName, content, ObjectMetaData());
auto outcome = client.PutObject(request);
if (!outcome.isSuccess()) {
std::cout << "PutObject fail"
<< ", code:" << outcome.error().Code()
<< ", message:" << outcome.error().Message()
<< ", requestId:" << outcome.error().RequestId()
<< std::endl;
ShutdownSdk();
return -1;
}
// 6. Release SDK network resources.
ShutdownSdk();
return 0;
}Delete a folder
Deleting a folder deletes all objects and subfolders inside it. Proceed with caution.
Deleting a folder requires two steps: list all objects whose key starts with the folder prefix, then delete them one by one. The following example deletes the exampledir/ folder and every object inside it.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
// 1. Set the endpoint, region, bucket, and the folder prefix to delete.
std::string Endpoint = "yourEndpoint";
std::string Region = "yourRegion";
std::string BucketName = "examplebucket";
std::string keyPrefix = "exampledir/";
// 2. Initialize SDK network resources.
InitializeSdk();
// 3. Build the OSSClient.
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
// 4. Page through all objects under the prefix and delete each one.
std::string nextMarker = "";
bool isTruncated = false;
do {
ListObjectsRequest listRequest(BucketName);
listRequest.setPrefix(keyPrefix);
listRequest.setMarker(nextMarker);
auto outcome = client.ListObjects(listRequest);
if (!outcome.isSuccess()) {
std::cout << "ListObjects fail"
<< ", code:" << outcome.error().Code()
<< ", message:" << outcome.error().Message()
<< ", requestId:" << outcome.error().RequestId()
<< std::endl;
break;
}
// Delete each object returned in this page.
for (const auto& object : outcome.result().ObjectSummarys()) {
DeleteObjectRequest delRequest(BucketName, object.Key());
client.DeleteObject(delRequest);
}
nextMarker = outcome.result().NextMarker();
isTruncated = outcome.result().IsTruncated();
} while (isTruncated);
// 5. Release SDK network resources.
ShutdownSdk();
return 0;
}See also
PutObject — API reference for creating a folder marker
DeleteObject — API reference for deleting an object