开发者社区 问答 正文

C-SDK之如何实现授权访问(二)?


生成上传文件请求的url签名:
aos_pool_t *p;
oss_request_options_t *options;
aos_http_request_t *req;
char *bucket_name = "<您的bucket名字>";
char *object_name = "<您的object名字>";
aos_string_t bucket;
aos_string_t object;
apr_time_t now;
int64_t expire_time;
int one_hour = 3600;
char *url_str = NULL;
aos_pool_create(&p, NULL);
/* 创建并初始化options */
options = oss_request_options_create(p);
init_options(options);
/* 初始化参数 */
aos_str_set(&bucket, bucket_name);
aos_str_set(&object, object_name);
req = aos_http_request_create(p);
req->method = HTTP_PUT;
now = apr_time_now(); //millisecond
expire_time = now / 1000000 + one_hour;
/* 生成签名url */
url_str = oss_gen_signed_url((options, &bucket, &object, expire_time, req);
printf("临时上传url:%s\n", url_str);
aos_pool_destroy(p);



使用签名URL下载文件
aos_pool_t *p;
oss_request_options_t *options;
aos_http_request_t *req;
aos_table_t *headers;
aos_table_t *resp_headers;
char *bucket_name = "<您的bucket名字>";
char *object_name = "<您的object名字>";
char *filepath = "<本地文件路径>";
aos_string_t bucket;
aos_string_t object;
aos_string_t file;
char *url_str;
apr_time_t now;
int64_t expire_time;
int one_hour = 3600;
aos_pool_create(&p, NULL);
/* 创建并初始化options */
options = oss_request_options_create(p);
init_options(options);
/* 初始化参数 */
aos_str_set(&bucket, bucket_name);
aos_str_set(&object, object_name);
aos_str_set(&file, filepath);
headers = aos_table_make(p, 0);
req = aos_http_request_create(p);
req->method = HTTP_GET;
now = apr_time_now();  /* 单位:微秒 */
expire_time = now / 1000000 + one_hour;
/* 生成签名url */
url_str = oss_gen_signed_url(options, &bucket, &object, expire_time, req);
/* 使用签名url下载文件 */
s = oss_get_object_to_file_by_url(options, url_str, headers, &file, &resp_headers);
if (aos_status_is_ok(s)) {
    printf("get object succeeded\n");
} else {
    printf("get object failed\n");
}
aos_pool_destroy(p);



使用URL签名的方式上传文件
aos_pool_t *p;
int is_oss_domain = 1;//是否使用三级域名
oss_request_options_t *options;
aos_http_request_t *req;
aos_table_t *headers;
aos_table_t *resp_headers;
char *bucket_name = "<您的bucket名字>";
char *object_name = "<您的object名字>";
char *filepath = "<本地文件路径>";
aos_string_t bucket;
aos_string_t object;
aos_string_t file;
char *url_str;
apr_time_t now;
int64_t expire_time;
int one_hour = 3600;
aos_pool_create(&p, NULL);
/* 创建并初始化options */
options = oss_request_options_create(p);
init_options(options);
/* 初始化参数 */
aos_str_set(&bucket, bucket_name);
aos_str_set(&object, object_name);
aos_str_set(&file, filepath);
headers = aos_table_make(p, 0);
req = aos_http_request_create(p);
req->method = HTTP_PUT;
now = apr_time_now(); /* 单位:微秒*/
expire_time = now / 1000000 + one_hour;
/* 生成签名url */
url_str = oss_gen_signed_url(options, &bucket, &object, expire_time, req);
/* 使用签名url上传文件 */
s = oss_put_object_from_file_by_url(options, url_str, &file, headers, &resp_headers);
if (aos_status_is_ok(s)) {
    printf("put objects by signed url succeeded\n");
} else {
    printf("put objects by signed url failed\n");
}
aos_pool_destroy(p);

展开
收起
青衫无名 2017-10-19 11:03:15 1646 分享 版权
阿里云 AI 助理回答

在阿里云的Object Storage Service (OSS)中,生成一个带有签名的上传文件请求URL涉及到几个关键步骤。下面是一个基于C语言和阿里云OSS SDK的示例代码,展示如何创建这样一个签名URL。请注意,实际使用时需要根据你的环境配置相应的密钥、bucket名称、object名称等信息。

#include "oss_api.h"
#include "aos_http_io.h"

int main() {
    aos_pool_t *p = NULL;
    oss_request_options_t *options = NULL;
    aos_string_t bucket;
    aos_string_t object;
    aos_table_t *headers = NULL;
    aos_table_t *params = NULL;
    aos_string_t url;
    char *url_str = NULL;
    apr_time_t expire_time;
    int is_cname = 0;

    // 初始化资源池
    aos_pool_create(&p, NULL);

    // 设置OSS请求选项,包括accessKeyId、accessKeySecret、endpoint等
    options = oss_request_options_create(p);
    oss_request_options_set_access_key(options, "<yourAccessKeyId>", "<yourAccessKeySecret>");
    oss_request_options_set_endpoint(options, "<yourEndpoint>");

    // 设置Bucket和Object名称
    aos_str_set(&bucket, "<yourBucketName>");
    aos_str_set(&object, "<yourObjectName>");

    // 计算过期时间,例如1小时后
    expire_time = apr_time_now() + apr_time_from_sec(3600);

    // 生成签名URL
    headers = aos_table_make(p, 0);
    params = aos_table_make(p, 0);
    aos_table_add_param(params, "Expires", apr_itoa(p, (int)expire_time));
    aos_table_add_param(params, "OSSAccessKeyId", "<yourAccessKeyId>");
    // 如果使用了STS Token,还需要添加"SecurityToken"参数

    // 注意:这里的签名过程通常由SDK内部完成,直接调用生成签名URL的API即可
    url_str = oss_generate_presigned_url(options, &bucket, &object, OSS_HTTP_PUT, expire_time, params, headers, &url);

    if (url_str) {
        printf("The signed URL for uploading is: %s\n", url_str);
    } else {
        printf("Failed to generate the signed URL.\n");
    }

    // 清理资源
    aos_pool_destroy(p);

    return 0;
}

请将<yourAccessKeyId>, <yourAccessKeySecret>, <yourEndpoint>, <yourBucketName>, 和 <yourObjectName> 替换为你的实际值。如果使用了STS临时凭证,还需添加SecurityToken到参数中。

注意:上述代码仅为示例,实际应用中需要处理错误检查、资源管理等细节,并确保使用的阿里云OSS SDK版本与你的开发环境兼容。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答