Skip to content

Commit

Permalink
Merge pull request #66 from tencentyun/feature_huberyxxiao_77a482ea
Browse files Browse the repository at this point in the history
Feature huberyxxiao 77a482ea
  • Loading branch information
Huberyxiao authored Dec 23, 2024
2 parents 44ff922 + 49b48aa commit 64bf8d5
Show file tree
Hide file tree
Showing 7 changed files with 436 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cos_c_sdk/cos_sys_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ typedef apr_array_header_t cos_array_header_t;
#define CR (char) 13
#define CRLF "\x0d\x0a"

#define COS_VERSION "5.0.21"
#define COS_VERSION "5.0.22"
#define COS_VER "cos-sdk-c/" COS_VERSION

#define COS_HTTP_PREFIX "http://"
Expand Down
54 changes: 54 additions & 0 deletions cos_c_sdk/cos_utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ static char *cos_invaild_params_error_msg[] = {
"bucket is null or empty, please check it",
"ak is start with space or end with space, please check it",
"sk is start with space or end with space, please check it",
"bucket is nillegal, please check it",
};

static uintptr_t ignore_bucket_check_ptr = -1;
Expand All @@ -143,6 +144,54 @@ static int is_ak_or_sk_valid(cos_string_t *str)
return COS_TRUE;
}

int is_alnum(char c) {
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9');
}

int matches_pattern_2(cos_string_t* str) {
if (str->len != 1 || !is_alnum(str->data[0])) {
return COS_FALSE;
}
return COS_TRUE;
}

int matches_pattern_1(cos_string_t* str) {
if (str->len == 0) {
return COS_FALSE;
}

// 检查第一个字符是否为字母或数字
if (!is_alnum(str->data[0])) {
return COS_FALSE;
}

// 检查最后一个字符是否为字母或数字
if (!is_alnum(str->data[str->len - 1])) {
return COS_FALSE;
}

// 检查中间字符是否为字母、数字或短横线
int i = 1;
for (i = 1; i < str->len - 1; ++i) {
char c = str->data[i];
if (!is_alnum(c) && c != '-') {
return COS_FALSE;
}
}

return COS_TRUE;
}

int check_bucket(cos_string_t* bucket) {
if (matches_pattern_1(bucket)) {
return COS_TRUE;
}
if (matches_pattern_2(bucket)) {
return COS_TRUE;
}
return COS_FALSE;
}

#ifdef MOCK_IS_SHOULD_RETRY
int is_should_retry(const cos_status_t *s, const char *str){
return get_test_retry_change_domin_config();
Expand Down Expand Up @@ -432,6 +481,11 @@ static int is_config_params_vaild(const cos_request_options_t *options,
cos_error_log("config params invaild, msg: %s", *error_msg);
return COS_FALSE;
}
if ((uintptr_t)bucket != ignore_bucket_check_ptr && !check_bucket(bucket)) {
*error_msg = cos_invaild_params_error_msg[5];
cos_error_log("config params invaild, msg: %s", *error_msg);
return COS_FALSE;
}

return COS_TRUE;
}
Expand Down
12 changes: 12 additions & 0 deletions cos_c_sdk_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ set(delete_object_demo_src cos_object_demo/delete_object_demo.c)
set(get_object_url_demo_src cos_object_demo/get_object_url_demo.c)
set(restore_object_demo_src cos_object_demo/restore_object_demo.c)
set(head_object_demo_src cos_object_demo/head_object_demo.c)
set(delete_bucket_demo_src cos_bucket_demo/delete_bucket_demo.c)
set(get_bucket_list_demo_src cos_bucket_demo/get_bucket_list_demo.c)
set(head_bucket_demo_src cos_bucket_demo/head_bucket_demo.c)
set(put_bucket_demo_src cos_bucket_demo/put_bucket_demo.c)

# find_path(APR_INCLUDE_DIR apr-1/apr_time.h)
# find_path(APR_UTIL_INCLUDE_DIR apr/include/apr-1/apr_md5.h)
Expand All @@ -37,6 +41,10 @@ add_executable(delete_object_demo ${delete_object_demo_src})
add_executable(get_object_url_demo ${get_object_url_demo_src})
add_executable(restore_object_demo ${restore_object_demo_src})
add_executable(head_object_demo ${head_object_demo_src})
add_executable(delete_bucket_demo ${delete_bucket_demo_src})
add_executable(get_bucket_list_demo ${get_bucket_list_demo_src})
add_executable(head_bucket_demo ${head_bucket_demo_src})
add_executable(put_bucket_demo ${put_bucket_demo_src})

