• 热门
  • 基础
  • 数据库
  • 安全
  • 大数据
  • 人工智能
  • 混合云
  • 开发与运维
  • 企业应用

应用服务

行业引擎

全部文档
当前文档

暂无内容

如果没有找到您期望的内容,请尝试其他搜索词

文档中心

C/C++

最近更新时间:2024-01-16 19:07:28

KS3 SDK For C/C++使用指南

说明

文档中的示例代码仅供参考使用,具体使用时请参考KS3 API文档,根据自己的实际情况调节参数。

1. 概述

该SDK适用于Linux/Windows环境下C/C++版本,基于KS3 API 构建。

2. 安装

2.1 下载源码

2.2 安装依赖

yum -y install libcurl-devel libxml2-devel openssl-devel

2.3 安装C SDK

  • 进入C SDK目录。

cd ks3-c-sdk-master/ks3sdk_c

  • 执行make命令进行编译,执行make install命令进行安装。

make 
make install

注:安装后的库文件和头文件分别存放在ks3-c-sdk-master/libks3-c-sdk-master/include文件夹中。

  • 进入C命令行工具目录,并执行make命令,可编译C SDK命令行工具。

cd ks3-c-sdk-master/tools
make

2.4 安装C++ SDK

  • 进入C++ SDK目录。

cd ks3-c-sdk-master/sdk_c++
  • 执行make命令进行编译。

make 
  • 进入C++命令行工具目录,并执行make命令,可编译C++ SDK命令行工具。

cd ks3-c-sdk-master/sdk_c++/tools
make

3. 初始化

3.1 获取密钥

1. 开通KS3服务,注册账号
2. 进入KS3控制台, 获取AccessKeyID 、AccessKeySecret。

3.2 常用术语介绍

Object(对象,文件)

在KS3中,用户操作的基本数据单元是Object。单个Object允许存储0~48.8TB的数据。 Object 包含Key和Data。其中,Key是Object的名字;Data是Object 的数据。Key为UTF-8编码,且编码后的长度不得超过1024个字符。

Key(文件名)

即Object的名字,Key为UTF-8编码,且编码后的长度不得超过1024个字符。Key中可以带有斜杠,当Key中带有斜杠的时候,将会自动在控制台里组织成目录结构。

更多术语介绍请参考文档:概念与术语

4. 快速入门

4.1 创建一个Bucket

const char* host = "ks3-cn-beijing.ksyuncs.com";
const char* bucket = "YOUR_BUCKET";
    const char* ak = "YOUR_ACCESS_KEY";
    const char* sk = "YOUR_SECRET_KEY";

int error;
buffer* resp = NULL;

resp = create_bucket(host, bucket, ak, sk, NULL, &error);
if (error != 0) {
    printf("curl err=%d\n", error);
} else {
    printf("status code=%d\n", resp->status_code);
    printf("status msg=%s\n", resp->status_msg);
    if (resp->body != NULL) {
        printf("error msg=%s\n", resp->body);
    }
}
buffer_free(resp);

4.2 删除一个Bucket

const char* host = "ks3-cn-beijing.ksyuncs.com";
const char* bucket = "YOUR_BUCKET";
    const char* ak = "YOUR_ACCESS_KEY";
    const char* sk = "YOUR_SECRET_KEY";

int error;
buffer* resp = NULL;

resp = delete_bucket(host, bucket, ak, sk, NULL, &error);
if (error != 0) {
    printf("curl err=%d\n", error);
} else {
    printf("status code=%d\n", resp->status_code);
    printf("status msg=%s\n", resp->status_msg);
    if (resp->body != NULL) {
        printf("error msg=%s\n", resp->body);
    }
}
buffer_free(resp);

4.3 列出用户所有空间

const char* host = "ks3-cn-beijing.ksyuncs.com";
const char* ak = "YOUR_ACCESS_KEY";
const char* sk = "YOUR_SECRET_KEY";

int error;
buffer* resp = NULL;

