本文介紹如何列舉指定存儲空間(Bucket)的所有文件、指定個數(shù)的文件以及指定目錄下的文件大小等。
背景信息
OSS 文件按照字母順序排列。您可以通過OssClient.ListObjects列出存儲空間下的文件。ListObjects有以下三類參數(shù)格式:
ListObjectOutcome ListObjects(const std::string& bucket) const:列舉存儲空間下的文件。最多列舉1000個文件。
ListObjectOutcome ListObjects(const std::string& bucket, const std::string& prefix) const: 列舉存儲空間下指定前綴的文件。最多列舉 1000 個文件。
ListObjectOutcome ListObjects(const ListObjectsRequest& request) const:提供多種過濾功能,實現(xiàn)靈活的查詢功能。
主要參數(shù)及說明如下:
參數(shù) | 描述 |
delimiter | 對文件名稱進行分組的一個字符。所有名稱包含指定的前綴且第一次出現(xiàn)delimiter字符之間的文件作為一組(commonPrefixes)。 |
prefix | 限定返回的文件必須以prefix作為前綴。 |
maxKeys | 限定此次列舉文件的最大個數(shù)。默認值為100,最大值為1000。 |
marker | 列舉指定marker之后的文件。 |
注意事項
本文以華東1(杭州)外網Endpoint為例。如果您希望通過與OSS同地域的其他阿里云產品訪問OSS,請使用內網Endpoint。關于OSS支持的Region與Endpoint的對應關系,請參見OSS地域和訪問域名。
本文以OSS域名新建OSSClient為例。如果您希望通過自定義域名、STS等方式新建OSSClient,請參見新建OssClient。
要列舉文件,您必須有
oss:ListObjects
權限。具體操作,請參見為RAM用戶授權自定義的權限策略。
簡單列舉文件
以下代碼用于列舉指定存儲空間下的文件,默認列舉100個文件。
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 初始化OSS賬號信息。*/
/* yourEndpoint填寫B(tài)ucket所在地域對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
std::string Endpoint = "yourEndpoint";
/* yourRegion填寫B(tài)ucket所在地域對應的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/
std::string Region = "yourRegion";
/* 填寫B(tài)ucket名稱,例如examplebucket。*/
std::string BucketName = "examplebucket";
/* 初始化網絡等資源。*/
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 從環(huán)境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* 列舉文件。*/
ListObjectsRequest request(BucketName);
auto outcome = client.ListObjects(request);
if (!outcome.isSuccess()) {
/* 異常處理。*/
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;
}
}
/* 釋放網絡等資源。*/
ShutdownSdk();
return 0;
}
列舉指定個數(shù)的文件
以下代碼用于列舉指定個數(shù)的文件。
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 初始化OSS賬號信息。*/
/* yourEndpoint填寫B(tài)ucket所在地域對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
std::string Endpoint = "yourEndpoint";
/* yourRegion填寫B(tài)ucket所在地域對應的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/
std::string Region = "yourRegion";
/* 填寫B(tài)ucket名稱,例如examplebucket。*/
std::string BucketName = "examplebucket";
/* 初始化網絡等資源。*/
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 從環(huán)境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* 列舉文件。*/
ListObjectsRequest request(BucketName);
/* 設置列舉文件的最大個數(shù)為200。*/
request.setMaxKeys(200);
auto outcome = client.ListObjects(request);
if (!outcome.isSuccess( )) {
/* 異常處理。*/
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;
}
}
/* 釋放網絡等資源。*/
ShutdownSdk();
return 0;
}
列舉指定目錄下的文件和子目錄
以下代碼用于列舉指定目錄下的文件和子目錄:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 初始化OSS賬號信息。*/
/* yourEndpoint填寫B(tài)ucket所在地域對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
std::string Endpoint = "yourEndpoint";
/* yourRegion填寫B(tài)ucket所在地域對應的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/
std::string Region = "yourRegion";
/* 填寫B(tài)ucket名稱,例如examplebucket。*/
std::string BucketName = "examplebucket";
/* 填寫列舉的文件前綴(Prefix)。*/
std::string keyPrefix = "yourkeyPrefix";
/* 初始化網絡等資源。*/
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 從環(huán)境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
std::string nextMarker = "";
bool isTruncated = false;
do {
/* 列舉文件。*/
ListObjectsRequest request(BucketName);
/* 設置正斜線(/)為文件夾的分隔符 */
request.setDelimiter("/");
request.setPrefix(keyPrefix);
request.setMarker(nextMarker);
auto outcome = client.ListObjects(request);
if (!outcome.isSuccess ()) {
/* 異常處理。*/
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);
/* 釋放網絡等資源。*/
ShutdownSdk();
return 0;
}
列舉指定目錄下的文件大小
以下代碼用于獲取指定目錄下的文件大小:
#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 {
/* 列舉文件。*/
ListObjectsRequest request(bucketName);
request.setPrefix(folder);
request.setMarker(nextMarker);
auto outcome = client.ListObjects(request);
if (!outcome.isSuccess()) {
/* 異常處理 */
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)
{
/* 初始化OSS賬號信息。*/
/* yourEndpoint填寫B(tài)ucket所在地域對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
std::string Endpoint = "yourEndpoint";
/* yourRegion填寫B(tài)ucket所在地域對應的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/
std::string Region = "yourRegion";
/* 填寫B(tài)ucket名稱,例如examplebucket。*/
std::string BucketName = "examplebucket";
/* 填寫列舉的文件前綴(Prefix)。*/
std::string keyPrefix = "yourkeyPrefix";
/* 初始化網絡等資源。*/
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 從環(huán)境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
std::string nextMarker = "";
bool isTruncated = false;
do {
/* 列舉文件。*/
ListObjectsRequest request(BucketName);
/* 設置正斜線(/)為文件夾的分隔符。*/
request.setDelimiter("/");
request.setPrefix(keyPrefix);
request.setMarker(nextMarker);
auto outcome = client.ListObjects(request);
if (!outcome.isSuccess()) {
/* 異常處理。*/
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);
/* 釋放網絡等資源。*/
ShutdownSdk();
return 0;
}
列舉指定前綴的文件
以下代碼用于列舉包含指定前綴(prefix)的文件。
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 初始化OSS賬號信息。*/
/* yourEndpoint填寫B(tài)ucket所在地域對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
std::string Endpoint = "yourEndpoint";
/* yourRegion填寫B(tài)ucket所在地域對應的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/
std::string Region = "yourRegion";
/* 填寫B(tài)ucket名稱,例如examplebucket。*/
std::string BucketName = "examplebucket";
/* 填寫列舉的文件前綴(Prefix)。*/
std::string keyPrefix = "yourkeyPrefix";
/* 初始化網絡等資源。*/
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 從環(huán)境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
std::string nextMarker = "";
bool isTruncated = false;
do {
/* 列舉文件。*/
ListObjectsRequest request(BucketName);
request.setPrefix(keyPrefix);
request.setMarker(nextMarker);
auto outcome = client.ListObjects(request);
if (!outcome.isSuccess()) {
/* 異常處理。*/
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);
/* 釋放網絡等資源。*/
ShutdownSdk();
return 0;
}
列舉指定marker之后的文件
參數(shù)marker代表文件名稱。以下代碼用于列舉指定marker之后的文件。
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 初始化OSS賬號信息。*/
/* yourEndpoint填寫B(tài)ucket所在地域對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
std::string Endpoint = "yourEndpoint";
/* yourRegion填寫B(tài)ucket所在地域對應的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/
std::string Region = "yourRegion";
/* 填寫B(tài)ucket名稱,例如examplebucket。*/
std::string BucketName = "examplebucket";
/* 設置marker。*/
std::string YourMarker = "yourMarker";
/* 初始化網絡等資源。*/
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 從環(huán)境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
ListObjectOutcome outcome;
do {
/* 列舉文件 */
ListObjectsRequest request(BucketName);
/* 列舉指定marker之后的文件 */
request.setMarker(YourMarker);
outcome = client.ListObjects(request);
if (!outcome.isSuccess()) {
/* 異常處理 */
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());
/* 釋放網絡等資源 */
ShutdownSdk();
return 0;
}
指定文件名稱編碼
如果文件名稱含有以下特殊字符,需要進行編碼傳輸。OSS目前僅支持URL編碼。
單引號('')
雙引號("")
and符號(&)
尖括號(< >)
頓號(、)
中文
以下代碼用于指定文件名稱編碼:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* 初始化OSS賬號信息。*/
/* yourEndpoint填寫B(tài)ucket所在地域對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
std::string Endpoint = "yourEndpoint";
/* yourRegion填寫B(tài)ucket所在地域對應的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/
std::string Region = "yourRegion";
/* 填寫B(tài)ucket名稱,例如examplebucket。*/
std::string BucketName = "examplebucket";
/* 初始化網絡等資源 */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* 從環(huán)境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
std::string nextMarker = "";
bool isTruncated = false;
do {
/* 列舉文件。*/
ListObjectsRequest request(BucketName) ;
/* 指定文件名稱編碼。*/
request.setEncodingType("url");
request.setMarker(nextMarker);
auto outcome = client.ListObjects(request);
if (!outcome.isSuccess() ) {
/* 異常處理。*/
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);
/* 釋放網絡等資源。*/
ShutdownSdk();
return 0;
}
文件夾功能
OSS沒有文件夾的概念,所有元素都是以文件來存儲。創(chuàng)建文件夾本質上來說是創(chuàng)建了一個大小為0并以正斜線(/)結尾的文件。這個文件可以被上傳和下載,控制臺會對以正斜線(/)結尾的文件以文件夾的方式展示。
通過delimiter和prefix兩個參數(shù)可以模擬文件夾功能:
如果設置prefix為某個文件夾名稱,則會列舉以此prefix開頭的文件,即該文件夾下所有的文件和子文件夾(目錄)均顯示為objects。
如果在設置了prefix的情況下,將delimiter設置為正斜線(/),則只列舉該文件夾下的文件和子文件夾(目錄),該文件夾下的子文件夾(目錄)顯示為CommonPrefixes,子文件夾下的文件和文件夾不顯示。
假設存儲空間中包含文件oss.jpg
、fun/test.jpg
、fun/movie/001.avi
和fun/movie/007.avi
,以正斜線(/)作為文件夾的分隔符。以下示例說明了如何通過模擬文件夾的方式列舉文件。
列舉存儲空間下所有文件
以下代碼用于列舉指定存儲空間下的所有文件。
#include <alibabacloud/oss/OssClient.h> using namespace AlibabaCloud::OSS; int main(void) { /* 初始化OSS賬號信息。*/ /* yourEndpoint填寫Bucket所在地域對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/ std::string Endpoint = "yourEndpoint"; /* yourRegion填寫Bucket所在地域對應的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/ std::string Region = "yourRegion"; /* 填寫Bucket名稱,例如examplebucket。*/ std::string BucketName = "examplebucket"; /* 初始化網絡等資源。*/ InitializeSdk(); ClientConfiguration conf; conf.signatureVersion = SignatureVersionType::V4; /* 從環(huán)境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/ auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>(); OssClient client(Endpoint, credentialsProvider, conf); client.SetRegion(Region); std::string nextMarker = ""; bool isTruncated = false; do { /* 列舉文件。*/ ListObjectsRequest request(BucketName); request.setMarker(nextMarker); auto outcome = client.ListObjects(request); if (!outcome.isSuccess()) { /* 異常處理。*/ 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); /* 釋放網絡等資源。*/ ShutdownSdk(); return 0; }
返回結果如下:
Objects: fun/movie/001.avi fun/movie/007.avi fun/test.jpg oss.jpg CommonPrefixes:
列舉指定目錄下所有文件
以下代碼用于列舉指定目錄下的所有文件。
#include <alibabacloud/oss/OssClient.h> using namespace AlibabaCloud::OSS; int main(void) { /* 初始化OSS賬號信息。*/ /* yourEndpoint填寫Bucket所在地域對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/ std::string Endpoint = "yourEndpoint"; /* yourRegion填寫Bucket所在地域對應的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/ std::string Region = "yourRegion"; /* 填寫Bucket名稱,例如examplebucket。*/ std::string BucketName = "examplebucket"; /* 初始化網絡等資源。*/ InitializeSdk(); ClientConfiguration conf; conf.signatureVersion = SignatureVersionType::V4; /* 從環(huán)境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/ auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>(); OssClient client(Endpoint, credentialsProvider, conf); client.SetRegion(Region); std::string nextMarker = ""; bool isTruncated = false; do { /* 列舉文件。*/ ListObjectsRequest request(BucketName); request.setPrefix("fun/"); request.setMarker(nextMarker); auto outcome = client.ListObjects(request); if (!outcome.isSuccess()) { /* 異常處理。*/ 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); /* 釋放網絡等資源。*/ ShutdownSdk(); return 0; }
返回結果如下:
Objects: fun/movie/001.avi fun/movie/007.avi fun/test.jpg CommonPrefixes:
列舉目錄下的文件和子目錄
以下代碼用于列舉指定目錄下的文件和子目錄。
#include <alibabacloud/oss/OssClient.h> using namespace AlibabaCloud::OSS; int main(void) { /* 初始化OSS賬號信息。*/ /* yourEndpoint填寫Bucket所在地域對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/ std::string Endpoint = "yourEndpoint"; /* yourRegion填寫Bucket所在地域對應的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/ std::string Region = "yourRegion"; /* 填寫Bucket名稱,例如examplebucket。*/ std::string BucketName = "examplebucket"; /* 初始化網絡等資源。*/ InitializeSdk(); ClientConfiguration conf; conf.signatureVersion = SignatureVersionType::V4; /* 從環(huán)境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/ auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>(); OssClient client(Endpoint, credentialsProvider, conf); client.SetRegion(Region); std::string nextMarker = ""; bool isTruncated = false; do { /* 列舉文件。*/ ListObjectsRequest request(BucketName); /* 設置正斜線(/)為文件夾的分隔符 */ request.setDelimiter("/"); request.setPrefix("fun/"); request.setMarker(nextMarker); auto outcome = client.ListObjects(request); if (!outcome.isSuccess()) { /* 異常處理。*/ 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); /* 釋放網絡等資源。*/ ShutdownSdk(); return 0; }
返回結果如下:
Objects: fun/test.jpg CommonPrefixes: fun/movie/
列舉指定目錄下的文件大小
以下代碼用于獲取指定目錄下的文件大小:
#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 { /* 列舉文件。*/ ListObjectsRequest request(bucketName); request.setPrefix(folder); request.setMarker(nextMarker); auto outcome = client.ListObjects(request); if (!outcome.isSuccess()) { /* 異常處理。*/ 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) { /* 初始化OSS賬號信息。*/ /* yourEndpoint填寫Bucket所在地域對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/ std::string Endpoint = "yourEndpoint"; /* yourRegion填寫Bucket所在地域對應的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/ std::string Region = "yourRegion"; /* 填寫Bucket名稱,例如examplebucket。*/ std::string BucketName = "examplebucket"; /* 初始化網絡等資源 */ InitializeSdk(); ClientConfiguration conf; conf.signatureVersion = SignatureVersionType::V4; /* 從環(huán)境變量中獲取訪問憑證。運行本代碼示例之前,請確保已設置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/ auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>(); OssClient client(Endpoint, credentialsProvider, conf); client.SetRegion(Region); std::string nextMarker = ""; bool isTruncated = false; do { /* 列舉文件。*/ ListObjectsRequest request(BucketName); /* 設置正斜線(/)為文件夾的分隔符。*/ request.setDelimiter("/"); request.setPrefix("fun/"); request.setMarker(nextMarker); auto outcome = client.ListObjects(request); if (!outcome.isSuccess()) { /* 異常處理。*/ 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); /* 釋放網絡等資源。*/ ShutdownSdk(); return 0; }
相關文檔
關于列舉文件的完整示例代碼,請參見GitHub示例。
關于列舉文件的API接口說明,請參見GetBucket (ListObjects)和ListObjectsV2(GetBucketV2)。