C列舉文件
本文介紹如何列舉指定存儲(chǔ)空間下(Bucket)的所有文件(Object)、指定個(gè)數(shù)的文件、指定前綴的文件等。
注意事項(xiàng)
本文以華東1(杭州)外網(wǎng)Endpoint為例。如果您希望通過(guò)與OSS同地域的其他阿里云產(chǎn)品訪問(wèn)OSS,請(qǐng)使用內(nèi)網(wǎng)Endpoint。關(guān)于OSS支持的Region與Endpoint的對(duì)應(yīng)關(guān)系,請(qǐng)參見(jiàn)OSS地域和訪問(wèn)域名。
本文以OSS域名新建OSSClient為例。如果您希望通過(guò)自定義域名、STS等方式新建OSSClient,請(qǐng)參見(jiàn)初始化。
要列舉文件,您必須有
oss:ListObjects
權(quán)限。具體操作,請(qǐng)參見(jiàn)為RAM用戶授權(quán)自定義的權(quán)限策略。
列舉所有文件
以下代碼用于列出當(dāng)前存儲(chǔ)空間下的所有文件。
#include "oss_api.h"
#include "aos_http_io.h"
/* yourEndpoint填寫B(tài)ucket所在地域?qū)?yīng)的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
const char *endpoint = "yourEndpoint";
/* 填寫B(tài)ucket名稱,例如examplebucket。*/
const char *bucket_name = "examplebucket";
/* yourRegion填寫B(tài)ucket所在地域?qū)?yīng)的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/
const char *region = "yourRegion";
void init_options(oss_request_options_t *options)
{
options->config = oss_config_create(options->pool);
/* 用char*類型的字符串初始化aos_string_t類型。*/
aos_str_set(&options->config->endpoint, endpoint);
/* 從環(huán)境變量中獲取訪問(wèn)憑證。運(yùn)行本代碼示例之前,請(qǐng)確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
aos_str_set(&options->config->access_key_id, getenv("OSS_ACCESS_KEY_ID"));
aos_str_set(&options->config->access_key_secret, getenv("OSS_ACCESS_KEY_SECRET"));
//需要額外配置以下兩個(gè)參數(shù)
aos_str_set(&options->config->region, region);
options->config->signature_version = 4;
/* 是否設(shè)置了CNAME。0表示不設(shè)置。*/
options->config->is_cname = 0;
/* 用于設(shè)置網(wǎng)絡(luò)相關(guān)參數(shù),比如超時(shí)時(shí)間等。*/
options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
/* 在程序入口調(diào)用aos_http_io_initialize方法來(lái)初始化網(wǎng)絡(luò)、內(nèi)存等全局資源。*/
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
exit(1);
}
/* 用于內(nèi)存管理的內(nèi)存池(pool),等價(jià)于apr_pool_t。其實(shí)現(xiàn)代碼在apr庫(kù)中。*/
aos_pool_t *pool;
/* 重新創(chuàng)建一個(gè)內(nèi)存池,第二個(gè)參數(shù)值是NULL,表示沒(méi)有繼承其它內(nèi)存池。*/
aos_pool_create(&pool, NULL);
/* 創(chuàng)建并初始化options,該參數(shù)包括endpoint、access_key_id、access_key_secret、is_cname、curl等全局配置信息。*/
oss_request_options_t *oss_client_options;
/* 在內(nèi)存池中分配內(nèi)存給options。*/
oss_client_options = oss_request_options_create(pool);
/* 初始化Client的選項(xiàng)oss_client_options。*/
init_options(oss_client_options);
/* 初始化參數(shù)。*/
aos_string_t bucket;
aos_status_t *resp_status = NULL;
oss_list_object_params_t *params = NULL;
oss_list_object_content_t *content = NULL;
int size = 0;
char *line = NULL;
char *prefix = "";
char *nextMarker = "";
aos_str_set(&bucket, bucket_name);
params = oss_create_list_object_params(pool);
/* 通過(guò)max_ret參數(shù)設(shè)置需要返回的文件數(shù)量。*/
/* 默認(rèn)列舉文件數(shù)量最多為1000個(gè)。如果列舉的文件數(shù)量超出1000,則只返回按字母排序的前1000個(gè)文件,且返回結(jié)果中的truncated為true,并返回next_marker作為下次讀取的起點(diǎn)。*/
params->max_ret = 100;
aos_str_set(¶ms->prefix, prefix);
aos_str_set(¶ms->marker, nextMarker);
printf("Object\tSize\tLastModified\n");
/* 列舉所有文件。*/
do {
resp_status = oss_list_object(oss_client_options, &bucket, params, NULL);
if (!aos_status_is_ok(resp_status))
{
printf("list object failed\n");
break;
}
aos_list_for_each_entry(oss_list_object_content_t, content, ¶ms->object_list, node) {
++size;
line = apr_psprintf(pool, "%.*s\t%.*s\t%.*s\n", content->key.len, content->key.data,
content->size.len, content->size.data,
content->last_modified.len, content->last_modified.data);
printf("%s", line);
}
nextMarker = apr_psprintf(pool, "%.*s", params->next_marker.len, params->next_marker.data);
aos_str_set(¶ms->marker, nextMarker);
aos_list_init(¶ms->object_list);
aos_list_init(¶ms->common_prefix_list);
} while (params->truncated == AOS_TRUE);
printf("Total %d\n", size);
/* 釋放內(nèi)存池,相當(dāng)于釋放了請(qǐng)求過(guò)程中各資源分配的內(nèi)存。*/
aos_pool_destroy(pool);
/* 釋放之前分配的全局資源。*/
aos_http_io_deinitialize();
return 0;
}
列舉指定個(gè)數(shù)的文件
以下代碼用于列舉指定個(gè)數(shù)的文件。
#include "oss_api.h"
#include "aos_http_io.h"
/* yourEndpoint填寫B(tài)ucket所在地域?qū)?yīng)的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
const char *endpoint = "yourEndpoint";
/* 填寫B(tài)ucket名稱,例如examplebucket。*/
const char *bucket_name = "examplebucket";
/* yourRegion填寫B(tài)ucket所在地域?qū)?yīng)的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/
const char *region = "yourRegion";
void init_options(oss_request_options_t *options)
{
options->config = oss_config_create(options->pool);
/* 用char*類型的字符串初始化aos_string_t類型。*/
aos_str_set(&options->config->endpoint, endpoint);
/* 從環(huán)境變量中獲取訪問(wèn)憑證。運(yùn)行本代碼示例之前,請(qǐng)確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
aos_str_set(&options->config->access_key_id, getenv("OSS_ACCESS_KEY_ID"));
aos_str_set(&options->config->access_key_secret, getenv("OSS_ACCESS_KEY_SECRET"));
//需要額外配置以下兩個(gè)參數(shù)
aos_str_set(&options->config->region, region);
options->config->signature_version = 4;
/* 是否設(shè)置了CNAME。0表示不設(shè)置。*/
options->config->is_cname = 0;
/* 用于設(shè)置網(wǎng)絡(luò)相關(guān)參數(shù),比如超時(shí)時(shí)間等。*/
options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
/* 程序入口調(diào)用aos_http_io_initialize方法,這個(gè)方法內(nèi)部會(huì)做一些全局資源的初始化,涉及網(wǎng)絡(luò)、內(nèi)存等部分。*/
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
exit(1);
}
/* 等價(jià)于apr_pool_t,用于內(nèi)存管理的內(nèi)存池,實(shí)現(xiàn)代碼在apr庫(kù)中。*/
aos_pool_t *pool;
/* 重新創(chuàng)建一個(gè)新的內(nèi)存池,第二個(gè)參數(shù)值是NULL,表示沒(méi)有繼承其它內(nèi)存池。*/
aos_pool_create(&pool, NULL);
/* 創(chuàng)建并初始化options,這個(gè)參數(shù)內(nèi)部主要包括endpoint,access_key_id,access_key_secret,is_cname, curl參數(shù)等全局配置信息。*/
oss_request_options_t *oss_client_options;
/* options的內(nèi)存是由pool分配的,后續(xù)釋放pool后,相當(dāng)于釋放了options的內(nèi)存,不需要單獨(dú)釋放內(nèi)存。*/
oss_client_options = oss_request_options_create(pool);
/* 初始化Client的選項(xiàng)oss_client_options。*/
init_options(oss_client_options);
/* 初始化參數(shù)。*/
aos_string_t bucket;
aos_status_t *resp_status = NULL;
oss_list_object_params_t *params = NULL;
oss_list_object_content_t *content = NULL;
int size = 0;
char *line = NULL;
char *prefix = "";
char *nextMarker = "";
aos_str_set(&bucket, bucket_name);
params = oss_create_list_object_params(pool);
/* 設(shè)置列舉文件的最大個(gè)數(shù),取值不能大于1000。如果不設(shè)置此參數(shù),則默認(rèn)值為100。*/
params->max_ret = 200;
aos_str_set(¶ms->prefix, prefix);
aos_str_set(¶ms->marker, nextMarker);
printf("Object\tSize\tLastModified\n");
/* 列舉所有文件。*/
resp_status = oss_list_object(oss_client_options, &bucket, params, NULL);
if (!aos_status_is_ok(resp_status))
{
printf("list object failed\n");
}
aos_list_for_each_entry(oss_list_object_content_t, content, ¶ms->object_list, node) {
++size;
line = apr_psprintf(pool, "%.*s\t%.*s\t%.*s\n", content->key.len, content->key.data,
content->size.len, content->size.data,
content->last_modified.len, content->last_modified.data);
printf("%s", line);
}
printf("Total %d\n", size);
/* 執(zhí)行完一個(gè)請(qǐng)求后,釋放這個(gè)內(nèi)存池,相當(dāng)于釋放了這個(gè)請(qǐng)求過(guò)程中各個(gè)部分分配的內(nèi)存。*/
aos_pool_destroy(pool);
/* 程序結(jié)束前,調(diào)用aos_http_io_deinitialize方法釋放之前分配的全局資源。*/
aos_http_io_deinitialize();
return 0;
}
列舉指定前綴的文件
以下代碼用于列舉包含指定前綴(prefix)的文件。
#include "oss_api.h"
#include "aos_http_io.h"
/* yourEndpoint填寫B(tài)ucket所在地域?qū)?yīng)的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
const char *endpoint = "yourEndpoint";
/* 填寫B(tài)ucket名稱,例如examplebucket。*/
const char *bucket_name = "examplebucket";
/* yourRegion填寫B(tài)ucket所在地域?qū)?yīng)的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/
const char *region = "yourRegion";
void init_options(oss_request_options_t *options)
{
options->config = oss_config_create(options->pool);
/* 用char*類型的字符串初始化aos_string_t類型。*/
aos_str_set(&options->config->endpoint, endpoint);
/* 從環(huán)境變量中獲取訪問(wèn)憑證。運(yùn)行本代碼示例之前,請(qǐng)確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
aos_str_set(&options->config->access_key_id, getenv("OSS_ACCESS_KEY_ID"));
aos_str_set(&options->config->access_key_secret, getenv("OSS_ACCESS_KEY_SECRET"));
//需要額外配置以下兩個(gè)參數(shù)
aos_str_set(&options->config->region, region);
options->config->signature_version = 4;
/* 是否設(shè)置了CNAME。0表示不設(shè)置。*/
options->config->is_cname = 0;
/* 用于設(shè)置網(wǎng)絡(luò)相關(guān)參數(shù),比如超時(shí)時(shí)間等。*/
options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
/* 程序入口調(diào)用aos_http_io_initialize方法,這個(gè)方法內(nèi)部會(huì)做一些全局資源的初始化,涉及網(wǎng)絡(luò),內(nèi)存等部分。*/
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
exit(1);
}
/* 等價(jià)于apr_pool_t,用于內(nèi)存管理的內(nèi)存池,實(shí)現(xiàn)代碼在apr庫(kù)中。*/
aos_pool_t *pool;
/* 重新創(chuàng)建一個(gè)新的內(nèi)存池,第二個(gè)參數(shù)值是NULL,表示沒(méi)有繼承其它內(nèi)存池。*/
aos_pool_create(&pool, NULL);
/* 創(chuàng)建并初始化options,這個(gè)參數(shù)內(nèi)部主要包括endpoint,access_key_id,access_key_secret,is_cname, curl參數(shù)等全局配置信息。*/
oss_request_options_t *oss_client_options;
/* options的內(nèi)存是由pool分配的,后續(xù)釋放pool后,相當(dāng)于釋放了options的內(nèi)存,不需要單獨(dú)釋放內(nèi)存。*/
oss_client_options = oss_request_options_create(pool);
/* 初始化Client的選項(xiàng)oss_client_options。*/
init_options(oss_client_options);
/* 初始化參數(shù)。*/
aos_string_t bucket;
aos_status_t *resp_status = NULL;
oss_list_object_params_t *params = NULL;
oss_list_object_content_t *content = NULL;
int size = 0;
char *line = NULL;
/* 列舉包含指定前綴的文件。*/
char *prefix = "<yourObjectPefix>";
char *nextMarker = "";
aos_str_set(&bucket, bucket_name);
params = oss_create_list_object_params(pool);
params->max_ret = 100;
aos_str_set(¶ms->prefix, prefix);
aos_str_set(¶ms->marker, nextMarker);
printf("Object\tSize\tLastModified\n");
/* 列舉所有文件。*/
do {
resp_status = oss_list_object(oss_client_options, &bucket, params, NULL);
if (!aos_status_is_ok(resp_status))
{
printf("list object failed\n");
break;
}
aos_list_for_each_entry(oss_list_object_content_t, content, ¶ms->object_list, node) {
++size;
line = apr_psprintf(pool, "%.*s\t%.*s\t%.*s\n", content->key.len, content->key.data,
content->size.len, content->size.data,
content->last_modified.len, content->last_modified.data);
printf("%s", line);
}
nextMarker = apr_psprintf(pool, "%.*s", params->next_marker.len, params->next_marker.data);
aos_str_set(¶ms->marker, nextMarker);
aos_list_init(¶ms->object_list);
aos_list_init(¶ms->common_prefix_list);
} while (params->truncated == AOS_TRUE);
printf("Total %d\n", size);
/* 執(zhí)行完一個(gè)請(qǐng)求后,釋放這個(gè)內(nèi)存池,相當(dāng)于釋放了這個(gè)請(qǐng)求過(guò)程中各個(gè)部分分配的內(nèi)存。*/
aos_pool_destroy(pool);
/* 程序結(jié)束前,調(diào)用aos_http_io_deinitialize方法釋放之前分配的全局資源。*/
aos_http_io_deinitialize();
return 0;
}
列舉指定marker之后的文件
參數(shù)marker代表文件名稱。以下代碼用于列舉指定marker之后的文件。
#include "oss_api.h"
#include "aos_http_io.h"
/* yourEndpoint填寫B(tài)ucket所在地域?qū)?yīng)的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
const char *endpoint = "yourEndpoint";
/* 填寫B(tài)ucket名稱,例如examplebucket。*/
const char *bucket_name = "examplebucket";
/* yourRegion填寫B(tài)ucket所在地域?qū)?yīng)的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/
const char *region = "yourRegion";
void init_options(oss_request_options_t *options)
{
options->config = oss_config_create(options->pool);
/* 用char*類型的字符串初始化aos_string_t類型。*/
aos_str_set(&options->config->endpoint, endpoint);
/* 從環(huán)境變量中獲取訪問(wèn)憑證。運(yùn)行本代碼示例之前,請(qǐng)確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
aos_str_set(&options->config->access_key_id, getenv("OSS_ACCESS_KEY_ID"));
aos_str_set(&options->config->access_key_secret, getenv("OSS_ACCESS_KEY_SECRET"));
//需要額外配置以下兩個(gè)參數(shù)
aos_str_set(&options->config->region, region);
options->config->signature_version = 4;
/* 是否設(shè)置了CNAME。0表示不設(shè)置。*/
options->config->is_cname = 0;
/* 用于設(shè)置網(wǎng)絡(luò)相關(guān)參數(shù),比如超時(shí)時(shí)間等。*/
options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
/* 程序入口調(diào)用aos_http_io_initialize方法,這個(gè)方法內(nèi)部會(huì)做一些全局資源的初始化,涉及網(wǎng)絡(luò),內(nèi)存等部分。*/
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
exit(1);
}
/* 等價(jià)于apr_pool_t,用于內(nèi)存管理的內(nèi)存池,實(shí)現(xiàn)代碼在apr庫(kù)中。*/
aos_pool_t *pool;
/* 重新創(chuàng)建一個(gè)新的內(nèi)存池,第二個(gè)參數(shù)值是NULL,表示沒(méi)有繼承其它內(nèi)存池。*/
aos_pool_create(&pool, NULL);
/* 創(chuàng)建并初始化options,這個(gè)參數(shù)內(nèi)部主要包括endpoint,access_key_id,access_key_secret,is_cname, curl參數(shù)等全局配置信息。*/
oss_request_options_t *oss_client_options;
/* options的內(nèi)存是由pool分配的,后續(xù)釋放pool后,相當(dāng)于釋放了options的內(nèi)存,不需要單獨(dú)釋放內(nèi)存。*/
oss_client_options = oss_request_options_create(pool);
/* 初始化Client的選項(xiàng)oss_client_options。*/
init_options(oss_client_options);
/* 初始化參數(shù)。*/
aos_string_t bucket;
aos_status_t *resp_status = NULL;
oss_list_object_params_t *params = NULL;
oss_list_object_content_t *content = NULL;
int size = 0;
char *line = NULL;
char *prefix = "";
/* 列舉指定marker之后的文件。*/
char *nextMarker = "<yourObjectMarker>";
aos_str_set(&bucket, bucket_name);
params = oss_create_list_object_params(pool);
params->max_ret = 100;
aos_str_set(¶ms->prefix, prefix);
aos_str_set(¶ms->marker, nextMarker);
printf("Object\tSize\tLastModified\n");
/* 列舉所有文件。*/
do {
resp_status = oss_list_object(oss_client_options, &bucket, params, NULL);
if (!aos_status_is_ok(resp_status))
{
printf("list object failed\n");
break;
}
aos_list_for_each_entry(oss_list_object_content_t, content, ¶ms->object_list, node) {
++size;
line = apr_psprintf(pool, "%.*s\t%.*s\t%.*s\n", content->key.len, content->key.data,
content->size.len, content->size.data,
content->last_modified.len, content->last_modified.data);
printf("%s", line);
}
nextMarker = apr_psprintf(pool, "%.*s", params->next_marker.len, params->next_marker.data);
aos_str_set(¶ms->marker, nextMarker);
aos_list_init(¶ms->object_list);
aos_list_init(¶ms->common_prefix_list);
} while (params->truncated == AOS_TRUE);
printf("Total %d\n", size);
/* 執(zhí)行完一個(gè)請(qǐng)求后,釋放這個(gè)內(nèi)存池,相當(dāng)于釋放了這個(gè)請(qǐng)求過(guò)程中各個(gè)部分分配的內(nèi)存。*/
aos_pool_destroy(pool);
/* 程序結(jié)束前,調(diào)用aos_http_io_deinitialize方法釋放之前分配的全局資源。*/
aos_http_io_deinitialize();
return 0;
}
文件夾功能
OSS沒(méi)有文件夾的概念,所有元素都是以文件來(lái)存儲(chǔ)。創(chuàng)建文件夾本質(zhì)上來(lái)說(shuō)是創(chuàng)建了一個(gè)大小為0并以正斜線(/)結(jié)尾的文件。這個(gè)文件可以被上傳和下載,控制臺(tái)會(huì)對(duì)以正斜線(/)結(jié)尾的文件以文件夾的方式展示。
通過(guò)delimiter和prefix兩個(gè)參數(shù)可以模擬文件夾功能:
如果設(shè)置prefix為某個(gè)文件夾名稱,則會(huì)列舉以此prefix開(kāi)頭的文件,即該文件夾下所有的文件和子文件夾(目錄)均顯示為objects。
如果在設(shè)置了prefix的情況下,將delimiter設(shè)置為正斜線(/),則只列舉該文件夾下的文件和子文件夾(目錄),該文件夾下的子文件夾(目錄)顯示為CommonPrefixes,子文件夾下的文件和文件夾不顯示。
假設(shè)存儲(chǔ)空間中包含文件oss.jpg
、fun/test.jpg
、fun/movie/001.avi
和fun/movie/007.avi
,以正斜線(/)作為文件夾的分隔符。以下示例說(shuō)明了如何通過(guò)模擬文件夾的方式列舉文件。
列舉存儲(chǔ)空間下所有文件
以下代碼用于列舉指定存儲(chǔ)空間下的所有文件。
#include "oss_api.h"
#include "aos_http_io.h"
/* yourEndpoint填寫B(tài)ucket所在地域?qū)?yīng)的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
const char *endpoint = "yourEndpoint";
/* 填寫B(tài)ucket名稱,例如examplebucket。*/
const char *bucket_name = "examplebucket";
/* yourRegion填寫B(tài)ucket所在地域?qū)?yīng)的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/
const char *region = "yourRegion";
void init_options(oss_request_options_t *options)
{
options->config = oss_config_create(options->pool);
/* 用char*類型的字符串初始化aos_string_t類型。*/
aos_str_set(&options->config->endpoint, endpoint);
/* 從環(huán)境變量中獲取訪問(wèn)憑證。運(yùn)行本代碼示例之前,請(qǐng)確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
aos_str_set(&options->config->access_key_id, getenv("OSS_ACCESS_KEY_ID"));
aos_str_set(&options->config->access_key_secret, getenv("OSS_ACCESS_KEY_SECRET"));
//需要額外配置以下兩個(gè)參數(shù)
aos_str_set(&options->config->region, region);
options->config->signature_version = 4;
/* 是否設(shè)置了CNAME。0表示不設(shè)置。*/
options->config->is_cname = 0;
/* 用于設(shè)置網(wǎng)絡(luò)相關(guān)參數(shù),比如超時(shí)時(shí)間等。*/
options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
/* 在程序入口調(diào)用aos_http_io_initialize方法來(lái)初始化網(wǎng)絡(luò)、內(nèi)存等全局資源。*/
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
exit(1);
}
/* 用于內(nèi)存管理的內(nèi)存池(pool),等價(jià)于apr_pool_t。其實(shí)現(xiàn)代碼在apr庫(kù)中。*/
aos_pool_t *pool;
/* 重新創(chuàng)建一個(gè)內(nèi)存池,第二個(gè)參數(shù)值是NULL,表示沒(méi)有繼承其它內(nèi)存池。*/
aos_pool_create(&pool, NULL);
/* 創(chuàng)建并初始化options,該參數(shù)包括endpoint、access_key_id、access_key_secret、is_cname、curl等全局配置信息。*/
oss_request_options_t *oss_client_options;
/* 在內(nèi)存池中分配內(nèi)存給options。*/
oss_client_options = oss_request_options_create(pool);
/* 初始化Client的選項(xiàng)oss_client_options。*/
init_options(oss_client_options);
/* 初始化參數(shù)。*/
aos_string_t bucket;
aos_status_t *resp_status = NULL;
oss_list_object_params_t *params = NULL;
oss_list_object_content_t *content = NULL;
int size = 0;
char *line = NULL;
char *nextMarker = "";
aos_str_set(&bucket, bucket_name);
params = oss_create_list_object_params(pool);
params->max_ret = 100;
aos_str_set(¶ms->marker, nextMarker);
printf("Object\tSize\tLastModified\n");
/* 列舉所有文件。*/
do {
resp_status = oss_list_object(oss_client_options, &bucket, params, NULL);
if (!aos_status_is_ok(resp_status))
{
printf("list object failed\n");
break;
}
aos_list_for_each_entry(oss_list_object_content_t, content, ¶ms->object_list, node) {
++size;
line = apr_psprintf(pool, "%.*s\t%.*s\t%.*s\n", content->key.len, content->key.data,
content->size.len, content->size.data,
content->last_modified.len, content->last_modified.data);
printf("%s", line);
}
nextMarker = apr_psprintf(pool, "%.*s", params->next_marker.len, params->next_marker.data);
aos_str_set(¶ms->marker, nextMarker);
aos_list_init(¶ms->object_list);
aos_list_init(¶ms->common_prefix_list);
} while (params->truncated == AOS_TRUE);
printf("Total %d\n", size);
/* 釋放內(nèi)存池,相當(dāng)于釋放了請(qǐng)求過(guò)程中各資源分配的內(nèi)存。*/
aos_pool_destroy(pool);
/* 釋放之前分配的全局資源。*/
aos_http_io_deinitialize();
return 0;
}
返回結(jié)果如下:
Object Size LastModified
exampleobject.txt 1 2023-08-15T06:53:44.000Z
fun/ 0 2023-08-15T06:22:02.000Z
fun/movie/ 0 2023-08-15T06:22:29.000Z
fun/movie/001.avi 11 2023-08-15T06:22:37.000Z
fun/movie/007.avi 0 2023-08-15T06:22:37.000Z
fun/test.jpg 57210 2023-08-15T06:22:15.000Z
oss.jpg 9 2023-08-15T06:23:20.000Z
Total 7
列舉指定目錄下所有文件
以下代碼用于列舉指定目錄下的所有文件。
#include "oss_api.h"
#include "aos_http_io.h"
/* yourEndpoint填寫B(tài)ucket所在地域?qū)?yīng)的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
const char *endpoint = "yourEndpoint";
/* 填寫B(tài)ucket名稱,例如examplebucket。*/
const char *bucket_name = "examplebucket";
/* yourRegion填寫B(tài)ucket所在地域?qū)?yīng)的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/
const char *region = "yourRegion";
void init_options(oss_request_options_t *options)
{
options->config = oss_config_create(options->pool);
/* aos_str_set是用char*類型的字符串初始化aos_string_t類型。*/
aos_str_set(&options->config->endpoint, endpoint);
/* 從環(huán)境變量中獲取訪問(wèn)憑證。運(yùn)行本代碼示例之前,請(qǐng)確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
aos_str_set(&options->config->access_key_id, getenv("OSS_ACCESS_KEY_ID"));
aos_str_set(&options->config->access_key_secret, getenv("OSS_ACCESS_KEY_SECRET"));
//需要額外配置以下兩個(gè)參數(shù)
aos_str_set(&options->config->region, region);
options->config->signature_version = 4;
/* 是否設(shè)置了CNAME。0表示不設(shè)置。*/
options->config->is_cname = 0;
/* 用于設(shè)置網(wǎng)絡(luò)相關(guān)參數(shù),比如超時(shí)時(shí)間等。*/
options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
/* 程序入口調(diào)用aos_http_io_initialize方法,該方法內(nèi)部會(huì)做某些全局資源的初始化,涉及網(wǎng)絡(luò)、內(nèi)存等部分。*/
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
exit(1);
}
/* 等價(jià)于apr_pool_t,用于內(nèi)存管理的內(nèi)存池,實(shí)現(xiàn)代碼在apr庫(kù)中。*/
aos_pool_t *pool;
/* 重新創(chuàng)建一個(gè)新的內(nèi)存池,第二個(gè)參數(shù)值是NULL,表示沒(méi)有繼承其它內(nèi)存池。*/
aos_pool_create(&pool, NULL);
/* 創(chuàng)建并初始化options,這個(gè)參數(shù)內(nèi)部主要包括endpoint,access_key_id,access_key_secret,is_cname, curl參數(shù)等全局配置信息。*/
oss_request_options_t *oss_client_options;
/* options的內(nèi)存是由pool分配的,后續(xù)釋放pool后,相當(dāng)于釋放了options的內(nèi)存,不需要單獨(dú)釋放內(nèi)存。*/
oss_client_options = oss_request_options_create(pool);
/* 初始化Client的選項(xiàng)oss_client_options。*/
init_options(oss_client_options);
/* 初始化參數(shù)。*/
aos_string_t bucket;
aos_status_t *resp_status = NULL;
oss_list_object_params_t *params = NULL;
oss_list_object_content_t *content = NULL;
int size = 0;
char *line = NULL;
/* 列舉包含指定前綴的文件。*/
char *prefix = "fun/";
char *nextMarker = "";
aos_str_set(&bucket, bucket_name);
params = oss_create_list_object_params(pool);
params->max_ret = 100;
aos_str_set(¶ms->prefix, prefix);
aos_str_set(¶ms->marker, nextMarker);
printf("Object\tSize\tLastModified\n");
/* 列舉所有文件。*/
do {
resp_status = oss_list_object(oss_client_options, &bucket, params, NULL);
if (!aos_status_is_ok(resp_status))
{
printf("list object failed\n");
break;
}
aos_list_for_each_entry(oss_list_object_content_t, content, ¶ms->object_list, node) {
++size;
line = apr_psprintf(pool, "%.*s\t%.*s\t%.*s\n", content->key.len, content->key.data,
content->size.len, content->size.data,
content->last_modified.len, content->last_modified.data);
printf("%s", line);
}
nextMarker = apr_psprintf(pool, "%.*s", params->next_marker.len, params->next_marker.data);
aos_str_set(¶ms->marker, nextMarker);
aos_list_init(¶ms->object_list);
aos_list_init(¶ms->common_prefix_list);
} while (params->truncated == AOS_TRUE);
printf("Total %d\n", size);
/* 執(zhí)行完一個(gè)請(qǐng)求后,釋放這個(gè)內(nèi)存池,相當(dāng)于釋放了這個(gè)請(qǐng)求過(guò)程中各個(gè)部分分配的內(nèi)存。*/
aos_pool_destroy(pool);
/* 程序結(jié)束前,調(diào)用aos_http_io_deinitialize方法釋放之前分配的全局資源。*/
aos_http_io_deinitialize();
return 0;
}
返回結(jié)果如下:
Object Size LastModified
fun/ 0 2023-08-15T06:22:02.000Z
fun/movie/ 0 2023-08-15T06:22:29.000Z
fun/movie/001.avi 11 2023-08-15T06:22:37.000Z
fun/movie/007.avi 0 2023-08-15T06:22:37.000Z
fun/test.jpg 57210 2023-08-15T06:22:15.000Z
Total 5
列舉目錄下的文件和子目錄
以下代碼用于列舉指定目錄下的文件和子目錄。
#include "oss_api.h"
#include "aos_http_io.h"
/* yourEndpoint填寫B(tài)ucket所在地域?qū)?yīng)的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
const char *endpoint = "yourEndpoint";
/* 填寫B(tài)ucket名稱,例如examplebucket。*/
const char *bucket_name = "examplebucket";
/* yourRegion填寫B(tài)ucket所在地域?qū)?yīng)的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/
const char *region = "yourRegion";
void init_options(oss_request_options_t *options)
{
options->config = oss_config_create(options->pool);
/* aos_str_set是用char*類型的字符串初始化aos_string_t類型。*/
aos_str_set(&options->config->endpoint, endpoint);
/* 從環(huán)境變量中獲取訪問(wèn)憑證。運(yùn)行本代碼示例之前,請(qǐng)確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
aos_str_set(&options->config->access_key_id, getenv("OSS_ACCESS_KEY_ID"));
aos_str_set(&options->config->access_key_secret, getenv("OSS_ACCESS_KEY_SECRET"));
//需要額外配置以下兩個(gè)參數(shù)
aos_str_set(&options->config->region, region);
options->config->signature_version = 4;
/* 是否設(shè)置了CNAME。0表示不設(shè)置。*/
options->config->is_cname = 0;
/* 用于設(shè)置網(wǎng)絡(luò)相關(guān)參數(shù),比如超時(shí)時(shí)間等。*/
options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
/* 程序入口調(diào)用aos_http_io_initialize方法,該方法內(nèi)部會(huì)做某些全局資源的初始化,涉及網(wǎng)絡(luò)、內(nèi)存等部分。*/
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
exit(1);
}
/* 等價(jià)于apr_pool_t,用于內(nèi)存管理的內(nèi)存池,實(shí)現(xiàn)代碼在apr庫(kù)中。*/
aos_pool_t *pool;
/* 重新創(chuàng)建一個(gè)新的內(nèi)存池,第二個(gè)參數(shù)值是NULL,表示沒(méi)有繼承其它內(nèi)存池。*/
aos_pool_create(&pool, NULL);
/* 創(chuàng)建并初始化options,這個(gè)參數(shù)內(nèi)部主要包括endpoint,access_key_id,access_key_secret,is_cname, curl參數(shù)等全局配置信息。*/
oss_request_options_t *oss_client_options;
/* options的內(nèi)存是由pool分配的,后續(xù)釋放pool后,相當(dāng)于釋放了options的內(nèi)存,不需要單獨(dú)釋放內(nèi)存。*/
oss_client_options = oss_request_options_create(pool);
/* 初始化Client的選項(xiàng)oss_client_options。*/
init_options(oss_client_options);
/* 初始化參數(shù)。*/
aos_string_t bucket;
aos_status_t *resp_status = NULL;
oss_list_object_params_t *params = NULL;
oss_list_object_content_t *content = NULL;
oss_list_object_common_prefix_t *commonPrefix = NULL;
int size = 0;
char *line = NULL;
/* 列舉包含指定前綴的文件。*/
char *prefix = "fun/";
char *nextMarker = "";
/* 設(shè)置正斜線(/)為文件夾的分隔符。*/
char *delimiter = "/";
aos_str_set(&bucket, bucket_name);
params = oss_create_list_object_params(pool);
params->max_ret = 100;
aos_str_set(¶ms->prefix, prefix);
aos_str_set(¶ms->marker, nextMarker);
aos_str_set(¶ms->delimiter, delimiter);
printf("Object\tSize\tLastModified\n");
/*列舉所有文件。*/
do {
resp_status = oss_list_object(oss_client_options, &bucket, params, NULL);
if (!aos_status_is_ok(resp_status))
{
printf("list object failed\n");
break;
}
aos_list_for_each_entry(oss_list_object_content_t, content, ¶ms->object_list, node) {
++size;
line = apr_psprintf(pool, "%.*s\t%.*s\t%.*s\n", content->key.len, content->key.data,
content->size.len, content->size.data,
content->last_modified.len, content->last_modified.data);
printf("%s", line);
}
aos_list_for_each_entry(oss_list_object_common_prefix_t, commonPrefix, ¶ms->common_prefix_list, node) {
line = apr_psprintf(pool, "%.*s", commonPrefix->prefix.len, commonPrefix->prefix.data);
printf("%s", line);
}
nextMarker = apr_psprintf(pool, "%.*s", params->next_marker.len, params->next_marker.data);
aos_str_set(¶ms->marker, nextMarker);
aos_list_init(¶ms->object_list);
aos_list_init(¶ms->common_prefix_list);
} while (params->truncated == AOS_TRUE);
printf("Total %d\n", size);
/* 執(zhí)行完一個(gè)請(qǐng)求后,釋放這個(gè)內(nèi)存池,相當(dāng)于釋放了這個(gè)請(qǐng)求過(guò)程中各個(gè)部分分配的內(nèi)存。*/
aos_pool_destroy(pool);
/* 程序結(jié)束前,調(diào)用aos_http_io_deinitialize方法釋放之前分配的全局資源。*/
aos_http_io_deinitialize();
return 0;
}
返回結(jié)果如下:
Object Size LastModified
fun/ 0 2023-08-15T06:22:02.000Z
fun/test.jpg 57210 2023-08-15T06:22:15.000Z
fun/movie/ 0 2023-08-15T06:22:29.000Z
Total 3
列舉指定目錄下的文件大小
以下代碼用于獲取指定目錄下的文件大小。
#include "oss_api.h"
#include "aos_http_io.h"
/* yourEndpoint填寫B(tài)ucket所在地域?qū)?yīng)的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
const char *endpoint = "yourEndpoint";
/* 填寫B(tài)ucket名稱,例如examplebucket。*/
const char *bucket_name = "examplebucket";
/* yourRegion填寫B(tài)ucket所在地域?qū)?yīng)的Region。以華東1(杭州)為例,Region填寫為cn-hangzhou。*/
const char *region = "yourRegion";
void init_options(oss_request_options_t *options)
{
options->config = oss_config_create(options->pool);
/* aos_str_set是用char*類型的字符串初始化aos_string_t類型。*/
aos_str_set(&options->config->endpoint, endpoint);
/* 從環(huán)境變量中獲取訪問(wèn)憑證。運(yùn)行本代碼示例之前,請(qǐng)確保已設(shè)置環(huán)境變量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
aos_str_set(&options->config->access_key_id, getenv("OSS_ACCESS_KEY_ID"));
aos_str_set(&options->config->access_key_secret, getenv("OSS_ACCESS_KEY_SECRET"));
//需要額外配置以下兩個(gè)參數(shù)
aos_str_set(&options->config->region, region);
options->config->signature_version = 4;
/* 是否設(shè)置了CNAME。0表示不設(shè)置。*/
options->config->is_cname = 0;
/* 用于設(shè)置網(wǎng)絡(luò)相關(guān)參數(shù),比如超時(shí)時(shí)間等。*/
options->ctl = aos_http_controller_create(options->pool, 0);
}
int64_t calculateFolderLength (aos_string_t bucketName, char* folder)
{
int64_t size = 0;
oss_list_object_params_t *params = NULL;
char *nextMarker = "";
aos_status_t *resp_status = NULL;
aos_pool_t *p = NULL;
oss_request_options_t *options = NULL;
oss_list_object_content_t* content = NULL;
aos_pool_create(&p, NULL);
options = oss_request_options_create(p);
params = oss_create_list_object_params(p);
params->max_ret = 10;
aos_str_set(¶ms->prefix, folder);
aos_str_set(¶ms->marker, nextMarker);
/* 列舉所有文件。*/
do {
resp_status = oss_list_object(options, &bucketName, params, NULL);
if (!aos_status_is_ok(resp_status))
{
printf("list object failed\n");
break;
}
aos_list_for_each_entry(oss_list_object_content_t, content, ¶ms->object_list, node) {
size += content->size.len;
}
nextMarker = apr_psprintf(p, "%.*s", params->next_marker.len, params->next_marker.data);
aos_str_set(¶ms->marker, nextMarker);
aos_list_init(¶ms->object_list);
aos_list_init(¶ms->common_prefix_list);
} while (params->truncated == AOS_TRUE);
/* 執(zhí)行完一個(gè)請(qǐng)求后,釋放這個(gè)內(nèi)存池,相當(dāng)于釋放了這個(gè)請(qǐng)求過(guò)程中各個(gè)部分分配的內(nèi)存。*/
aos_pool_destroy(p);
return size;
}
int main(int argc, char *argv[])
{
/* 程序入口調(diào)用aos_http_io_initialize方法,該方法內(nèi)部會(huì)做某些全局資源的初始化,涉及網(wǎng)絡(luò)、內(nèi)存等部分。*/
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
exit(1);
}
/* 等價(jià)于apr_pool_t,用于內(nèi)存管理的內(nèi)存池,實(shí)現(xiàn)代碼在apr庫(kù)中。*/
aos_pool_t *pool;
/* 重新創(chuàng)建一個(gè)新的內(nèi)存池,第二個(gè)參數(shù)值是NULL,表示沒(méi)有繼承其它內(nèi)存池。*/
aos_pool_create(&pool, NULL);
/* 創(chuàng)建并初始化options,這個(gè)參數(shù)內(nèi)部主要包括endpoint,access_key_id,access_key_secret,is_cname, curl參數(shù)等全局配置信息。*/
oss_request_options_t *oss_client_options;
/* options的內(nèi)存是由pool分配的,后續(xù)釋放pool后,相當(dāng)于釋放了options的內(nèi)存,不需要單獨(dú)釋放內(nèi)存。*/
oss_client_options = oss_request_options_create(pool);
/* 初始化Client的選項(xiàng)oss_client_options。*/
init_options(oss_client_options);
/* 初始化參數(shù)。*/
aos_string_t bucket;
aos_status_t *resp_status = NULL;
oss_list_object_params_t *params = NULL;
oss_list_object_content_t *content = NULL;
oss_list_object_common_prefix_t *commonPrefix = NULL;
int size = 0;
char *line = NULL;
/* 列舉包含指定前綴的文件。*/
char *prefix = "fun/";
char *nextMarker = "";
/* 設(shè)置正斜線(/)為文件夾的分隔符。*/
char *delimiter = "/";
aos_str_set(&bucket, bucket_name);
params = oss_create_list_object_params(pool);
params->max_ret = 100;
aos_str_set(¶ms->prefix, prefix);
aos_str_set(¶ms->marker, nextMarker);
aos_str_set(¶ms->delimiter, delimiter);
printf("Object\tSize\tLastModified\n");
/*列舉所有文件。*/
do {
resp_status = oss_list_object(oss_client_options, &bucket, params, NULL);
if (!aos_status_is_ok(resp_status))
{
printf("list object failed\n");
break;
}
aos_list_for_each_entry(oss_list_object_content_t, content, ¶ms->object_list, node) {
++size;
line = apr_psprintf(pool, "%.*s\t%.*s\t%.*s\n", content->key.len, content->key.data,
content->size.len, content->size.data,
content->last_modified.len, content->last_modified.data);
printf("%s", line);
}
aos_list_for_each_entry(oss_list_object_common_prefix_t, commonPrefix, ¶ms->common_prefix_list, node) {
line = apr_psprintf(pool, "%.*s", commonPrefix->prefix.len, commonPrefix->prefix.data);
int64_t size = calculateFolderLength(bucket, commonPrefix->prefix.data);
printf("Total %ld\n", size);
}
nextMarker = apr_psprintf(pool, "%.*s", params->next_marker.len, params->next_marker.data);
aos_str_set(¶ms->marker, nextMarker);
aos_list_init(¶ms->object_list);
aos_list_init(¶ms->common_prefix_list);
} while (params->truncated == AOS_TRUE);
printf("Total %d\n", size);
/* 執(zhí)行完一個(gè)請(qǐng)求后,釋放這個(gè)內(nèi)存池,相當(dāng)于釋放了這個(gè)請(qǐng)求過(guò)程中各個(gè)部分分配的內(nèi)存。*/
aos_pool_destroy(pool);
/* 程序結(jié)束前,調(diào)用aos_http_io_deinitialize方法釋放之前分配的全局資源。*/
aos_http_io_deinitialize();
return 0;
}
返回結(jié)果如下:
Object Size LastModified
fun/ 0 2023-08-15T06:22:02.000Z
fun/test.jpg 57210 2023-08-15T06:22:15.000Z
相關(guān)文檔
關(guān)于列舉文件的完整示例代碼,請(qǐng)參見(jiàn)GitHub示例。
關(guān)于列舉文件的API接口說(shuō)明,請(qǐng)參見(jiàn)GetBucket (ListObjects)和ListObjectsV2(GetBucketV2)。