resp = list_all_bucket(host, ak, sk, &error);
if (error != 0) {
    printf("curl err=%d\n", error);
} else {
    printf("status code=%d\n", resp->status_code);
    printf("status msg=%s\n", resp->status_msg);
    if (resp->body != NULL) {
        printf("error msg=%s\n", resp->body);
    }
}
buffer_free(resp);

4.4 上传文件

const char* host = "ks3-cn-beijing.ksyuncs.com";
const char* bucket = "YOUR_BUCKET";
const char* object_key = "YOUR_OBJECT_KEY";
const char* filename = "LOCAL_DISK_FILE_PATH";
const char* ak = "YOUR_ACCESS_KEY";
const char* sk = "YOUR_SECRET_KEY";
const char* headers = "x-kss-acl:public-read";

int error;
buffer* resp = NULL;

resp = upload_file_object(host, bucket,
    object_key, filename, ak, sk, NULL, headers, &error);
if (error != 0) {
    printf("curl err=%d\n", error);
} else {
    printf("status code=%d\n", resp->status_code);
    printf("status msg=%s\n", resp->status_msg);
    if (resp->body != NULL) {
        printf("error msg=%s\n", resp->body);
    }
}
buffer_free(resp);

4.5 分块上传

4.5.1 初始化分块上传
const char* host = "ks3-cn-beijing.ksyuncs.com";
const char* bucket = "YOUR_BUCKET";
const char* object_key = "YOUR_OBJECT_KEY";
const char* ak = "YOUR_ACCESS_KEY";
const char* sk = "YOUR_SECRET_KEY";
const char* headers = "x-kss-acl:public-read";

int error;
buffer* resp = NULL;

resp = init_multipart_upload(host, bucket, object_key, ak, sk, NULL, headers, &error);
if (error != 0) {
    printf("curl err=%d\n", error);
} else {
    printf("status code = %ld\n", resp->status_code);
    printf("status msg = %s\n", resp->status_msg);
    if (resp->body != NULL) {
       printf("error msg = %s\n", resp->body);
    }
}
buffer_free(resp);

// 从response body中解析出uploadid:
char uploadid_str[128];
char *oid_beg_ptr = strstr(resp->body, "<UploadId>");
if (oid_beg_ptr) {
   oid_beg_ptr += strlen("<UploadId>");
   char *oid_end_ptr = strstr(oid_beg_ptr, "</UploadId>");
   if (oid_end_ptr) {
      strncpy(uploadid_str, oid_beg_ptr, oid_end_ptr - oid_beg_ptr);
      uploadid_str[oid_end_ptr - oid_beg_ptr] = 0;
  }
}
4.5.2 上传分块数据
char* data_buf = "hello world";
int data_len = strlen(data_buf);
int partNumber = 1;
resp = upload_part(host, bucket, object_key, ak, sk,
uploadid_str, partNumber, data_buf, data_len, NULL, NULL, &error);
if (error != 0) {
   printf("curl err=%d\n", error);
} else {
   printf("status code = %ld\n", resp->status_code);
   printf("status msg = %s\n", resp->status_msg);
   if (resp->body != NULL) {
      printf("error msg = %s\n", resp->body);
   }
}
buffer_free(resp);
4.5.3 完成分块上传
// 构造request body (如下是一块的例子,如果多块,则Part部分重复拼接多次)
snprintf(com_xml, sizeof(com_xml), "<CompleteMultipartUpload>\n"
           "<Part>\n"
           "<PartNumber>%d</PartNumber>\n"
           "<ETag>\"%.*s\"</ETag>"
           "</Part>\n</CompleteMultipartUpload>",
           1, 32, etag_ptr);

