更新时间:2024-12-03 GMT+08:00
c sdk通过自定义域名访问obs
通过sdk,使用自定义域名访问obs,可以完全复用对应接口的示例,只需要注意在设置option的时候,按如下方式设置:
option.bucket_options.useCname = true; option.bucket_options.host_name = "yourCustomDomain";
sdk通过自定义域名上传对象:
static void test_put_object_from_file() { // 上传对象名 char *key = "put_file_test"; // 上传的文件 char file_name[256] = "./put_file_test.txt"; uint64_t content_length = 0; // 初始化option obs_options option; init_obs_options(&option); option.bucket_options.host_name = "<your-endpoint>"; option.bucket_options.bucket_name = "<Your bucketname>"; // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;本示例以ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量ACCESS_KEY_ID和SECRET_ACCESS_KEY。 // 您可以登录访问管理控制台获取访问密钥AK/SK,获取方式请参见https://support.huaweicloud.com/intl/zh-cn/usermanual-ca/ca_01_0003.html option.bucket_options.access_key = getenv("ACCESS_KEY_ID"); option.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY"); option.bucket_options.certificate_info = "<Your certificate>"; // 设置自定义域名 option.bucket_options.useCname = true; option.bucket_options.host_name = "yourCustomDomain"; // 初始化上传对象属性 obs_put_properties put_properties; init_put_properties(&put_properties); // 初始化存储上传数据的结构体 put_file_object_callback_data data; memset(&data, 0, sizeof(put_file_object_callback_data)); // 打开文件,并获取文件长度 content_length = open_file_and_get_length(file_name, &data); // 设置回调函数 obs_put_object_handler putobjectHandler = { { &response_properties_callback, &put_file_complete_callback }, &put_file_data_callback }; put_object(&option, key, content_length, &put_properties, 0, &putobjectHandler, &data); if (OBS_STATUS_OK == data.ret_status) { printf("put object from file successfully. \n"); } else { printf("put object failed(%s).\n", obs_get_status_name(data.ret_status)); } }
sdk通过自定义域名下载对象:
static void test_get_object() { char *file_name = "./test"; obs_object_info object_info; // 初始化option obs_options option; init_obs_options(&option); option.bucket_options.host_name = "<your-endpoint>"; option.bucket_options.bucket_name = "<Your bucketname>"; // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;本示例以ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量ACCESS_KEY_ID和SECRET_ACCESS_KEY。 // 您可以登录访问管理控制台获取访问密钥AK/SK,获取方式请参见https://support.huaweicloud.com/intl/zh-cn/usermanual-ca/ca_01_0003.html option.bucket_options.access_key = getenv("ACCESS_KEY_ID"); option.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY"); // 设置自定义域名 option.bucket_options.useCname = true; option.bucket_options.host_name = "yourCustomDomain"; // 设置下载的对象 memset(&object_info, 0, sizeof(obs_object_info)); object_info.key = "<object key>"; object_info.version_id = "<object version ID>"; //根据业务需要设置存放下载对象数据的结构 get_object_callback_data data; data.ret_status = OBS_STATUS_BUTT; data.outfile = write_to_file(file_name); // 定义范围下载参数 obs_get_conditions getcondition; memset(&getcondition, 0, sizeof(obs_get_conditions)); init_get_properties(&getcondition); getcondition.start_byte = "<start byte>"; // 下载长度,默认0,读到对象尾 getcondition.byte_count = "<byte count>"; // 定义下载的回调函数 obs_get_object_handler get_object_handler = { { &response_properties_callback, &get_object_complete_callback}, &get_object_data_callback }; get_object(&option, &object_info, &getcondition, 0, &get_object_handler, &data); if (OBS_STATUS_OK == data.ret_status) { printf("get object successfully. \n"); } else { printf("get object faied(%s).\n", obs_get_status_name(data.ret_status)); } fclose(data.outfile); }
父主题: 自定义域名访问