List objects (C++ SDK V2)
List objects in an OSS bucket by using the OSS C++ SDK V2, with support for prefix filtering and automatic pagination.
Usage notes
-
Before you run the sample code, replace
<region>with an actual region, such ascn-hangzhou. -
The sample code retrieves access credentials from environment variables.
-
To list objects, you must have the
oss:ListObjectsV2permission.
Sample code
List objects in a single request (ListObjectsV2)
The following example calls ListObjectsV2 once to list objects. A single call returns up to maxKeys objects (default: 100, maximum: 1000). To retrieve all objects, use the paginator pattern described later.
#include <iostream>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"
namespace oss = alibabacloud::oss2;
int main() {
auto conf = oss::ClientConfiguration::loadDefault();
conf.region = "<region>";
conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();
oss::OSSClient client(conf);
auto outcome = client.listObjectsV2(
oss::models::ListObjectsV2Request()
.setBucket("examplebucket")
.setMaxKeys(100)
.setPrefix("exampledir/"));
if (!outcome.has_value()) {
auto& e = outcome.error();
std::cerr << "Failed to list objects"
<< ", code: " << e.getCode()
<< ", message: " << e.getMessage()
<< ", requestId: " << e.getRequestId() << std::endl;
return 1;
}
auto& result = outcome.value();
std::cout << "status: " << result.getStatusCode()
<< ", keyCount: " << result.getKeyCount()
<< ", isTruncated: " << result.getIsTruncated() << std::endl;
for (const auto& obj : result.getContents()) {
std::cout << "Object key: " << obj.key
<< ", size: " << obj.size
<< ", lastModified: " << obj.lastModified << std::endl;
}
return 0;
}
List objects with a specified prefix
The following example lists objects with a specified prefix. The delimiter parameter groups results by common prefixes, simulating subdirectory listing.
#include <iostream>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"
namespace oss = alibabacloud::oss2;
int main() {
auto conf = oss::ClientConfiguration::loadDefault();
conf.region = "<region>";
conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();
oss::OSSClient client(conf);
auto outcome = client.listObjectsV2(
oss::models::ListObjectsV2Request()
.setBucket("examplebucket")
.setPrefix("exampledir/")
.setDelimiter("/")
.setMaxKeys(100));
if (!outcome.has_value()) {
auto& e = outcome.error();
std::cerr << "Failed to list objects"
<< ", code: " << e.getCode()
<< ", message: " << e.getMessage()
<< ", requestId: " << e.getRequestId() << std::endl;
return 1;
}
auto& result = outcome.value();
// Objects
std::cout << "Objects:" << std::endl;
for (const auto& obj : result.getContents()) {
std::cout << " " << obj.key << std::endl;
}
// Subdirectories (CommonPrefixes)
std::cout << "Subdirectories:" << std::endl;
for (const auto& cp : result.getCommonPrefixes()) {
std::cout << " " << cp.prefix << std::endl;
}
return 0;
}
List all objects with a paginator
A paginator automatically handles pagination and iterates through all objects in a bucket, eliminating the need to manually manage the ContinuationToken.
#include <iostream>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/Paginator.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"
namespace oss = alibabacloud::oss2;
int main() {
auto conf = oss::ClientConfiguration::loadDefault();
conf.region = "<region>";
conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();
oss::OSSClient client(conf);
auto paginator = oss::makePaginator(client,
oss::models::ListObjectsV2Request()
.setBucket("examplebucket")
.setMaxKeys(100));
int pageNum = 0;
int totalObjects = 0;
while (paginator.hasNext()) {
auto outcome = paginator.nextPage();
if (!outcome.has_value()) {
auto& e = outcome.error();
std::cerr << "Failed to list objects"
<< ", code: " << e.getCode()
<< ", message: " << e.getMessage()
<< ", requestId: " << e.getRequestId() << std::endl;
return 1;
}
auto& result = outcome.value();
pageNum++;
std::cout << "Page " << pageNum << ", keyCount: " << result.getKeyCount() << std::endl;
for (const auto& obj : result.getContents()) {
std::cout << " " << obj.key << ", size: " << obj.size << std::endl;
totalObjects++;
}
}
std::cout << "Total: " << totalObjects << " objects in " << pageNum << " pages" << std::endl;
return 0;
}
References
-
For the complete sample code for listing objects in a single request, see ListObjectsV2.cpp.
-
For the complete sample code for listing all objects with a paginator, see ListObjectsV2Paginator.cpp.