腾讯云智能媒资托管(Smart Media Hosting)C++ SDK
# 克隆仓库
git clone https://cnb.cool/tencent/cloud/smh/smh-cpp-sdk
cd smh-cpp-sdk
# Debug 构建
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Debug ..
cmake --build . --config=Debug
# Release 构建
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
cmake --build . --config=Release
在您的 CMakeLists.txt 中添加以下内容:
# 方式1:作为子目录引入 add_subdirectory(path/to/smh-cpp-sdk) target_link_libraries(your_target PRIVATE smh_cpp_sdk) # 方式2:手动指定头文件和库 target_include_directories(your_target PRIVATE path/to/smh-cpp-sdk/include) target_link_libraries(your_target PRIVATE smh_cpp_sdk CURL::libcurl OpenSSL::Crypto JsonCpp::JsonCpp fmt::fmt)
⚠️ 重要说明
在使用 SDK 前,您需要先在 腾讯云智能媒资托管控制台 创建媒体库。创建完成后,控制台会展示为您生成的专属域名(如
smhxxx.api.tencentsmh.cn),如下图所示:
请务必将 SDK 的服务器地址设置为您的专属域名。
#include "smhcppsdk/smh_client.h"
#include "smhcppsdk/smh_client_option.h"
using namespace TencentCloud::Smh;
// 创建客户端选项
std::vector<Option> options = {
// 设置专属域名(必须),请将 URL 替换为您在控制台获取的专属域名
WithEndpoint("https://smhxxx.api.tencentsmh.cn")
};
// 创建客户端
SmhClient client(options);
#include "smhcppsdk/smh_client_token_operation.h"
// 创建 Token 请求参数
SmhClientTokenOperation::CreateTokenArgs args;
args.library_id = "your-library-id"; // 媒体库 ID
args.library_secret = "your-library-secret"; // 媒体库密钥
args.space_id = "your-space-id"; // 空间 ID
args.user_id = "user-id"; // 用户身份识别
args.period = 7200; // 令牌有效时长(秒)
// 获取访问令牌
SmhClientTokenOperation::CreateTokenResult result;
int status = client.TokenOperation().CreateToken(args, &result);
if (status == 0) {
std::cout << "Access Token: " << result.access_token << std::endl;
std::cout << "Expires In: " << result.expires_in << " seconds" << std::endl;
} else {
std::cerr << "Error: " << client.GetLastError().ToString() << std::endl;
}
Token 有效期(默认 2 小时),过期后需要重新获取。建议在业务代码中实现 Token 续期逻辑:
// Token 续期函数
bool RenewAccessToken(SmhClient& client,
const std::string& library_id,
const std::string& access_token,
std::string& new_token,
int& expires_in) {
SmhClientTokenOperation::RenewTokenArgs args;
args.library_id = library_id;
args.access_token = access_token;
SmhClientTokenOperation::RenewTokenResult result;
int status = client.TokenOperation().RenewToken(args, &result);
if (status == 0) {
new_token = result.access_token;
expires_in = result.expires_in;
return true;
}
return false;
}
// 使用示例
std::string new_token;
int expires_in;
if (RenewAccessToken(client, library_id, current_token, new_token, expires_in)) {
std::cout << "Token renewed: " << new_token << std::endl;
std::cout << "New expires in: " << expires_in << " seconds" << std::endl;
}
建议的续期策略:
- 缓存 Token 及其过期时间
- 在每次请求前检查 Token 是否即将过期
- 如即将过期,调用 RenewToken 接口续期
完整的 API 文档请参考 docs 目录。
| 模块 | 功能说明 |
|---|---|
TokenOperation | 访问令牌管理(文档) - 创建访问令牌(CreateToken) - 续期访问令牌(RenewToken) - 删除访问令牌(DeleteToken) - 删除用户所有令牌(DeleteUserTokens) |
FileOperation | 文件操作(文档) - 简单上传文件(SimpleUploadFile) - 分块上传文件(MultipartUploadFile) - 表单上传文件(FormUploadFile) - 完成文件上传(CompleteFileUpload) - 下载文件(DownloadFile) - 获取文件信息(InfoFile) - 复制文件(CopyFile) - 移动文件(MoveFile) - 删除文件(DeleteFile) - 检查文件状态(CheckFileStatus) - 获取文件上传信息(GetFileUpload) - 续期分块上传(RenewMultipartUpload) - 中止文件上传(AbortFileUpload) - 创建符号链接(CreateSymlink) - 文件预览(PreviewFile) - 获取封面(GetCover) - 文件转换(ConvertFile) - 通过 iNode 获取文件信息(GetFileInfoByInode) |
DirectoryOperation | 目录操作(文档) - 创建目录(CreateDirectory) - 列出目录内容(ListDirectory) - 获取目录或文件信息(InfoFileOrDirectory) - 复制目录(CopyDirectory) - 移动目录(MoveDirectory) - 删除目录(DeleteDirectory) - 检查目录状态(CheckDirectoryStatus) - 更新目录标签(UpdateDirectoryLabels) - 更新文件标签(UpdateFileLabels) - 分页列出目录内容(ListDirectoryByPage) |
SpaceOperation | 租户空间管理(文档) - 创建空间(CreateSpace) - 删除空间(DeleteSpace) - 列出空间列表(ListSpace) - 获取空间数量(GetLibrarySpaceCount) - 获取空间大小(GetSpaceSize) - 获取空间文件数量(GetFileCountInSpace) - 获取空间扩展信息(GetSpaceExtension) - 更新空间扩展信息(UpdateSpaceExtension) - 获取空间内容视图(GetContentsView) - 设置空间流量限制(SetSpaceTrafficLimit) |
BatchOperation | 批量操作 - 批量复制(BatchCopy) - 批量移动(BatchMove) - 批量删除(BatchDelete) |
QuotaOperation | 配额管理(文档) - 创建配额(CreateQuota) - 获取空间配额(GetQuota) - 获取配额信息(GetQuotaInfo) - 更新空间配额(UpdateQuota) - 通过 ID 更新配额(UpdateQuotaById) |
RecycledOperation | 回收站管理(文档) - 列出回收站内容(RecycleList) - 获取回收站项目信息(RecycleInfo) - 恢复回收站项目(RecycleRestore) - 批量恢复回收站项目(RecycleRestoreBatch) - 永久删除回收站项目(RecyclePurge) - 批量永久删除(RecyclePurgeBatch) - 清空回收站(RecycleEmpty) - 设置回收站生命周期(RecycleSetLifecycle) - 预览回收站文件(RecyclePreview) |
SearchOperation | 搜索功能(文档) - 搜索目录与文件(SearchFs) - 支持按文件名、标签、类型等条件搜索 - 支持搜索结果分页 |
FavoriteOperation | 收藏管理(文档) - 添加收藏(CreateFavorite) - 删除收藏(DeleteFavorite) - 列出收藏列表(ListFavorite) |
RecentOperation | 最近使用文件(文档) - 列出最近使用的文件(ListRecentlyUsedFile) |
HistoryOperation | 历史版本管理(文档) - 列出文件历史版本(ListHistory) - 删除历史版本(DeleteHistory) - 设置为最新版本(SetHistoryLatest) - 清空历史版本(EmptyHistory) - 获取历史版本配置(GetHistoryConfig) - 设置历史版本配置(SetHistoryConfig) |
TaskOperation | 任务查询 - 查询空间任务(QueryTaskV2) - 查询媒体库任务(QueryLibraryTaskV2) |
UsageOperation | 容量统计(文档) - 获取空间使用量(GetUsage) - 获取媒体库使用量(GetLibraryUsage) |
HlsOperation | 媒体处理 - 获取媒体文件信息(GetMediaFileInfo) - 创建转码任务(CreateTranscodeTask) - 实时转码媒体文件(LiveTranscodeMediaFile) |
SDK 提供了高级的文件传输模块(上传文档 | 下载文档),支持:
#include <iostream>
#include "smhcppsdk/smh_client.h"
#include "smhcppsdk/smh_client_option.h"
#include "smhcppsdk/smh_client_file_transfer_manager.h"
using namespace TencentCloud::Smh;
int main() {
// 创建配置和客户端(参考上面的初始化步骤)
std::vector<Option> options = {
WithEndpoint("https://smhxxx.api.tencentsmh.cn")
};
SmhClient client(options);
// 创建文件传输管理器
SmhClientFileTransferManager transfer_manager(&client);
// 方式1: 从本地文件上传
SmhClientFileTransferManager::UploadFileArgs upload_args;
upload_args.library_id = "your-library-id";
upload_args.space_id = "your-space-id";
upload_args.file_path = "/remote/path/file.txt"; // 远端文件路径
upload_args.access_token = "your-access-token";
upload_args.conflict_resolution_strategy = "rename";
upload_args.with_inode = 1;
// 设置进度回调
auto progress_callback = [](int64_t transferred_bytes, int64_t total_bytes) -> FileTransferStatus {
double progress = static_cast<double>(transferred_bytes) / total_bytes * 100;
std::cout << "\rUpload progress: " << progress << "%" << std::flush;
return FileTransferStatus::CONTINUE; // 返回 ABORT 可中止上传
};
SmhClientFileTransferManager::UploadFileResult result;
int ret = transfer_manager.UploadFileFromLocalFile(
upload_args, "/local/path/file.txt", &result, progress_callback);
if (ret == 0) {
std::cout << "\nUpload completed! Name=" << result.name
<< ", Inode=" << result.inode
<< ", Size=" << result.size << std::endl;
} else {
std::cerr << "\nUpload failed: " << transfer_manager.GetLastError().ToString() << std::endl;
}
// 方式2: 从内存上传
std::string content = "Hello, SMH!";
SmhClientFileTransferManager::UploadFileArgs mem_args;
mem_args.library_id = "your-library-id";
mem_args.space_id = "your-space-id";
mem_args.file_path = "/temp/hello.txt";
mem_args.access_token = "your-access-token";
SmhClientFileTransferManager::UploadFileResult mem_result;
ret = transfer_manager.UploadFileFromMemory(mem_args, content, &mem_result);
if (ret == 0) {
std::cout << "Memory upload completed! Name=" << mem_result.name
<< ", Inode=" << mem_result.inode << std::endl;
} else {
std::cerr << "Memory upload failed: " << transfer_manager.GetLastError().ToString() << std::endl;
}
return 0;
}
#include <iostream>
#include <fstream>
#include "smhcppsdk/smh_client.h"
#include "smhcppsdk/smh_client_option.h"
#include "smhcppsdk/smh_client_file_transfer_manager.h"
using namespace TencentCloud::Smh;
int main() {
// 创建配置和客户端(参考上面的初始化步骤)
std::vector<Option> options = {
WithEndpoint("https://smhxxx.api.tencentsmh.cn")
};
SmhClient client(options);
// 创建文件传输管理器
SmhClientFileTransferManager transfer_manager(&client);
// 方式1: 下载到本地文件
SmhClientFileOperation::DownloadFileArgs download_args;
download_args.library_id = "your-library-id";
download_args.space_id = "your-space-id";
download_args.file_path = "/remote/path/file.txt"; // 远端文件路径
download_args.access_token = "your-access-token";
// 设置进度回调
auto progress_callback = [](int64_t transferred_bytes, int64_t total_bytes) -> FileTransferStatus {
double progress = static_cast<double>(transferred_bytes) / total_bytes * 100;
std::cout << "\rDownload progress: " << progress << "%" << std::flush;
return FileTransferStatus::CONTINUE; // 返回 ABORT 可中止下载
};
int ret = transfer_manager.DownloadFileIntoLocalFile(
download_args, "/local/path/file.txt", progress_callback);
if (ret == 0) {
std::cout << "\nDownload completed! File saved to: /local/path/file.txt" << std::endl;
} else {
std::cerr << "\nDownload failed: " << transfer_manager.GetLastError().ToString() << std::endl;
}
// 方式2: 下载到内存(适用于小文件)
SmhClientFileOperation::DownloadFileArgs mem_args;
mem_args.library_id = "your-library-id";
mem_args.space_id = "your-space-id";
mem_args.file_path = "/remote/path/config.json";
mem_args.access_token = "your-access-token";
std::string file_content;
ret = transfer_manager.DownloadFileIntoMemory(mem_args, &file_content, progress_callback);
if (ret == 0) {
std::cout << "\nDownloaded " << file_content.size() << " bytes" << std::endl;
std::cout << "Content: " << file_content << std::endl;
} else {
std::cerr << "\nDownload to memory failed: " << transfer_manager.GetLastError().ToString() << std::endl;
}
return 0;
}
MIT License