This topic describes how to list all objects in a specified bucket, list a specified number of objects, and list the total size of objects in a specified folder.
Background information
OSS objects are sorted alphabetically. You can use OssClient.ListObjects to list objects in a bucket. ListObjects has the following three parameter formats:
ListObjectOutcome ListObjects(const std::string& bucket) const: Lists the objects in a bucket. A maximum of 1,000 objects can be returned in a single request.
ListObjectOutcome ListObjects(const std::string& bucket, const std::string& prefix) const: Lists the objects that have a specified prefix in a bucket. A maximum of 1,000 objects can be returned in a single request.
ListObjectOutcome ListObjects(const ListObjectsRequest& request) const: Provides multiple filtering features for flexible queries.
The following table describes the main parameters.
Parameter | Description |
delimiter | A character used to group object names. All objects whose names contain the specified prefix and appear between the prefix and the first occurrence of the delimiter are grouped as a set (commonPrefixes). |
prefix | Specifies that the returned objects must have the specified prefix. |
maxKeys | The maximum number of objects to return in this request. The default value is 100. The maximum value is 1,000. |
marker | Lists objects that appear after the specified marker. |
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.
To list objects, you must have the
oss:ListObjectspermission. For more information, see Attach a custom policy to a RAM user.
List objects
The following code provides an example of how to list objects in a specified bucket. By default, 100 objects are returned.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the OSS account information. */
/* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* List objects. */
ListObjectsRequest request(BucketName);
auto outcome = client.ListObjects(request);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "ListObjects fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
else {
for (const auto& object : outcome.result().ObjectSummarys()) {
std::cout << "object"<<
",name:" << object.Key() <<
",size:" << object.Size() <<
",last modified time:" << object.LastModified() << std:: endl;
}
}
/* Release network resources. */
ShutdownSdk();
return 0;
}List a specified number of objects
The following code provides an example of how to list a specified number of objects.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the OSS account information. */
/* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* List objects. */
ListObjectsRequest request(BucketName);
/* Set the maximum number of objects to list to 200. */
request.setMaxKeys(200);
auto outcome = client.ListObjects(request);
if (!outcome.isSuccess( )) {
/* Handle exceptions. */
std::cout << "ListObjects fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
else {
for (const auto& object : outcome.result().ObjectSummarys()) {
std::cout << "object"<<
",name:" << object.Key() <<
",size:" << object.Size() <<
",last modified time:" << object.LastModified() << std ::endl;
}
}
/* Release network resources. */
ShutdownSdk();
return 0;
}List objects and subfolders in a specified folder
The following code provides an example of how to list the objects and subfolders in a specified folder:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the OSS account information. */
/* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the prefix of the objects to list. */
std::string keyPrefix = "yourkeyPrefix";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
std::string nextMarker = "";
bool isTruncated = false;
do {
/* List objects. */
ListObjectsRequest request(BucketName);
/* Set the forward slash (/) as the folder separator. */
request.setDelimiter("/");
request.setPrefix(keyPrefix);
request.setMarker(nextMarker);
auto outcome = client.ListObjects(request);
if (!outcome.isSuccess ()) {
/* Handle exceptions. */
std::cout << "ListObjects fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
break;
}
for (const auto& object : outcome.result().ObjectSummarys()) {
std::cout << "object"<<
",name:" << object.Key() <<
",size:" << object.Size() <<
",last modified time:" << object.LastModified() << std::endl;
}
for (const auto& commonPrefix : outcome.result().CommonPrefixes()) {
std::cout << "commonPrefix" << ",name:" << commonPrefix << std::endl;
}
nextMarker = outcome.result().NextMarker();
isTruncated = outcome.result().IsTruncated();
} while (isTruncated);
/* Release network resources. */
ShutdownSdk();
return 0;
}List the size of objects in a specified folder
The following code provides an example of how to obtain the total size of objects in a specified folder:
#include <iostream>
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
static int64_t calculateFolderLength(const OssClient &client, const std::string &bucketName, const std::string &folder)
{
std::string nextMarker = "";
bool isTruncated = false;
int64_t size = 0;
do {
/* List objects. */
ListObjectsRequest request(bucketName);
request.setPrefix(folder);
request.setMarker(nextMarker);
auto outcome = client.ListObjects(request);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "ListObjects fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
break;
}
for (const auto& object : outcome.result().ObjectSummarys()) {
size += object.Size();
}
nextMarker = outcome.result().NextMarker();
isTruncated = outcome.result().IsTruncated();
} while (isTruncated);
return size;
}
int main(void)
{
/* Initialize the OSS account information. */
/* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the prefix of the objects to list. */
std::string keyPrefix = "yourkeyPrefix";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
std::string nextMarker = "";
bool isTruncated = false;
do {
/* List objects. */
ListObjectsRequest request(BucketName);
/* Set the forward slash (/) as the folder separator. */
request.setDelimiter("/");
request.setPrefix(keyPrefix);
request.setMarker(nextMarker);
auto outcome = client.ListObjects(request);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "ListObjects fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
break;
}
for (const auto& object : outcome.result().ObjectSummarys()) {
std::cout << "object" <<
",name:" << object.Key() <<
",size:" << object.Size() << std::endl;
}
for (const auto& commonPrefix : outcome.result().CommonPrefixes()) {
int64_t foldersize = calculateFolderLength(client, BucketName, commonPrefix);
std::cout << "folder" <<
",name:" << commonPrefix <<
",size:" << foldersize << std::endl;
}
nextMarker = outcome.result().NextMarker();
isTruncated = outcome.result().IsTruncated();
} while (isTruncated);
/* Release network resources. */
ShutdownSdk();
return 0;
}List objects with a specified prefix
The following code provides an example of how to list objects that have a specified prefix.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the OSS account information. */
/* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the prefix of the objects to list. */
std::string keyPrefix = "yourkeyPrefix";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
std::string nextMarker = "";
bool isTruncated = false;
do {
/* List objects. */
ListObjectsRequest request(BucketName);
request.setPrefix(keyPrefix);
request.setMarker(nextMarker);
auto outcome = client.ListObjects(request);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "ListObjects fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
break;
}
for (const auto& object : outcome.result().ObjectSummarys()) {
std::cout << "object"<<
",name:" << object.Key() <<
",size:" << object.Size() <<
",last modified time:" << object.LastModified() << std::endl;
}
nextMarker = outcome.result().NextMarker();
isTruncated = outcome.result().IsTruncated();
} while (isTruncated);
/* Release network resources. */
ShutdownSdk();
return 0;
}List objects after a specified marker
The marker parameter specifies an object name. The following code provides an example of how to list objects that appear after a specified marker.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the OSS account information. */
/* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Set the marker. */
std::string YourMarker = "yourMarker";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
ListObjectOutcome outcome;
do {
/* List objects. */
ListObjectsRequest request(BucketName);
/* List objects that appear after the specified marker. */
request.setMarker(YourMarker);
outcome = client.ListObjects(request);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "ListObjects fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
break;
}
YourMarker = outcome.result().NextMarker();
for (const auto& object : outcome.result().ObjectSummarys()) {
std::cout << "object"<<
",name:" << object.Key() <<
",size:" << object.Size() <<
",last modified time:" << object.LastModified() << std::endl;
}
} while (outcome.result().IsTruncated());
/* Release network resources. */
ShutdownSdk();
return 0;
}Specify the encoding of object names
If an object name contains special characters, you must URL-encode the object name for transmission. OSS supports only URL encoding.
Single quotation marks (')
Double quotation marks ("")
Ampersands (&)
Angle brackets (< >)
Chinese
The following code provides an example of how to specify the encoding for object names:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the OSS account information. */
/* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
std::string nextMarker = "";
bool isTruncated = false;
do {
/* List objects. */
ListObjectsRequest request(BucketName) ;
/* Specify the encoding of object names. */
request.setEncodingType("url");
request.setMarker(nextMarker);
auto outcome = client.ListObjects(request);
if (!outcome.isSuccess() ) {
/* Handle exceptions. */
std::cout << "ListObjects fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
break;
}
for (const auto& object : outcome.result().ObjectSummarys()) {
std::cout << "object"<<
",name:" << object.Key() <<
",size:" << object.Size() <<
",last modified time:" << object.LastModified() << std::endl;
}
nextMarker = outcome.result().NextMarker();
isTruncated = outcome.result().IsTruncated();
} while (isTruncated);
/* Release network resources. */
ShutdownSdk();
return 0;
}Folder feature
OSS does not have the concept of folders. All elements are stored as objects. Creating a folder is equivalent to creating a zero-byte object that ends with a forward slash (/). This object can be uploaded and downloaded. The OSS console displays objects that end with a forward slash (/) as folders.
You can specify the delimiter and prefix parameters to list objects by directory.
If you set prefix to a directory name in the request, the objects and subdirectories whose names contain the prefix are listed.
If you specify a prefix and set delimiter to a forward slash (/) in the request, the objects and subdirectories whose names start with the specified prefix in the directory are listed. Each subdirectory is listed as a single result element in CommonPrefixes. The objects and directories in these subdirectories are not listed.
For example, a bucket contains the following objects: oss.jpg, fun/test.jpg, fun/movie/001.avi, and fun/movie/007.avi. The forward slash (/) is specified as the directory delimiter. The following examples describe how to list objects in simulated directories.
List all objects in a bucket
The following code provides an example of how to list all objects in a specified bucket.
#include <alibabacloud/oss/OssClient.h> using namespace AlibabaCloud::OSS; int main(void) { /* Initialize the OSS account information. */ /* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */ std::string Endpoint = "yourEndpoint"; /* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */ std::string Region = "yourRegion"; /* Specify the bucket name. Example: examplebucket. */ std::string BucketName = "examplebucket"; /* Initialize network resources. */ InitializeSdk(); ClientConfiguration conf; conf.signatureVersion = SignatureVersionType::V4; /* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */ auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>(); OssClient client(Endpoint, credentialsProvider, conf); client.SetRegion(Region); std::string nextMarker = ""; bool isTruncated = false; do { /* List objects. */ ListObjectsRequest request(BucketName); request.setMarker(nextMarker); auto outcome = client.ListObjects(request); if (!outcome.isSuccess()) { /* Handle exceptions. */ std::cout << "ListObjects fail" << ",code:" << outcome.error().Code() << ",message:" << outcome.error().Message() << ",requestId:" << outcome.error().RequestId() << std::endl; ShutdownSdk(); return -1; } else { for (const auto& object : outcome.result().ObjectSummarys()) { std::cout << "object"<< ",name:" << object.Key() << ",size:" << object.Size() << ",last modified time:" << object.LastModified() << std::endl; } } nextMarker = outcome.result().NextMarker(); isTruncated = outcome.result().IsTruncated(); } while (isTruncated); /* Release network resources. */ ShutdownSdk(); return 0; }The following result is returned:
Objects: fun/movie/001.avi fun/movie/007.avi fun/test.jpg oss.jpg CommonPrefixes:List all objects in a specified folder
The following code provides an example of how to list all objects in a specified folder.
#include <alibabacloud/oss/OssClient.h> using namespace AlibabaCloud::OSS; int main(void) { /* Initialize the OSS account information. */ /* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */ std::string Endpoint = "yourEndpoint"; /* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */ std::string Region = "yourRegion"; /* Specify the bucket name. Example: examplebucket. */ std::string BucketName = "examplebucket"; /* Initialize network resources. */ InitializeSdk(); ClientConfiguration conf; conf.signatureVersion = SignatureVersionType::V4; /* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */ auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>(); OssClient client(Endpoint, credentialsProvider, conf); client.SetRegion(Region); std::string nextMarker = ""; bool isTruncated = false; do { /* List objects. */ ListObjectsRequest request(BucketName); request.setPrefix("fun/"); request.setMarker(nextMarker); auto outcome = client.ListObjects(request); if (!outcome.isSuccess()) { /* Handle exceptions. */ std::cout << "ListObjects fail" << ",code:" << outcome.error().Code() << ",message:" << outcome.error().Message() << ",requestId:" << outcome.error().RequestId() << std::endl; break; } for (const auto& object : outcome.result().ObjectSummarys()) { std::cout << "object"<< ",name:" << object.Key() << ",size:" << object.Size() << ",last modified time:" << object.LastModified() << std::endl; } for (const auto& commonPrefix : outcome.result().CommonPrefixes()) { std::cout << "commonPrefix" << ",name:" << commonPrefix << std::endl; } nextMarker = outcome.result().NextMarker(); isTruncated = outcome.result().IsTruncated(); } while (isTruncated); /* Release network resources. */ ShutdownSdk(); return 0; }The following result is returned:
Objects: fun/movie/001.avi fun/movie/007.avi fun/test.jpg CommonPrefixes:List objects and subfolders in a folder
The following code provides an example of how to list the objects and subfolders in a specified folder.
#include <alibabacloud/oss/OssClient.h> using namespace AlibabaCloud::OSS; int main(void) { /* Initialize the OSS account information. */ /* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */ std::string Endpoint = "yourEndpoint"; /* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */ std::string Region = "yourRegion"; /* Specify the bucket name. Example: examplebucket. */ std::string BucketName = "examplebucket"; /* Initialize network resources. */ InitializeSdk(); ClientConfiguration conf; conf.signatureVersion = SignatureVersionType::V4; /* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */ auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>(); OssClient client(Endpoint, credentialsProvider, conf); client.SetRegion(Region); std::string nextMarker = ""; bool isTruncated = false; do { /* List objects. */ ListObjectsRequest request(BucketName); /* Set the forward slash (/) as the folder separator. */ request.setDelimiter("/"); request.setPrefix("fun/"); request.setMarker(nextMarker); auto outcome = client.ListObjects(request); if (!outcome.isSuccess()) { /* Handle exceptions. */ std::cout << "ListObjects fail" << ",code:" << outcome.error().Code() << ",message:" << outcome.error().Message() << ",requestId:" << outcome.error().RequestId() << std::endl; break; } for (const auto& object : outcome.result().ObjectSummarys()) { std::cout << "object"<< ",name:" << object.Key() << ",size:" << object.Size() << ",last modified time:" << object.LastModified() << std::endl; } for (const auto& commonPrefix : outcome.result().CommonPrefixes()) { std::cout << "commonPrefix" << ",name:" << commonPrefix << std::endl; } nextMarker = outcome.result().NextMarker(); isTruncated = outcome.result().IsTruncated(); } while (isTruncated); /* Release network resources. */ ShutdownSdk(); return 0; }The following result is returned:
Objects: fun/test.jpg CommonPrefixes: fun/movie/List the size of objects in a specified folder
The following code provides an example of how to obtain the total size of objects in a specified folder:
#include <alibabacloud/oss/OssClient.h> using namespace AlibabaCloud::OSS; static int64_t calculateFolderLength (const OssClient &client, const std::string &bucketName, const std::string &folder) { std::string nextMarker = ""; bool isTruncated = false; int64_t size = 0; do { /* List objects. */ ListObjectsRequest request(bucketName); request.setPrefix(folder); request.setMarker(nextMarker); auto outcome = client.ListObjects(request); if (!outcome.isSuccess()) { /* Handle exceptions. */ std::cout << "ListObjects fail" << ",code:" << outcome.error().Code() << ",message:" << outcome.error().Message() << ",requestId:" << outcome.error().RequestId() << std::endl; break; } for (const auto& object : outcome.result().ObjectSummarys()) { size += object.Size(); } nextMarker = outcome.result().NextMarker(); isTruncated = outcome.result().IsTruncated(); } while (isTruncated); return size; } int main(void) { /* Initialize the OSS account information. */ /* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */ std::string Endpoint = "yourEndpoint"; /* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */ std::string Region = "yourRegion"; /* Specify the bucket name. Example: examplebucket. */ std::string BucketName = "examplebucket"; /* Initialize network resources. */ InitializeSdk(); ClientConfiguration conf; conf.signatureVersion = SignatureVersionType::V4; /* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */ auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>(); OssClient client(Endpoint, credentialsProvider, conf); client.SetRegion(Region); std::string nextMarker = ""; bool isTruncated = false; do { /* List objects. */ ListObjectsRequest request(BucketName); /* Set the forward slash (/) as the folder separator. */ request.setDelimiter("/"); request.setPrefix("fun/"); request.setMarker(nextMarker); auto outcome = client.ListObjects(request); if (!outcome.isSuccess()) { /* Handle exceptions. */ std::cout << "ListObjects fail" << ",code:" << outcome.error().Code() << ",message:" << outcome.error().Message() << ",requestId:" << outcome.error().RequestId() << std::endl; break; } for (const auto& object : outcome.result().ObjectSummarys()) { std::cout << "object" << ",name:" << object.Key() << ",size:" << object.Size() << std::endl; } for (const auto& commonPrefix : outcome.result().CommonPrefixes()) { int64_t foldersize = calculateFolderLength(client, BucketName, commonPrefix); std::cout << "folder" << ",name:" << commonPrefix << ",size:" << foldersize << std::endl; } nextMarker = outcome.result().NextMarker(); isTruncated = outcome.result().IsTruncated(); } while (isTruncated); /* Release network resources. */ ShutdownSdk(); return 0; }
References
For the complete sample code that is used to list objects, see the GitHub example.
For more information about the API operations that are used to list objects, see GetBucket (ListObjects) and ListObjectsV2 (GetBucketV2).