
# 使用临时URL进行授权访问(C SDK)
![](https://support.huaweicloud.com/sdk-c-devg-obs/public_sys-resources/notice_3.0-zh-cn.png)
开发过程中，您有任何问题可以在GitHub上[提交issue](https://github.com/huaweicloud/huaweicloud-sdk-c-obs/issues)，或者在[华为云对象存储服务论坛](https://bbs.huaweicloud.com/forum/forum-620-1.html)中发帖求助。
#### 功能说明
临时授权访问是指通过访问密钥、请求方法类型、请求参数等信息生成一个临时访问权限的URL，这个URL中会包含鉴权信息，您可以使用该URL进行访问OBS服务进行特定操作。在生成URL时，您需要指定URL的有效期。生成临时授权访问的URL是通过设置结构体temp_auth_configure来实现的。
temp_auth_configure结构体存在于obs_options结构体中。该方法适用于每个C SDK接口。
| **参数名称**           | **参数类型**                                                                                                         | **是否必选** | **描述**                                                                                                                                                                                                                                                                                                  |
|:---|:---|:---|:---|
| expires            | long long int                                                                                                    | 必选       | **参数解释：** 生成的临时URL的有效期。 **约束限制：** 无 **取值范围：** 正整数，单位为秒。建议取值范围\[1, 604800\]（即1秒至7天）。0或负数为无效值。 **默认取值：** 无 |
| temp_auth_callback | void (\*[表1 temp_auth_callback])(char \*, uint64_t, char \*, uint64_t, void \*) | 必选       | **参数解释：** 回调函数，用于返回生成的临时URL和实际请求头。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无                                      |
| callback_data      | void \*                                                                                                          | 可选       | **参数解释：** 用户自定义回调数据指针，将在回调函数中透传回用户。 **约束限制：** 无 **取值范围：** 无 **默认取值：** NULL                             |
   
 表1temp_auth_callback 
| **参数名称**              | **参数类型** | **是否必选** | **描述**                                                                                                                                                                                                                                                                         |
|:---|:---|:---|:---|
| temp_auth_url         | char \*  | 必选       | **参数解释：** 生成的临时授权URL。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无         |
| temp_auth_url_len     | uint64_t | 必选       | **参数解释：** 临时授权URL的长度，单位为字节。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无  |
| temp_auth_headers     | char \*  | 必选       | **参数解释：** 生成临时URL时实际使用的请求头。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无    |
| temp_auth_headers_len | uint64_t | 必选       | **参数解释：** 实际请求头的长度，单位为字节。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无 |
| callback_data         | void \*  | 必选       | **参数解释：** 用户自定义回调数据指针。 **约束限制：** 无 **取值范围：** 无 **默认取值：** 无          |
   
#### 接口约束
- OBS支持的Region与Endpoint的对应关系，详细信息请参见[地区与终端节点](https://console.huaweicloud.com/apiexplorer/#/endpoint/OBS)。

- 如果遇到跨域报错、签名不匹配问题，请参考以下步骤排查问题：
  1. 未配置跨域，需要在控制台配置CORS规则，请参考[配置桶允许跨域请求](https://support.huaweicloud.com/sdk-browserjs-devg-obs/obs_24_0107.html)。
  
  2. 签名计算问题，请参考[URL中携带签名](https://support.huaweicloud.com/api-obs/obs_04_0011.html)排查签名参数是否正确；比如上传对象功能，后端将Content-Type参与计算签名生成授权URL，但是前端使用授权URL时没有设置Content-Type字段或者传入错误的值，此时会出现跨域错误。解决方案为：Content-Type字段前后端保持一致。
   
 
#### 方法定义
```
void (*temp_auth_callback)(char *temp_auth_url, uint64_t temp_auth_url_len, char *temp_auth_headers,
        uint64_t temp_auth_headers_len, void *callback_data);
```
通过OBS C SDK生成临时URL访问OBS的步骤如下：
1. 按照下面的代码示例，调用任意SDK接口生成带签名信息的URL和header。
2. 使用任意HTTP库发送HTTP/HTTPS请求，访问OBS服务。
以下代码示例展示了如何使用临时URL进行授权访问，包括：创建桶、上传对象、下载对象、列举对象、删除对象。
#### 代码示例一：生成用于创建桶的临时授权URL
以下示例展示如何生成用于创建桶的临时授权URL：
```
#include "eSDKOBS.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TEMP_URL_LEN 1024
#define MAX_HEADER_LEN 1024
// 响应回调函数，可以在这个回调中把properties的内容记录到callback_data(用户自定义回调数据)中
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data);
// 结束回调函数，可以在这个回调中把obs_status和obs_error_details的内容记录到callback_data(用户自定义回调数据)中
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data); 
typedef struct __tempAuthResult
{
    char tmpAuthUrl[MAX_TEMP_URL_LEN];
    char actualHeaders[MAX_HEADER_LEN];
}tempAuthResult;
void tempAuthCallBack_getResult(char *tempAuthUrl, uint64_t tempAuthUrlLen, char *tempAuthActualHeaders,
    uint64_t tempAuthActualHeadersLen, void *callbackData);
int main()
{
    // 以下示例展示如何生成创建桶的URL：
    // 在程序入口调用obs_initialize方法来初始化网络、内存等全局资源。
    obs_status ret_status = obs_initialize(OBS_INIT_ALL);
    if (OBS_STATUS_OK != ret_status)
    {
        printf("obs_initialize failed(%s).\n", obs_get_status_name(ret_status));
        return -1;
    }
    obs_options options;
    // 创建并初始化options，该参数包括访问域名(host_name)、访问密钥（access_key_id和access_key_secret）、桶名(bucket_name)、桶存储类别(storage_class)等配置信息
    init_obs_options(&options);
    // host_name填写桶所在的endpoint, 此处以华北-北京四为例，其他地区请按实际情况填写。
    options.bucket_options.host_name = "obs.cn-north-4.myhuaweicloud.com";
    // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险，建议在配置文件或者环境变量中密文存放，使用时解密，确保安全；
    // 本示例以ak和sk保存在环境变量中为例，运行本示例前请先在本地环境中设置环境变量ACCESS_KEY_ID和SECRET_ACCESS_KEY。
    options.bucket_options.access_key = getenv("ACCESS_KEY_ID");
    options.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY");
    // 检查环境变量是否设置
    if (!options.bucket_options.access_key || !options.bucket_options.secret_access_key) {
        printf("ERROR: ACCESS_KEY_ID and SECRET_ACCESS_KEY environment variables must be set.\n");
        obs_deinitialize();
        return -1;
    }
    // 填写Bucket名称，例如example-bucket-name。
    char * bucket_name = "example-bucket-name";
    options.bucket_options.bucket_name = bucket_name;
    temp_auth_configure tempauth;
    tempAuthResult  ptrResult;
    memset(&ptrResult, 0, sizeof(tempAuthResult));
    //回调数据
    tempauth.callback_data = (void *)(&ptrResult);
    // 有效时间（单位：秒）    
    tempauth.expires = 10;
    // 回调函数 返回生成的临时URL
    tempauth.temp_auth_callback = &tempAuthCallBack_getResult;
    options.temp_auth = &tempauth;
    // 回调函数赋值  
    obs_response_handler response_handler =
    {
        &response_properties_callback,
        &response_complete_callback
    };
    ret_status = OBS_STATUS_BUTT;
    // 接口调用
    create_bucket(&options, OBS_CANNED_ACL_PRIVATE, NULL, &response_handler, &ret_status);
    // 判断请求是否成功
    if (ret_status == OBS_STATUS_OK) {
        printf("the temporary signature url of create bucket generated successfully. \n"
            "the temporary signature url is %s. \n"
            "the actualHeaders are %s. \n", ptrResult.tmpAuthUrl, ptrResult.actualHeaders);
    }
    else
    {
        printf(" the temporary signature url of create bucket generation failed(%s).\n", obs_get_status_name(ret_status));
    }
    // 释放分配的全局资源
    obs_deinitialize();
}
// 响应回调函数，可以在这个回调中把properties的内容记录到callback_data(用户自定义回调数据)中
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data)
{
    if (properties == NULL)
    {
        printf("error! obs_response_properties is null!");
        return OBS_STATUS_OK;      
    }
    // 打印响应信息
#define print_nonnull(name, field)                                 \
    do {                                                           \
        if (properties-> field) {                                  \
            printf("%s: %s\n", name, properties->field);          \
        }                                                          \
    } while (0)
    print_nonnull("request_id", request_id);
    print_nonnull("request_id2", request_id2);
    print_nonnull("content_type", content_type);
    if (properties->content_length) {
        printf("content_length: %llu\n", properties->content_length);
    }
    print_nonnull("server", server);
    print_nonnull("ETag", etag);
    print_nonnull("expiration", expiration);
    print_nonnull("website_redirect_location", website_redirect_location);
    print_nonnull("version_id", version_id);
    print_nonnull("allow_origin", allow_origin);
    print_nonnull("allow_headers", allow_headers);
    print_nonnull("max_age", max_age);
    print_nonnull("allow_methods", allow_methods);
    print_nonnull("expose_headers", expose_headers);
    print_nonnull("storage_class", storage_class);
    print_nonnull("server_side_encryption", server_side_encryption);
    print_nonnull("kms_key_id", kms_key_id);
    print_nonnull("customer_algorithm", customer_algorithm);
    print_nonnull("customer_key_md5", customer_key_md5);
    print_nonnull("bucket_location", bucket_location);
    print_nonnull("obs_version", obs_version);
    print_nonnull("restore", restore);
    print_nonnull("obs_object_type", obs_object_type);
    print_nonnull("obs_next_append_position", obs_next_append_position);
    print_nonnull("obs_head_epid", obs_head_epid);
    print_nonnull("reserved_indicator", reserved_indicator);
    int i;
    for (i = 0; i < properties->meta_data_count; i++) {
        printf("x-obs-meta-%s: %s\n", properties->meta_data[i].name,
            properties->meta_data[i].value);
    }
    return OBS_STATUS_OK;
}
// 结束回调函数，可以在这个回调中把obs_status和obs_error_details的内容记录到callback_data(用户自定义回调数据)中
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data)
{
    if (callback_data) {
        obs_status *ret_status = (obs_status *)callback_data;
        *ret_status = status;
    }
    else {
        printf("Callback_data is NULL");
    }
    if (error && error->message) {
        printf("Error Message: \n   %s\n", error->message);
    }
    if (error && error->resource) {
        printf("Error Resource: \n  %s\n", error->resource);
    }
    if (error && error->further_details) {
        printf("Error further_details: \n   %s\n", error->further_details);
    }
    if (error && error->extra_details_count) {
        int i;
        for (i = 0; i < error->extra_details_count; i++) {
            printf("Error Extra Detail(%d):\n   %s:%s\n", i, error->extra_details[i].name,
                error->extra_details[i].value);
        }
    }
    if (error && error->error_headers_count) {
        int i;
        for (i = 0; i < error->error_headers_count; i++) {
            const char *errorHeader = error->error_headers[i];
            printf("Error Headers(%d):\n    %s\n", i, errorHeader == NULL ? "NULL Header" : errorHeader);
        }
    }
}
void tempAuthCallBack_getResult(char *tempAuthUrl, uint64_t tempAuthUrlLen, char *tempAuthActualHeaders,
    uint64_t tempAuthActualHeadersLen, void *callbackData)
{
    tempAuthResult * ptrResult = (tempAuthResult *)callbackData;
    if (tempAuthUrlLen >= MAX_TEMP_URL_LEN) {
        printf("WARNING: temp URL is too long (%llu >= %d), truncated.\n",
            (unsigned long long)tempAuthUrlLen, MAX_TEMP_URL_LEN);
    }
    if (tempAuthActualHeadersLen >= MAX_HEADER_LEN) {
        printf("WARNING: temp headers are too long (%llu >= %d), truncated.\n",
            (unsigned long long)tempAuthActualHeadersLen, MAX_HEADER_LEN);
    }
    snprintf(ptrResult->tmpAuthUrl, MAX_TEMP_URL_LEN, "%s", tempAuthUrl);
    if (tempAuthUrl && tempAuthUrl[0] != '\0' && ptrResult->tmpAuthUrl[0] == '\0') {
        printf("ERROR: failed to copy temp auth URL.\n");
    }
    snprintf(ptrResult->actualHeaders, MAX_HEADER_LEN, "%s", tempAuthActualHeaders);
    if (tempAuthActualHeaders && tempAuthActualHeaders[0] != '\0' && ptrResult->actualHeaders[0] == '\0') {
        printf("ERROR: failed to copy temp auth headers.\n");
    }
}
```
#### 代码示例二：生成用于上传对象的临时授权URL
以下示例展示如何生成用于上传对象的临时授权URL：
```
#include "eSDKOBS.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TEMP_URL_LEN 1024
#define MAX_HEADER_LEN 1024
// 响应回调函数，可以在这个回调中把properties的内容记录到callback_data(用户自定义回调数据)中
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data);
// 结束回调函数，可以在这个回调中把obs_status和obs_error_details的内容记录到callback_data(用户自定义回调数据)中
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data); 
typedef struct __tempAuthResult
{
    char tmpAuthUrl[MAX_TEMP_URL_LEN];
    char actualHeaders[MAX_HEADER_LEN];
}tempAuthResult;
void tempAuthCallBack_getResult(char *tempAuthUrl, uint64_t tempAuthUrlLen, char *tempAuthActualHeaders,
    uint64_t tempAuthActualHeadersLen, void *callbackData);
int main()
{
    // 以下示例展示如何生成上传对象的URL：
    // 在程序入口调用obs_initialize方法来初始化网络、内存等全局资源。
    obs_status ret_status = obs_initialize(OBS_INIT_ALL);
    if (OBS_STATUS_OK != ret_status)
    {
        printf("obs_initialize failed(%s).\n", obs_get_status_name(ret_status));
        return -1;
    }
    obs_options options;
    // 创建并初始化options，该参数包括访问域名(host_name)、访问密钥（access_key_id和access_key_secret）、桶名(bucket_name)、桶存储类别(storage_class)等配置信息
    init_obs_options(&options);
    // host_name填写桶所在的endpoint, 此处以华北-北京四为例，其他地区请按实际情况填写。
    options.bucket_options.host_name = "obs.cn-north-4.myhuaweicloud.com";
    // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险，建议在配置文件或者环境变量中密文存放，使用时解密，确保安全；
    // 本示例以ak和sk保存在环境变量中为例，运行本示例前请先在本地环境中设置环境变量ACCESS_KEY_ID和SECRET_ACCESS_KEY。
    options.bucket_options.access_key = getenv("ACCESS_KEY_ID");
    options.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY");
    // 检查环境变量是否设置
    if (!options.bucket_options.access_key || !options.bucket_options.secret_access_key) {
        printf("ERROR: ACCESS_KEY_ID and SECRET_ACCESS_KEY environment variables must be set.\n");
        obs_deinitialize();
        return -1;
    }
    // 填写Bucket名称，例如example-bucket-name。
    char * bucket_name = "example-bucket-name";
    options.bucket_options.bucket_name = bucket_name;
    temp_auth_configure tempauth;
    tempAuthResult  ptrResult;
    memset(&ptrResult, 0, sizeof(tempAuthResult));
    //回调数据
    tempauth.callback_data = (void *)(&ptrResult);
    // 有效时间（单位：秒）    
    tempauth.expires = 10;
    // 回调函数 返回生成的临时URL
    tempauth.temp_auth_callback = &tempAuthCallBack_getResult;
    options.temp_auth = &tempauth;
    // 回调函数赋值  
    obs_response_handler response_handler =
    {
        &response_properties_callback,
        &response_complete_callback
    };
    ret_status = OBS_STATUS_BUTT;
    char* key = "example-object-key";
    obs_put_object_handler putobjectHandler =
    {
        response_handler,
        NULL
    };
    // 初始化结构体put_properties
    obs_put_properties put_properties;
    init_put_properties(&put_properties);
    // 接口调用
    put_object(&options, key, 0, &put_properties, 0, &putobjectHandler, &ret_status);
    // 判断请求是否成功
    if (ret_status == OBS_STATUS_OK) {
        printf("the temporary signature url of put object generated successfully. \n"
            "the temporary signature url is %s. \n"
            "the actualHeaders are %s. \n", ptrResult.tmpAuthUrl, ptrResult.actualHeaders);
    }
    else
    {
        printf(" the temporary signature url of delete object generation failed(%s).\n", obs_get_status_name(ret_status));
    }
    // 释放分配的全局资源
    obs_deinitialize();
}
// 响应回调函数，可以在这个回调中把properties的内容记录到callback_data(用户自定义回调数据)中
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data)
{
    if (properties == NULL)
    {
        printf("error! obs_response_properties is null!");
        return OBS_STATUS_OK;      
    }
    // 打印响应信息
#define print_nonnull(name, field)                                 \
    do {                                                           \
        if (properties-> field) {                                  \
            printf("%s: %s\n", name, properties->field);          \
        }                                                          \
    } while (0)
    print_nonnull("request_id", request_id);
    print_nonnull("request_id2", request_id2);
    print_nonnull("content_type", content_type);
    if (properties->content_length) {
        printf("content_length: %llu\n", properties->content_length);
    }
    print_nonnull("server", server);
    print_nonnull("ETag", etag);
    print_nonnull("expiration", expiration);
    print_nonnull("website_redirect_location", website_redirect_location);
    print_nonnull("version_id", version_id);
    print_nonnull("allow_origin", allow_origin);
    print_nonnull("allow_headers", allow_headers);
    print_nonnull("max_age", max_age);
    print_nonnull("allow_methods", allow_methods);
    print_nonnull("expose_headers", expose_headers);
    print_nonnull("storage_class", storage_class);
    print_nonnull("server_side_encryption", server_side_encryption);
    print_nonnull("kms_key_id", kms_key_id);
    print_nonnull("customer_algorithm", customer_algorithm);
    print_nonnull("customer_key_md5", customer_key_md5);
    print_nonnull("bucket_location", bucket_location);
    print_nonnull("obs_version", obs_version);
    print_nonnull("restore", restore);
    print_nonnull("obs_object_type", obs_object_type);
    print_nonnull("obs_next_append_position", obs_next_append_position);
    print_nonnull("obs_head_epid", obs_head_epid);
    print_nonnull("reserved_indicator", reserved_indicator);
    int i;
    for (i = 0; i < properties->meta_data_count; i++) {
        printf("x-obs-meta-%s: %s\n", properties->meta_data[i].name,
            properties->meta_data[i].value);
    }
    return OBS_STATUS_OK;
}
// 结束回调函数，可以在这个回调中把obs_status和obs_error_details的内容记录到callback_data(用户自定义回调数据)中
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data)
{
    if (callback_data) {
        obs_status *ret_status = (obs_status *)callback_data;
        *ret_status = status;
    }
    else {
        printf("Callback_data is NULL");
    }
    if (error && error->message) {
        printf("Error Message: \n   %s\n", error->message);
    }
    if (error && error->resource) {
        printf("Error Resource: \n  %s\n", error->resource);
    }
    if (error && error->further_details) {
        printf("Error further_details: \n   %s\n", error->further_details);
    }
    if (error && error->extra_details_count) {
        int i;
        for (i = 0; i < error->extra_details_count; i++) {
            printf("Error Extra Detail(%d):\n   %s:%s\n", i, error->extra_details[i].name,
                error->extra_details[i].value);
        }
    }
    if (error && error->error_headers_count) {
        int i;
        for (i = 0; i < error->error_headers_count; i++) {
            const char *errorHeader = error->error_headers[i];
            printf("Error Headers(%d):\n    %s\n", i, errorHeader == NULL ? "NULL Header" : errorHeader);
        }
    }
}
void tempAuthCallBack_getResult(char *tempAuthUrl, uint64_t tempAuthUrlLen, char *tempAuthActualHeaders,
    uint64_t tempAuthActualHeadersLen, void *callbackData)
{
    tempAuthResult * ptrResult = (tempAuthResult *)callbackData;
    snprintf(ptrResult->tmpAuthUrl, MAX_TEMP_URL_LEN, "%s", tempAuthUrl);
    if (tempAuthUrl && tempAuthUrl[0] != '\0' && ptrResult->tmpAuthUrl[0] == '\0') { printf("ERROR: failed to copy temp auth URL.\n"); }
    snprintf(ptrResult->actualHeaders, MAX_HEADER_LEN, "%s", tempAuthActualHeaders);
    if (tempAuthActualHeaders && tempAuthActualHeaders[0] != '\0' && ptrResult->actualHeaders[0] == '\0') { printf("ERROR: failed to copy temp auth headers.\n"); }
}
```
#### 代码示例三：生成用于下载对象的临时授权URL
以下示例展示如何生成用于下载对象的临时授权URL：
```
#include "eSDKOBS.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TEMP_URL_LEN 1024
#define MAX_HEADER_LEN 1024
// 响应回调函数，可以在这个回调中把properties的内容记录到callback_data(用户自定义回调数据)中
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data);
// 结束回调函数，可以在这个回调中把obs_status和obs_error_details的内容记录到callback_data(用户自定义回调数据)中
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data); 
typedef struct __tempAuthResult
{
    char tmpAuthUrl[MAX_TEMP_URL_LEN];
    char actualHeaders[MAX_HEADER_LEN];
}tempAuthResult;
void tempAuthCallBack_getResult(char *tempAuthUrl, uint64_t tempAuthUrlLen, char *tempAuthActualHeaders,
    uint64_t tempAuthActualHeadersLen, void *callbackData);
int main()
{
    // 以下示例展示如何生成下载对象的URL：
    // 在程序入口调用obs_initialize方法来初始化网络、内存等全局资源。
    obs_status ret_status = obs_initialize(OBS_INIT_ALL);
    if (OBS_STATUS_OK != ret_status)
    {
        printf("obs_initialize failed(%s).\n", obs_get_status_name(ret_status));
        return -1;
    }
    obs_options options;
    // 创建并初始化options，该参数包括访问域名(host_name)、访问密钥（access_key_id和access_key_secret）、桶名(bucket_name)、桶存储类别(storage_class)等配置信息
    init_obs_options(&options);
    // host_name填写桶所在的endpoint, 此处以华北-北京四为例，其他地区请按实际情况填写。
    options.bucket_options.host_name = "obs.cn-north-4.myhuaweicloud.com";
    // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险，建议在配置文件或者环境变量中密文存放，使用时解密，确保安全；
    // 本示例以ak和sk保存在环境变量中为例，运行本示例前请先在本地环境中设置环境变量ACCESS_KEY_ID和SECRET_ACCESS_KEY。
    options.bucket_options.access_key = getenv("ACCESS_KEY_ID");
    options.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY");
    // 检查环境变量是否设置
    if (!options.bucket_options.access_key || !options.bucket_options.secret_access_key) {
        printf("ERROR: ACCESS_KEY_ID and SECRET_ACCESS_KEY environment variables must be set.\n");
        obs_deinitialize();
        return -1;
    }
    // 填写Bucket名称，例如example-bucket-name。
    char * bucket_name = "example-bucket-name";
    options.bucket_options.bucket_name = bucket_name;
    temp_auth_configure tempauth;
    tempAuthResult  ptrResult;
    memset(&ptrResult, 0, sizeof(tempAuthResult));
    //回调数据
    tempauth.callback_data = (void *)(&ptrResult);
    // 有效时间（单位：秒）    
    tempauth.expires = 10;
    // 回调函数 返回生成的临时URL
    tempauth.temp_auth_callback = &tempAuthCallBack_getResult;
    options.temp_auth = &tempauth;
    // 回调函数赋值  
    obs_response_handler response_handler =
    {
        &response_properties_callback,
        &response_complete_callback
    };
    ret_status = OBS_STATUS_BUTT;
    char* key = "example-object-key";
    char* versionid = NULL;
    // 下载对象信息
    obs_object_info object_info;
    memset(&object_info, 0, sizeof(obs_object_info));
    object_info.key = key;
    object_info.version_id = versionid;
    obs_get_object_handler getobjectHandler =
    {
        response_handler,
        NULL
    };
    // 初始化结构体put_properties
    obs_put_properties put_properties;
    init_put_properties(&put_properties);
    // 接口调用
    get_object(&options, &object_info, 0, 0, &getobjectHandler, &ret_status);
    // 判断请求是否成功
    if (ret_status == OBS_STATUS_OK) {
        printf("the temporary signature url of get object generated successfully. \n"
            "the temporary signature url is %s. \n"
            "the actualHeaders are %s. \n", ptrResult.tmpAuthUrl, ptrResult.actualHeaders);
    }
    else
    {
        printf(" the temporary signature url of get object generation failed(%s).\n", obs_get_status_name(ret_status));
    }
    // 释放分配的全局资源
    obs_deinitialize();
}
// 响应回调函数，可以在这个回调中把properties的内容记录到callback_data(用户自定义回调数据)中
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data)
{
    if (properties == NULL)
    {
        printf("error! obs_response_properties is null!");
        return OBS_STATUS_OK;      
    }
    // 打印响应信息
#define print_nonnull(name, field)                                 \
    do {                                                           \
        if (properties-> field) {                                  \
            printf("%s: %s\n", name, properties->field);          \
        }                                                          \
    } while (0)
    print_nonnull("request_id", request_id);
    print_nonnull("request_id2", request_id2);
    print_nonnull("content_type", content_type);
    if (properties->content_length) {
        printf("content_length: %llu\n", properties->content_length);
    }
    print_nonnull("server", server);
    print_nonnull("ETag", etag);
    print_nonnull("expiration", expiration);
    print_nonnull("website_redirect_location", website_redirect_location);
    print_nonnull("version_id", version_id);
    print_nonnull("allow_origin", allow_origin);
    print_nonnull("allow_headers", allow_headers);
    print_nonnull("max_age", max_age);
    print_nonnull("allow_methods", allow_methods);
    print_nonnull("expose_headers", expose_headers);
    print_nonnull("storage_class", storage_class);
    print_nonnull("server_side_encryption", server_side_encryption);
    print_nonnull("kms_key_id", kms_key_id);
    print_nonnull("customer_algorithm", customer_algorithm);
    print_nonnull("customer_key_md5", customer_key_md5);
    print_nonnull("bucket_location", bucket_location);
    print_nonnull("obs_version", obs_version);
    print_nonnull("restore", restore);
    print_nonnull("obs_object_type", obs_object_type);
    print_nonnull("obs_next_append_position", obs_next_append_position);
    print_nonnull("obs_head_epid", obs_head_epid);
    print_nonnull("reserved_indicator", reserved_indicator);
    int i;
    for (i = 0; i < properties->meta_data_count; i++) {
        printf("x-obs-meta-%s: %s\n", properties->meta_data[i].name,
            properties->meta_data[i].value);
    }
    return OBS_STATUS_OK;
}
// 结束回调函数，可以在这个回调中把obs_status和obs_error_details的内容记录到callback_data(用户自定义回调数据)中
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data)
{
    if (callback_data) {
        obs_status *ret_status = (obs_status *)callback_data;
        *ret_status = status;
    }
    else {
        printf("Callback_data is NULL");
    }
    if (error && error->message) {
        printf("Error Message: \n   %s\n", error->message);
    }
    if (error && error->resource) {
        printf("Error Resource: \n  %s\n", error->resource);
    }
    if (error && error->further_details) {
        printf("Error further_details: \n   %s\n", error->further_details);
    }
    if (error && error->extra_details_count) {
        int i;
        for (i = 0; i < error->extra_details_count; i++) {
            printf("Error Extra Detail(%d):\n   %s:%s\n", i, error->extra_details[i].name,
                error->extra_details[i].value);
        }
    }
    if (error && error->error_headers_count) {
        int i;
        for (i = 0; i < error->error_headers_count; i++) {
            const char *errorHeader = error->error_headers[i];
            printf("Error Headers(%d):\n    %s\n", i, errorHeader == NULL ? "NULL Header" : errorHeader);
        }
    }
}
void tempAuthCallBack_getResult(char *tempAuthUrl, uint64_t tempAuthUrlLen, char *tempAuthActualHeaders,
    uint64_t tempAuthActualHeadersLen, void *callbackData)
{
    tempAuthResult * ptrResult = (tempAuthResult *)callbackData;
    snprintf(ptrResult->tmpAuthUrl, MAX_TEMP_URL_LEN, "%s", tempAuthUrl);
    if (tempAuthUrl && tempAuthUrl[0] != '\0' && ptrResult->tmpAuthUrl[0] == '\0') { printf("ERROR: failed to copy temp auth URL.\n"); }
    snprintf(ptrResult->actualHeaders, MAX_HEADER_LEN, "%s", tempAuthActualHeaders);
    if (tempAuthActualHeaders && tempAuthActualHeaders[0] != '\0' && ptrResult->actualHeaders[0] == '\0') { printf("ERROR: failed to copy temp auth headers.\n"); }
}
```
#### 代码示例四：生成用于列举对象的临时授权URL
以下示例展示如何生成用于列举对象的临时授权URL：
```
#include "eSDKOBS.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TEMP_URL_LEN 1024
#define MAX_HEADER_LEN 1024
// 响应回调函数，可以在这个回调中把properties的内容记录到callback_data(用户自定义回调数据)中
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data);
// 结束回调函数，可以在这个回调中把obs_status和obs_error_details的内容记录到callback_data(用户自定义回调数据)中
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data); 
typedef struct __tempAuthResult
{
    char tmpAuthUrl[MAX_TEMP_URL_LEN];
    char actualHeaders[MAX_HEADER_LEN];
}tempAuthResult;
void tempAuthCallBack_getResult(char *tempAuthUrl, uint64_t tempAuthUrlLen, char *tempAuthActualHeaders,
    uint64_t tempAuthActualHeadersLen, void *callbackData);
int main()
{
    // 以下示例展示如何生成列举对象的URL：
    // 在程序入口调用obs_initialize方法来初始化网络、内存等全局资源。
    obs_status ret_status = obs_initialize(OBS_INIT_ALL);
    if (OBS_STATUS_OK != ret_status)
    {
        printf("obs_initialize failed(%s).\n", obs_get_status_name(ret_status));
        return -1;
    }
    obs_options options;
    // 创建并初始化options，该参数包括访问域名(host_name)、访问密钥（access_key_id和access_key_secret）、桶名(bucket_name)、桶存储类别(storage_class)等配置信息
    init_obs_options(&options);
    // host_name填写桶所在的endpoint, 此处以华北-北京四为例，其他地区请按实际情况填写。
    options.bucket_options.host_name = "obs.cn-north-4.myhuaweicloud.com";
    // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险，建议在配置文件或者环境变量中密文存放，使用时解密，确保安全；
    // 本示例以ak和sk保存在环境变量中为例，运行本示例前请先在本地环境中设置环境变量ACCESS_KEY_ID和SECRET_ACCESS_KEY。
    options.bucket_options.access_key = getenv("ACCESS_KEY_ID");
    options.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY");
    // 检查环境变量是否设置
    if (!options.bucket_options.access_key || !options.bucket_options.secret_access_key) {
        printf("ERROR: ACCESS_KEY_ID and SECRET_ACCESS_KEY environment variables must be set.\n");
        obs_deinitialize();
        return -1;
    }
    // 填写Bucket名称，例如example-bucket-name。
    char * bucket_name = "example-bucket-name";
    options.bucket_options.bucket_name = bucket_name;
    temp_auth_configure tempauth;
    tempAuthResult  ptrResult;
    memset(&ptrResult, 0, sizeof(tempAuthResult));
    //回调数据
    tempauth.callback_data = (void *)(&ptrResult);
    // 有效时间（单位：秒）    
    tempauth.expires = 10;
    // 回调函数 返回生成的临时URL
    tempauth.temp_auth_callback = &tempAuthCallBack_getResult;
    options.temp_auth = &tempauth;
    // 回调函数赋值  
    obs_response_handler response_handler =
    {
        &response_properties_callback,
        &response_complete_callback
    };
    ret_status = OBS_STATUS_BUTT;
    char* prefix = "example-prefix";
    char* next_marker = "example-next-marker";
    char* delimiter = "/";
    //列举对象的最大数目
    int maxkeys = 100;
    obs_list_objects_handler list_bucket_objects_handler =
    {
        response_handler,
        NULL
    };
    list_bucket_objects(&options, prefix, next_marker, delimiter, maxkeys,
        &list_bucket_objects_handler, &ret_status);
    // 判断请求是否成功
    if (ret_status == OBS_STATUS_OK) {
        printf("the temporary signature url of list objects generated successfully. \n"
            "the temporary signature url is %s. \n"
            "the actualHeaders are %s. \n", ptrResult.tmpAuthUrl, ptrResult.actualHeaders);
    }
    else
    {
        printf(" the temporary signature url of list objects generation failed(%s).\n", obs_get_status_name(ret_status));
    }
    // 释放分配的全局资源
    obs_deinitialize();
}
// 响应回调函数，可以在这个回调中把properties的内容记录到callback_data(用户自定义回调数据)中
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data)
{
    if (properties == NULL)
    {
        printf("error! obs_response_properties is null!");
        return OBS_STATUS_OK;      
    }
    // 打印响应信息
#define print_nonnull(name, field)                                 \
    do {                                                           \
        if (properties-> field) {                                  \
            printf("%s: %s\n", name, properties->field);          \
        }                                                          \
    } while (0)
    print_nonnull("request_id", request_id);
    print_nonnull("request_id2", request_id2);
    print_nonnull("content_type", content_type);
    if (properties->content_length) {
        printf("content_length: %llu\n", properties->content_length);
    }
    print_nonnull("server", server);
    print_nonnull("ETag", etag);
    print_nonnull("expiration", expiration);
    print_nonnull("website_redirect_location", website_redirect_location);
    print_nonnull("version_id", version_id);
    print_nonnull("allow_origin", allow_origin);
    print_nonnull("allow_headers", allow_headers);
    print_nonnull("max_age", max_age);
    print_nonnull("allow_methods", allow_methods);
    print_nonnull("expose_headers", expose_headers);
    print_nonnull("storage_class", storage_class);
    print_nonnull("server_side_encryption", server_side_encryption);
    print_nonnull("kms_key_id", kms_key_id);
    print_nonnull("customer_algorithm", customer_algorithm);
    print_nonnull("customer_key_md5", customer_key_md5);
    print_nonnull("bucket_location", bucket_location);
    print_nonnull("obs_version", obs_version);
    print_nonnull("restore", restore);
    print_nonnull("obs_object_type", obs_object_type);
    print_nonnull("obs_next_append_position", obs_next_append_position);
    print_nonnull("obs_head_epid", obs_head_epid);
    print_nonnull("reserved_indicator", reserved_indicator);
    int i;
    for (i = 0; i < properties->meta_data_count; i++) {
        printf("x-obs-meta-%s: %s\n", properties->meta_data[i].name,
            properties->meta_data[i].value);
    }
    return OBS_STATUS_OK;
}
// 结束回调函数，可以在这个回调中把obs_status和obs_error_details的内容记录到callback_data(用户自定义回调数据)中
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data)
{
    if (callback_data) {
        obs_status *ret_status = (obs_status *)callback_data;
        *ret_status = status;
    }
    else {
        printf("Callback_data is NULL");
    }
    if (error && error->message) {
        printf("Error Message: \n   %s\n", error->message);
    }
    if (error && error->resource) {
        printf("Error Resource: \n  %s\n", error->resource);
    }
    if (error && error->further_details) {
        printf("Error further_details: \n   %s\n", error->further_details);
    }
    if (error && error->extra_details_count) {
        int i;
        for (i = 0; i < error->extra_details_count; i++) {
            printf("Error Extra Detail(%d):\n   %s:%s\n", i, error->extra_details[i].name,
                error->extra_details[i].value);
        }
    }
    if (error && error->error_headers_count) {
        int i;
        for (i = 0; i < error->error_headers_count; i++) {
            const char *errorHeader = error->error_headers[i];
            printf("Error Headers(%d):\n    %s\n", i, errorHeader == NULL ? "NULL Header" : errorHeader);
        }
    }
}
void tempAuthCallBack_getResult(char *tempAuthUrl, uint64_t tempAuthUrlLen, char *tempAuthActualHeaders,
    uint64_t tempAuthActualHeadersLen, void *callbackData)
{
    tempAuthResult * ptrResult = (tempAuthResult *)callbackData;
    snprintf(ptrResult->tmpAuthUrl, MAX_TEMP_URL_LEN, "%s", tempAuthUrl);
    if (tempAuthUrl && tempAuthUrl[0] != '\0' && ptrResult->tmpAuthUrl[0] == '\0') { printf("ERROR: failed to copy temp auth URL.\n"); }
    snprintf(ptrResult->actualHeaders, MAX_HEADER_LEN, "%s", tempAuthActualHeaders);
    if (tempAuthActualHeaders && tempAuthActualHeaders[0] != '\0' && ptrResult->actualHeaders[0] == '\0') { printf("ERROR: failed to copy temp auth headers.\n"); }
}
```
#### 代码示例五：生成用于删除对象的临时授权URL
以下示例展示如何生成用于删除对象的临时授权URL：
```
#include "eSDKOBS.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TEMP_URL_LEN 1024
#define MAX_HEADER_LEN 1024
// 响应回调函数，可以在这个回调中把properties的内容记录到callback_data(用户自定义回调数据)中
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data);
// 结束回调函数，可以在这个回调中把obs_status和obs_error_details的内容记录到callback_data(用户自定义回调数据)中
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data); 
typedef struct __tempAuthResult
{
    char tmpAuthUrl[MAX_TEMP_URL_LEN];
    char actualHeaders[MAX_HEADER_LEN];
}tempAuthResult;
void tempAuthCallBack_getResult(char *tempAuthUrl, uint64_t tempAuthUrlLen, char *tempAuthActualHeaders,
    uint64_t tempAuthActualHeadersLen, void *callbackData);
int main()
{
    // 以下示例展示如何生成删除对象的URL：
    // 在程序入口调用obs_initialize方法来初始化网络、内存等全局资源。
    obs_status ret_status = obs_initialize(OBS_INIT_ALL);
    if (OBS_STATUS_OK != ret_status)
    {
        printf("obs_initialize failed(%s).\n", obs_get_status_name(ret_status));
        return -1;
    }
    obs_options options;
    // 创建并初始化options，该参数包括访问域名(host_name)、访问密钥（access_key_id和access_key_secret）、桶名(bucket_name)、桶存储类别(storage_class)等配置信息
    init_obs_options(&options);
    // host_name填写桶所在的endpoint, 此处以华北-北京四为例，其他地区请按实际情况填写。
    options.bucket_options.host_name = "obs.cn-north-4.myhuaweicloud.com";
    // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险，建议在配置文件或者环境变量中密文存放，使用时解密，确保安全；
    // 本示例以ak和sk保存在环境变量中为例，运行本示例前请先在本地环境中设置环境变量ACCESS_KEY_ID和SECRET_ACCESS_KEY。
    options.bucket_options.access_key = getenv("ACCESS_KEY_ID");
    options.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY");
    // 检查环境变量是否设置
    if (!options.bucket_options.access_key || !options.bucket_options.secret_access_key) {
        printf("ERROR: ACCESS_KEY_ID and SECRET_ACCESS_KEY environment variables must be set.\n");
        obs_deinitialize();
        return -1;
    }
    // 填写Bucket名称，例如example-bucket-name。
    char * bucket_name = "example-bucket-name";
    options.bucket_options.bucket_name = bucket_name;
    temp_auth_configure tempauth;
    tempAuthResult  ptrResult;
    memset(&ptrResult, 0, sizeof(tempAuthResult));
    //回调数据
    tempauth.callback_data = (void *)(&ptrResult);
    // 有效时间（单位：秒）    
    tempauth.expires = 10;
    // 回调函数 返回生成的临时URL
    tempauth.temp_auth_callback = &tempAuthCallBack_getResult;
    options.temp_auth = &tempauth;
    // 回调函数赋值  
    obs_response_handler response_handler =
    {
        &response_properties_callback,
        &response_complete_callback
    };
    ret_status = OBS_STATUS_BUTT;
    char* key = "example-object-key";
    char* versionid = NULL;
    // 要删除的对象的信息
    obs_object_info object_info;
    memset(&object_info, 0, sizeof(obs_object_info));
    object_info.key = key;
    object_info.version_id = versionid;
    delete_object(&options, &object_info, &response_handler, &ret_status);
    // 判断请求是否成功
    if (ret_status == OBS_STATUS_OK) {
        printf("the temporary signature url of delete object generated successfully. \n"
            "the temporary signature url is %s. \n"
            "the actualHeaders are %s. \n", ptrResult.tmpAuthUrl, ptrResult.actualHeaders);
    }
    else
    {
        printf(" the temporary signature url of delete object generation failed(%s).\n", obs_get_status_name(ret_status));
    }
    // 释放分配的全局资源
    obs_deinitialize();
}
// 响应回调函数，可以在这个回调中把properties的内容记录到callback_data(用户自定义回调数据)中
obs_status response_properties_callback(const obs_response_properties *properties, void *callback_data)
{
    if (properties == NULL)
    {
        printf("error! obs_response_properties is null!");
        return OBS_STATUS_OK;      
    }
    // 打印响应信息
#define print_nonnull(name, field)                                 \
    do {                                                           \
        if (properties-> field) {                                  \
            printf("%s: %s\n", name, properties->field);          \
        }                                                          \
    } while (0)
    print_nonnull("request_id", request_id);
    print_nonnull("request_id2", request_id2);
    print_nonnull("content_type", content_type);
    if (properties->content_length) {
        printf("content_length: %llu\n", properties->content_length);
    }
    print_nonnull("server", server);
    print_nonnull("ETag", etag);
    print_nonnull("expiration", expiration);
    print_nonnull("website_redirect_location", website_redirect_location);
    print_nonnull("version_id", version_id);
    print_nonnull("allow_origin", allow_origin);
    print_nonnull("allow_headers", allow_headers);
    print_nonnull("max_age", max_age);
    print_nonnull("allow_methods", allow_methods);
    print_nonnull("expose_headers", expose_headers);
    print_nonnull("storage_class", storage_class);
    print_nonnull("server_side_encryption", server_side_encryption);
    print_nonnull("kms_key_id", kms_key_id);
    print_nonnull("customer_algorithm", customer_algorithm);
    print_nonnull("customer_key_md5", customer_key_md5);
    print_nonnull("bucket_location", bucket_location);
    print_nonnull("obs_version", obs_version);
    print_nonnull("restore", restore);
    print_nonnull("obs_object_type", obs_object_type);
    print_nonnull("obs_next_append_position", obs_next_append_position);
    print_nonnull("obs_head_epid", obs_head_epid);
    print_nonnull("reserved_indicator", reserved_indicator);
    int i;
    for (i = 0; i < properties->meta_data_count; i++) {
        printf("x-obs-meta-%s: %s\n", properties->meta_data[i].name,
            properties->meta_data[i].value);
    }
    return OBS_STATUS_OK;
}
// 结束回调函数，可以在这个回调中把obs_status和obs_error_details的内容记录到callback_data(用户自定义回调数据)中
void response_complete_callback(obs_status status, const obs_error_details *error, void *callback_data)
{
    if (callback_data) {
        obs_status *ret_status = (obs_status *)callback_data;
        *ret_status = status;
    }
    else {
        printf("Callback_data is NULL");
    }
    if (error && error->message) {
        printf("Error Message: \n   %s\n", error->message);
    }
    if (error && error->resource) {
        printf("Error Resource: \n  %s\n", error->resource);
    }
    if (error && error->further_details) {
        printf("Error further_details: \n   %s\n", error->further_details);
    }
    if (error && error->extra_details_count) {
        int i;
        for (i = 0; i < error->extra_details_count; i++) {
            printf("Error Extra Detail(%d):\n   %s:%s\n", i, error->extra_details[i].name,
                error->extra_details[i].value);
        }
    }
    if (error && error->error_headers_count) {
        int i;
        for (i = 0; i < error->error_headers_count; i++) {
            const char *errorHeader = error->error_headers[i];
            printf("Error Headers(%d):\n    %s\n", i, errorHeader == NULL ? "NULL Header" : errorHeader);
        }
    }
}
void tempAuthCallBack_getResult(char *tempAuthUrl, uint64_t tempAuthUrlLen, char *tempAuthActualHeaders,
    uint64_t tempAuthActualHeadersLen, void *callbackData)
{
    tempAuthResult * ptrResult = (tempAuthResult *)callbackData;
    snprintf(ptrResult->tmpAuthUrl, MAX_TEMP_URL_LEN, "%s", tempAuthUrl);
    if (tempAuthUrl && tempAuthUrl[0] != '\0' && ptrResult->tmpAuthUrl[0] == '\0') { printf("ERROR: failed to copy temp auth URL.\n"); }
    snprintf(ptrResult->actualHeaders, MAX_HEADER_LEN, "%s", tempAuthActualHeaders);
    if (tempAuthActualHeaders && tempAuthActualHeaders[0] != '\0' && ptrResult->actualHeaders[0] == '\0') { printf("ERROR: failed to copy temp auth headers.\n"); }
}
```