resp = complete_multipart_upload(host, bucket, object_key, ak, sk,
uploadid_str, com_xml, strlen(com_xml), NULL, NULL, &error);
if (error != 0) {
   printf("curl err=%d\n", error);
} else {
   printf("status code = %ld\n", resp->status_code);
   printf("status msg = %s\n", resp->status_msg);
   if (resp->body != NULL) {
     printf("error msg = %s\n", resp->body);
   }
}
buffer_free(resp);
4.5.4 列举已经上传的块
resp = list_multipart_uploads(host, bucket, ak, sk, uploadid_str, NULL, NULL, &error);
if (error != 0) {
   printf("curl err=%d\n", error);
} else {
   printf("status code = %ld\n", resp->status_code);
   printf("status msg = %s\n", resp->status_msg);
   if (resp->body != NULL) {
      printf("error msg = %s\n", resp->body);
   }
}
buffer_free(resp);
4.5.5 取消分块上传
resp = abort_multipart_upload(host, bucket, object_key, ak, sk, uploadId, NULL, NULL, &error);
if (error != 0) {
   printf("curl err=%d\n", error);
} else {
   printf("status code = %ld\n", resp->status_code);
   printf("status msg = %s\n", resp->status_msg);
   if (resp->body != NULL) {
      printf("error msg = %s\n", resp->body);
   }
}
buffer_free(resp);

4.6 带Header上传文件

注意:Header之间要以’\n’分隔。

const char* headers = "x-kss-acl:public-read\nx-kss-callbackurl:http://www.callbackurl.com/";
resp = upload_file_object(host, bucket,
    object_key, filename, ak, sk, NULL, headers, &error);
if (error != 0) {
    printf("curl err=%d\n", error);
} else {
    printf("status code=%d\n", resp->status_code);
    printf("status msg=%s\n", resp->status_msg);
    if (resp->body != NULL) {
        printf("error msg=%s\n", resp->body);
    }
}
buffer_free(resp);

4.7 下载文件

const char* to_save_file_name = "./local_save";
resp = download_file_object(host, bucket,
    object_key, to_save_file_name, ak, sk, NULL, &error);
if (error != 0) {
    printf("curl err=%d\n", error);
} else {
    printf("status code=%d\n", resp->status_code);
    printf("status msg=%s\n", resp->status_msg);
    if (resp->body != NULL) {
        printf("error msg=%s\n", resp->body);
    }
}
buffer_free(resp);

4.8 删除文件

const char* object_key = "YOUR_OBJECT_KEY";
resp = delete_object(host, bucket, object_key, ak, sk, NULL, &error);
if (error != 0) {
    printf("curl err=%d\n", error);
} else {
    printf("status code=%d\n", resp->status_code);
    printf("status msg=%s\n", resp->status_msg);
    if (resp->body != NULL) {
        printf("error msg=%s\n", resp->body);
    }
}
buffer_free(resp);

4.9 复制文件

const char* src_bucket = "SRC_BUCKET_NAME";
const char* src_object_key = "SRC_OBJECT_KEY";
const char* dst_bucket = "DST_BUCKET";
const char* dst_object_key = "DST_OBJECT_KEY";

resp = copy_object(host, src_bucket, src_object_key,
        dst_bucket, dst_object_key, ak, sk, NULL, NULL, &error);

if (error != 0) {
    printf("curl err=%d\n", error);
} else {
    printf("status code=%d\n", resp->status_code);
    printf("status msg=%s\n", resp->status_msg);
    if (resp->body != NULL) {
        printf("error msg=%s\n", resp->body);
    }
}
buffer_free(resp);

4.10 上传buf object

const char* buf = "hello world";
int buf_len = strlen(buf);

resp = upload_object(host, bucket,
    object_key, buf, buf_len, ak, sk, NULL, NULL, &error);
if (error != 0) {
    printf("curl err=%d\n", error);
} else {
    printf("status code=%d\n", resp->status_code);
    printf("status msg=%s\n", resp->status_msg);
    if (resp->body != NULL) {
        printf("error msg=%s\n", resp->body);
    }
}
free(buf);
buffer_free(resp);

纯净模式常规模式

纯净模式

点击可全屏预览文档内容

鼠标选中内容,快速反馈问题

如果在文档使用中出现问题,可选中有问题的部分进行快速反馈,我们将跟进处理。
不再提示
好的,我知道了

聆听反馈