target_link_libraries(cos_c_sdk_test cos_c_sdk)
target_link_libraries(cos_c_sdk_test ${APR_UTIL_LIBRARY})
Expand All @@ -51,4 +59,8 @@ target_link_libraries(delete_object_demo cos_c_sdk ${APR_UTIL_LIBRARY} ${APR_LIB
target_link_libraries(get_object_url_demo cos_c_sdk ${APR_UTIL_LIBRARY} ${APR_LIBRARY} ${MINIXML_LIBRARY} ${CURL_LIBRARY})
target_link_libraries(restore_object_demo cos_c_sdk ${APR_UTIL_LIBRARY} ${APR_LIBRARY} ${MINIXML_LIBRARY} ${CURL_LIBRARY})
target_link_libraries(head_object_demo cos_c_sdk ${APR_UTIL_LIBRARY} ${APR_LIBRARY} ${MINIXML_LIBRARY} ${CURL_LIBRARY})
target_link_libraries(delete_bucket_demo cos_c_sdk ${APR_UTIL_LIBRARY} ${APR_LIBRARY} ${MINIXML_LIBRARY} ${CURL_LIBRARY})
target_link_libraries(get_bucket_list_demo cos_c_sdk ${APR_UTIL_LIBRARY} ${APR_LIBRARY} ${MINIXML_LIBRARY} ${CURL_LIBRARY})
target_link_libraries(head_bucket_demo cos_c_sdk ${APR_UTIL_LIBRARY} ${APR_LIBRARY} ${MINIXML_LIBRARY} ${CURL_LIBRARY})
target_link_libraries(put_bucket_demo cos_c_sdk ${APR_UTIL_LIBRARY} ${APR_LIBRARY} ${MINIXML_LIBRARY} ${CURL_LIBRARY})

76 changes: 76 additions & 0 deletions cos_c_sdk_test/cos_bucket_demo/delete_bucket_demo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <stdint.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include "cos_api.h"
#include "cos_http_io.h"
#include "cos_log.h"

/**
* 本样例演示了如何使用 COS C SDK 删除存储桶
*/

// COS 的 bucket 名称, [bucket]-[appid], 如: mybucket-1253666666,可在 https://console.cloud.tencent.com/cos5/bucket 查看
char bucket_name[] = "examplebucket-12500000000";
// 开发者访问 COS 服务时拥有的用户维度唯一资源标识,用以标识资源,可在 https://console.cloud.tencent.com/cam/capi 页面获取
char appid[] = "12500000000";
// 开发者拥有的项目身份ID/密钥,可在 https://console.cloud.tencent.com/cam/capi 页面获取
char secret_id[] = "AKIDXXXXXXXX";
char secret_key[] = "1A2Z3YYYYYYYYYY";
//endpoint 是 COS 访问域名信息(不设置存储桶前缀,访问 COS 时会自动在前面拼接上[bucket]-[appid]), 详情请参见 https://cloud.tencent.com/document/product/436/6224 文档
char endpoint[] = "cos.ap-guangzhou.myqcloud.com";
// 是否使用自定域名。如果设置为 COS_TRUE ,则访问 COS 时需要将 endpoint 的值修改为自定义域名
int is_cname = COS_FALSE;

void init_test_config(cos_config_t* config, int is_cname) {
cos_str_set(&config->endpoint, endpoint);
cos_str_set(&config->access_key_id, secret_id);
cos_str_set(&config->access_key_secret, secret_key);
cos_str_set(&config->appid, appid);
// cos_str_set(&config->sts_token, token); // 使用临时密钥时的 token
config->is_cname = is_cname; // 是否使用自定义域名
}

void init_test_request_options(cos_request_options_t* options, int is_cname) {
options->config = cos_config_create(options->pool);
init_test_config(options->config, is_cname);
options->ctl = cos_http_controller_create(options->pool, 0);
}

void log_status(cos_status_t* s) {
cos_warn_log("status->code: %d", s->code);
if (s->error_code)
cos_warn_log("status->error_code: %s", s->error_code);
if (s->error_msg)
cos_warn_log("status->error_msg: %s", s->error_msg);
if (s->req_id)
cos_warn_log("status->req_id: %s", s->req_id);
}
void delete_bucket_demo(){
cos_pool_t *p = NULL;
cos_status_t *s = NULL;
cos_request_options_t *options = NULL;
cos_acl_e cos_acl = COS_ACL_PRIVATE;
cos_string_t bucket;
cos_table_t *resp_headers = NULL;

cos_pool_create(&p, NULL);
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
cos_str_set(&bucket, bucket_name);
//delete bucket
s = cos_delete_bucket(options, &bucket, &resp_headers);
log_status(s);
}

int main() {
if (cos_http_io_initialize(NULL, 0) != COSE_OK) {
exit(1);
}
// set log level, default COS_LOG_WARN
cos_log_set_level(COS_LOG_ERROR);
delete_bucket_demo();
// cos_http_io_deinitialize last
cos_http_io_deinitialize();
return 0;
}
100 changes: 100 additions & 0 deletions cos_c_sdk_test/cos_bucket_demo/get_bucket_list_demo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include <stdint.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include "cos_api.h"
#include "cos_http_io.h"
#include "cos_log.h"

/**
* 本样例演示了如何使用 COS C SDK 获取存储桶列表
*/

// COS 的 bucket 名称, [bucket]-[appid], 如: mybucket-1253666666,可在 https://console.cloud.tencent.com/cos5/bucket 查看
char bucket_name[] = "examplebucket-12500000000";
// 开发者访问 COS 服务时拥有的用户维度唯一资源标识,用以标识资源,可在 https://console.cloud.tencent.com/cam/capi 页面获取
char appid[] = "12500000000";
// 开发者拥有的项目身份ID/密钥,可在 https://console.cloud.tencent.com/cam/capi 页面获取
char secret_id[] = "AKIDXXXXXXXX";
char secret_key[] = "1A2Z3YYYYYYYYYY";
//endpoint 是 COS 访问域名信息(不设置存储桶前缀,访问 COS 时会自动在前面拼接上[bucket]-[appid]), 详情请参见 https://cloud.tencent.com/document/product/436/6224 文档
char endpoint[] = "cos.ap-guangzhou.myqcloud.com";
// 是否使用自定域名。如果设置为 COS_TRUE ,则访问 COS 时需要将 endpoint 的值修改为自定义域名
int is_cname = COS_FALSE;

void init_test_config(cos_config_t* config, int is_cname) {
cos_str_set(&config->endpoint, endpoint);
cos_str_set(&config->access_key_id, secret_id);
cos_str_set(&config->access_key_secret, secret_key);
cos_str_set(&config->appid, appid);
// cos_str_set(&config->sts_token, token); // 使用临时密钥时的 token
config->is_cname = is_cname; // 是否使用自定义域名
}

void init_test_request_options(cos_request_options_t* options, int is_cname) {
options->config = cos_config_create(options->pool);
init_test_config(options->config, is_cname);
options->ctl = cos_http_controller_create(options->pool, 0);
}

void log_status(cos_status_t* s) {
cos_warn_log("status->code: %d", s->code);
if (s->error_code)
cos_warn_log("status->error_code: %s", s->error_code);
if (s->error_msg)
cos_warn_log("status->error_msg: %s", s->error_msg);
if (s->req_id)
cos_warn_log("status->req_id: %s", s->req_id);
}
void get_bucket_list_demo()
{
cos_pool_t *pool = NULL;
cos_status_t *status = NULL;
cos_request_options_t *options = NULL;
cos_get_service_params_t *list_params = NULL;
cos_table_t *resp_headers = NULL;

//创建内存池
cos_pool_create(&pool, NULL);

//初始化请求选项
options = cos_request_options_create(pool);
options->config = cos_config_create(options->pool);

init_test_request_options(options, is_cname);
options->ctl = cos_http_controller_create(options->pool, 0);

//创建get service参数, 默认获取全部bucket
list_params = cos_create_get_service_params(options->pool);
//若将all_region设置为0,则只根据options->config->endpoint的区域进行查询
//list_params->all_region = 0;

status = cos_get_service(options, list_params, &resp_headers);
log_status(status);
if (!cos_status_is_ok(status)) {
cos_pool_destroy(pool);
return;
}

//查看结果
cos_get_service_content_t *content = NULL;
char *line = NULL;
cos_list_for_each_entry(cos_get_service_content_t, content, &list_params->bucket_list, node) {
line = apr_psprintf(options->pool, "%.*s\t%.*s\t%.*s\n", content->bucket_name.len, content->bucket_name.data, content->location.len, content->location.data, content->creation_date.len, content->creation_date.data);
printf("%s", line);
}

cos_pool_destroy(pool);
}

int main() {
if (cos_http_io_initialize(NULL, 0) != COSE_OK) {
exit(1);
}
// set log level, default COS_LOG_WARN
cos_log_set_level(COS_LOG_ERROR);
get_bucket_list_demo();
// cos_http_io_deinitialize last
cos_http_io_deinitialize();
return 0;
}
Loading

0 comments on commit 64bf8d5

Please sign in to comment.