This topic describes how to list all objects and a specific number of objects in a bucket, and list the sizes of objects in a directory of a bucket.
Background information
Objects are listed alphabetically by object name. You can use OssClient.ListObjects to list the objects in a bucket. ListObjects supports the following types of parameter formats:
ListObjectOutcome ListObjects(const std::string& bucket) const: lists objects in a bucket. A maximum of 1,000 objects can be listed.
ListObjectOutcome ListObjects(const std::string& bucket, const std::string& prefix) const: lists objects whose names contain the specified prefix in a bucket. A maximum of 1,000 objects can be listed.
ListObjectOutcome ListObjects(const ListObjectsRequest& request) const: provides multiple filter conditions to query objects.
The following table describes the parameters that you can configure to list objects.
Parameter | Description |
delimiter | The character that is used to group objects by name. Objects whose names contain the same string that stretches from the specified prefix to the delimiter that appears for the first time are grouped as a commonPrefixes element. |
prefix | The prefix that must be included in the names of returned objects. |
maxKeys | The maximum number of objects that can be listed at a time. Maximum value: 1000. Default value: 100. |
marker | The position from which the list operation starts. All objects whose names are alphabetically after the value of marker are listed. |
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.
To list objects, you must have the
oss:ListObjects
permission. For more information, see Attach a custom policy to a RAM user.
Simple list
The following code provides an example on how to list objects in a bucket by using simple list. By default, a maximum of 100 objects are listed.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account that is used to access OSS. */
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize resources such as network resources. */
InitializeSdk();
ClientConfiguration conf;
/* 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);
/* 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() <<
",lastmodify time:" << object.LastModified() << std:: endl;
}
}
/* Release resources such as network resources. */
ShutdownSdk();
return 0;
}
List a specific number of objects
The following code provides an example on how to list a specific number of objects:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account that is used to access OSS. */
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize resources such as network resources. */
InitializeSdk();
ClientConfiguration conf;
/* 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);
/* List objects. */
ListObjectsRequest request(BucketName);
/* Set the maximum number of objects that you want to return 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() <<
",lastmodify time:" << object.LastModified() << std ::endl;
}
}
/* Release resources such as network resources. */
ShutdownSdk();
return 0;
}
List objects and subdirectories in a directory
The following code provides an example on how to list the objects and subdirectories in a directory of a bucket:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account that is used to access OSS. */
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the prefix that must be included in the names of the objects you want to list. */
std::string keyPrefix = "yourkeyPrefix";
/* Initialize resources such as network resources. */
InitializeSdk();
ClientConfiguration conf;
/* 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);
std::string nextMarker = "";
bool isTruncated = false;
do {
/* List objects. */
ListObjectsRequest request(BucketName);
/* Set the delimiter to a forward slash (/). */
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() <<
",lastmodify 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 resources such as network resources. */
ShutdownSdk();
return 0;
}
List the sizes of objects in a directory
The following code provides an example on how to list the sizes of objects in a directory:
#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 information about the account that is used to access OSS. */
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the prefix that must be included in the names of the objects you want to list. */
std::string keyPrefix = "yourkeyPrefix";
/* Initialize resources such as network resources. */
InitializeSdk();
ClientConfiguration conf;
/* 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);
std::string nextMarker = "";
bool isTruncated = false;
do {
/* List objects. */
ListObjectsRequest request(BucketName);
/* Set the delimiter to a forward slash (/). */
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 resources such as network resources. */
ShutdownSdk();
return 0;
}
List objects whose names contain the specified prefix
The following code provides an example on how to list objects whose names contain the specified prefix:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account that is used to access OSS. */
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the prefix that must be included in the names of the objects you want to list. */
std::string keyPrefix = "yourkeyPrefix";
/* Initialize resources such as network resources. */
InitializeSdk();
ClientConfiguration conf;
/* 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);
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() <<
",lastmodify time:" << object.LastModified() << std::endl;
}
nextMarker = outcome.result().NextMarker();
isTruncated = outcome.result().IsTruncated();
} while (isTruncated);
/* Release resources such as network resources. */
ShutdownSdk();
return 0;
}
List objects whose names are alphabetically after the value of marker
The name of the object from which the list operation starts is specified by the marker parameter. The following code provides an example on how to list objects whose names are alphabetically after the value of the marker parameter:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account that is used to access OSS. */
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the value of marker. */
std::string YourMarker = "yourMarker";
/* Initialize resources such as network resources. */
InitializeSdk();
ClientConfiguration conf;
/* 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);
ListObjectOutcome outcome;
do {
/* List objects. */
ListObjectsRequest request(BucketName);
/* List objects whose names are alphabetically after the value of 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() <<
",lastmodify time:" << object.LastModified() << std::endl;
}
} while (outcome.result().IsTruncated());
/* Release network resources. */
ShutdownSdk();
return 0;
}
List objects whose names are encoded by the specified encoding type
An object name that contains one of the following special characters needs to be encoded before the object is transmitted. Only URL encoding is supported in OSS.
Single quotation marks (')
Double quotation marks (")
Ampersands (&)
Angle brackets (<>)
Chinese characters
The following code provides an example on how to list objects whose names are encoded by the specified encoding type:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account that is used to access OSS. */
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
/* Specify the name of the bucket. Example: examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
/* 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);
std::string nextMarker = "";
bool isTruncated = false;
do {
/* List objects. */
ListObjectsRequest request(BucketName) ;
/* Specify the encoding type of the 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() <<
",lastmodify time:" << object.LastModified() << std::endl;
}
nextMarker = outcome.result().NextMarker();
isTruncated = outcome.result().IsTruncated();
} while (isTruncated);
/* Release resources such as network resources. */
ShutdownSdk();
return 0;
}
List objects in a directory
OSS uses a flat structure to store objects. A directory is a zero-byte object whose name ends with a forward slash (/). You can upload and download a directory. By default, an object whose name ends with a forward slash (/) is displayed as a directory in the OSS console.
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.
Assume that 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 on how to list all objects in a bucket:
#include <alibabacloud/oss/OssClient.h> using namespace AlibabaCloud::OSS; int main(void) { /* Initialize information about the account that is used to access OSS. */ /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */ std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; /* Specify the name of the bucket. Example: examplebucket. */ std::string BucketName = "examplebucket"; /* Initialize resources such as network resources. */ InitializeSdk(); ClientConfiguration conf; /* 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); 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() << ",lastmodify time:" << object.LastModified() << std::endl; } } nextMarker = outcome.result().NextMarker(); isTruncated = outcome.result().IsTruncated(); } while (isTruncated); /* Release resources such as network resources. */ ShutdownSdk(); return 0; }
The following objects are returned:
Objects: fun/movie/001.avi fun/movie/007.avi fun/test.jpg oss.jpg CommonPrefixes:
List all objects in a directory
The following code provides an example on how to list all objects in a directory:
#include <alibabacloud/oss/OssClient.h> using namespace AlibabaCloud::OSS; int main(void) { /* Initialize information about the account that is used to access OSS. */ /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */ std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; /* Specify the name of the bucket. Example: examplebucket. */ std::string BucketName = "examplebucket"; /* Initialize resources such as network resources. */ InitializeSdk(); ClientConfiguration conf; /* 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); 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() << ",lastmodify 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 resources such as network resources. */ ShutdownSdk(); return 0; }
The following objects are returned:
Objects: fun/movie/001.avi fun/movie/007.avi fun/test.jpg CommonPrefixes:
List objects and subdirectories in a directory
The following code provides an example on how to list the objects and subdirectories in a directory:
#include <alibabacloud/oss/OssClient.h> using namespace AlibabaCloud::OSS; int main(void) { /* Initialize information about the account that is used to access OSS. */ /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */ std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; /* Specify the name of the bucket. Example: examplebucket. */ std::string BucketName = "examplebucket"; /* Initialize resources such as network resources. */ InitializeSdk(); ClientConfiguration conf; /* 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); std::string nextMarker = ""; bool isTruncated = false; do { /* List objects. */ ListObjectsRequest request(BucketName); /* Set the delimiter to a forward slash (/). */ 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() << ",lastmodify 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 resources such as network resources. */ ShutdownSdk(); return 0; }
The following objects are returned:
Objects: fun/test.jpg CommonPrefixes: fun/movie/
List the sizes of objects in a directory
The following code provides an example on how to list the sizes of objects in a directory:
#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 information about the account that is used to access OSS. */ /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */ std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; /* Specify the name of the bucket. Example: examplebucket. */ std::string BucketName = "examplebucket"; /* Initialize network resources. */ InitializeSdk(); ClientConfiguration conf; /* 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); std::string nextMarker = ""; bool isTruncated = false; do { /* List objects. */ ListObjectsRequest request(BucketName); /* Set the delimiter to a forward slash (/). */ 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 resources such as network resources. */ ShutdownSdk(); return 0; }
References
For the complete sample code that is used to list objects, visit GitHub.
For more information about the API operations that you can call to list objects, see GetBucket (ListObjects) and ListObjectsV2 (GetBucketV2).