Skip to content

Commit 72c9336

Browse files
committed
易用性优化:
1、path、fileName合并为一个参数 2、文件、目录操作分开 3、增加超时参数 4、首尾'/'检查 5、prefixSearch增加一个接口 6、命名空间修改,防止用户同时使用cos、图片、视频会冲突 7、签名有效期改为1分钟 8、签名传入参数改为相对path,相对path不带appid、bucket前缀
1 parent 7ffbcd6 commit 72c9336

File tree

8 files changed

+382
-178
lines changed

8 files changed

+382
-178
lines changed

README.md

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,44 +44,67 @@ g++ -o sample sample.cpp -I ./include/ -L. -L../cos-cpp-sdk/lib/ -lcosdk -lcurl
4444
Cosapi::global_init();
4545
Cosapi api("your appid",
4646
"your secretId",
47-
"your secretKey");
47+
"your secretKey",
48+
"interface timeout");
4849

4950
注意cos上的path以 / 开头
5051

5152
##计算多次签名,静态函数任何地方可以直接调用
52-
string sign = Auth::appSign_more(
53+
string sign = Auth::appSign(
5354
"your appid", "",
5455
"your secretId",
55-
123, "bucketName");
56+
"expired unix timestamp",
57+
"bucketName");
5658

5759
##创建目录
58-
//注意path以 / 结尾
5960
api.createFolder(
6061
"bucketName", "/test/");
61-
cout << "retJson:" << api.retJson << endl;
62-
cout << "retCode:" << api.retCode << endl;
63-
cout << "retMsg:" << api.retMsg << endl;
64-
65-
##删除文件或者目录,目录必须以 / 结尾
66-
api.del(
62+
api.dump_res();
63+
64+
##listFolder目录下文件列表
65+
api.listFolder("bucketName", "/", 10);
66+
api.dump_res();
67+
68+
##prefixSearch前缀搜索
69+
api.prefixSearch("bucketName", "/test", 10);
70+
api.dump_res();
71+
72+
##更新目录属性
73+
api.updateFolder(
74+
bucketName, "/test/", "attr");
75+
api.dump_res();
76+
77+
##更新文件属性
78+
api.update(
79+
bucketName, "/test.log", "attr");
80+
api.dump_res();
81+
82+
##statFolder查询目录
83+
api.statFolder(
84+
bucketName, "/test/");
85+
api.dump_res();
86+
87+
##stat查询文件
88+
//可以用来判断文件是否存在
89+
api.stat(
90+
bucketName, "/test.log");
91+
api.dump_res();
92+
93+
##删除目录
94+
api.deleteFolder(
6795
"bucketName", "/test/");
68-
cout << "retJson:" << api.retJson << endl;
69-
cout << "retCode:" << api.retCode << endl;
70-
cout << "retMsg:" << api.retMsg << endl;
96+
api.dump_res();
7197

72-
##list文件列表
73-
api.list("bucketName", "/", 10);
74-
cout << "retJson:" << api.retJson << endl;
75-
cout << "retCode:" << api.retCode << endl;
76-
cout << "retMsg:" << api.retMsg << endl;
98+
##删除文件
99+
api.del(
100+
"bucketName", "/test.log");
101+
api.dump_res();
77102

78103
##上传文件
79104
api.upload(
80105
"srcPath", "bucketName",-
81106
"/dstPath");
82-
cout << "retJson:" << api.retJson << endl;
83-
cout << "retCode:" << api.retCode << endl;
84-
cout << "retMsg:" << api.retMsg << endl;
107+
api.dump_res();
85108

86109
##大文件分片上传
87110
//sliceSize参数可以指定分片大小,默认是 512KB
@@ -90,4 +113,5 @@ g++ -o sample sample.cpp -I ./include/ -L. -L../cos-cpp-sdk/lib/ -lcosdk -lcurl
90113
api.upload_slice(
91114
"srcPath", "bucketName",-
92115
"/dstPath", "", 3*1024*1024);
116+
api.dump_res();
93117

include/Auth.h

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,31 @@
66
#include <stdint.h>
77
using namespace std;
88

9-
namespace Tencentyun {
9+
namespace Qcloud_cos{
1010

1111
class Auth
1212
{
1313
static const int AUTH_URL_FORMAT_ERROR;
1414
static const int AUTH_SECRET_ID_KEY_ERROR;
15+
public:
1516

1617
/**
17-
* 签名函数(上传、下载、查看资源会生成多次有效签名,
18-
* 更新、删除资源会生成单次有效签名)
19-
* @param uint64_t appId
20-
* @param string secretId
21-
* @param string secretKey
22-
* @param uint64_t expired 过期时间,unix时间戳
23-
* @param string fileId 文件路径,以 /{$appId}/{$bucketName} 开头
18+
* 生成多次有效签名函数(用于上传和下载资源,有效期内可重复对不同资源使用)
19+
* @param uint64_t expired 过期时间,unix时间戳
2420
* @param string bucketName 文件所在bucket
2521
*
2622
* @return string sign 签名
2723
*/
28-
29-
public:
30-
3124
static string appSign(
3225
const uint64_t appId,
3326
const string &secretId,
3427
const string &secretKey,
3528
const uint64_t expired,
36-
const string &fileId,
37-
const string &bucketName);
38-
29+
const string &bucketName);
3930

4031
/**
41-
* 生成单次有效签名函数(用于删除和更新指定fileId资源,使用一次即失效)
42-
* @param string fileId 文件路径,以 /{$appId}/{$bucketName} 开头
32+
* 生成单次有效签名函数(用于删除和更新指定path的资源,使用一次即失效)
33+
* @param string path 文件路径,以 '/' 开头
4334
* @param string bucketName 文件所在bucket
4435
*
4536
* @return string sign 签名
@@ -48,26 +39,34 @@ class Auth
4839
const uint64_t appId,
4940
const string &secretId,
5041
const string &secretKey,
51-
const string &fileId,
42+
const string &path,
5243
const string &bucketName);
5344

45+
private:
5446
/**
55-
* 生成多次有效签名函数(用于上传和下载资源,有效期内可重复对不同资源使用)
56-
* @param uint64_t expired 过期时间,unix时间戳
47+
* 签名函数(上传、下载、查看资源需要多次有效签名,
48+
* 更新、删除资源需要单次有效签名)
49+
* @param uint64_t appId
50+
* @param string secretId
51+
* @param string secretKey
52+
* @param uint64_t expired 过期时间,unix时间戳
53+
* @param string fileId 资源全路径,格式为 /{$appId}/{$bucketName}/${path}
5754
* @param string bucketName 文件所在bucket
5855
*
5956
* @return string sign 签名
6057
*/
61-
static string appSign_more(
58+
59+
static string appSignBase(
6260
const uint64_t appId,
6361
const string &secretId,
6462
const string &secretKey,
6563
const uint64_t expired,
66-
const string &bucketName);
64+
const string &fileId,
65+
const string &bucketName);
6766

6867

6968
};
7069

71-
}
70+
}//namespace Qcloud_cos
7271

7372
#endif

include/Cosapi.h

Lines changed: 96 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
using namespace std;
1515

16-
namespace Tencentyun {
16+
namespace Qcloud_cos{
1717

1818
class Cosapi
1919
{
@@ -45,7 +45,8 @@ class Cosapi
4545
Cosapi(
4646
const uint64_t appid,
4747
const string &secretId,
48-
const string &secretKey);
48+
const string &secretKey,
49+
const uint64_t timeout = 30);
4950
virtual ~Cosapi();
5051

5152

@@ -92,7 +93,7 @@ class Cosapi
9293
/*
9394
* 创建目录
9495
* @param string bucketName
95-
* @param string path 目录路径,必须以‘/’结尾
96+
* @param string path 目录路径,sdk会补齐末尾的 '/'
9697
*
9798
*/
9899
int createFolder(
@@ -102,17 +103,16 @@ class Cosapi
102103
);
103104

104105
/*
105-
* 目录列表,前缀搜索
106+
* 目录列表
106107
* @param string bucketName
107-
* @param string path 目录路径 web.file.myqcloud.com/files/v1/[appid]/[bucket_name]/[DirName]/
108-
* web.file.myqcloud.com/files/v1/appid/[bucket_name]/[DirName]/[prefix] <- 如果填写prefix, 则列出含此前缀的所有文件
108+
* @param string path 目录路径,sdk会补齐末尾的 '/'
109+
* @param int num 拉取的总数
109110
* @param string pattern eListBoth,ListDirOnly,eListFileOnly 默认both
110111
* @param string offset 透传字段,用于翻页,前端不需理解,需要往前/往后翻页则透传回来
111-
* @param int num 拉取的总数
112112
* @param int order 默认正序(=0), 填1为反序,
113113
*
114114
*/
115-
int list(
115+
int listFolder(
116116
const string &bucketName,
117117
const string &path,
118118
const int num = 20,
@@ -122,9 +122,40 @@ class Cosapi
122122
);
123123

124124
/*
125-
* 目录/文件信息 update
125+
* 前缀搜索
126126
* @param string bucketName
127-
* @param string path 路径,如果是目录则必须以‘/’结尾
127+
* @param string prefix 列出含此前缀的所有文件
128+
* @param int num 拉取的总数
129+
* @param string pattern eListBoth,ListDirOnly,eListFileOnly 默认both
130+
* @param string offset 透传字段,用于翻页,前端不需理解,需要往前/往后翻页则透传回来
131+
* @param int order 默认正序(=0), 填1为反序,
132+
*
133+
*/
134+
int prefixSearch(
135+
const string &bucketName,
136+
const string &prefix,
137+
const int num = 20,
138+
const string &pattern = "eListBoth",
139+
const int order = 0,
140+
const string &offset = ""
141+
);
142+
143+
/*
144+
* 目录信息 updateFolder
145+
* @param string bucketName
146+
* @param string path 路径,sdk会补齐末尾的 '/'
147+
*
148+
*/
149+
int updateFolder(
150+
const string &bucketName,
151+
const string &path,
152+
const string &biz_attr = ""
153+
);
154+
155+
/*
156+
* 文件信息 update
157+
* @param string bucketName
158+
* @param string path 路径
128159
*
129160
*/
130161
int update(
@@ -134,9 +165,20 @@ class Cosapi
134165
);
135166

136167
/*
137-
* 目录/文件信息 查询
168+
* 目录信息 查询
138169
* @param string bucketName
139-
* @param string path 路径,如果是目录则必须以‘/’结尾
170+
* @param string path 路径,sdk会补齐末尾的 '/'
171+
*
172+
*/
173+
int statFolder(
174+
const string &bucketName,
175+
const string &path
176+
);
177+
178+
/*
179+
* 文件信息 查询
180+
* @param string bucketName
181+
* @param string path 路径
140182
*
141183
*/
142184
int stat(
@@ -145,7 +187,18 @@ class Cosapi
145187
);
146188

147189
/*
148-
* 删除文件及目录
190+
* 删除目录
191+
* @param string bucketName
192+
* @param string path 路径,sdk会补齐末尾的 '/'
193+
*
194+
*/
195+
int delFolder(
196+
const string &bucketName,
197+
const string &path
198+
);
199+
200+
/*
201+
* 删除文件
149202
* @param string bucketName
150203
* @param string path 路径,如果是目录则必须以‘/’结尾
151204
*
@@ -158,6 +211,8 @@ class Cosapi
158211
void dump_res();
159212

160213
private:
214+
string validFolderPath(const string &path);
215+
string validFilePath(const string &path);
161216
int upload_prepare(
162217
const uint64_t fileSize,
163218
const string &sha,
@@ -178,8 +233,35 @@ class Cosapi
178233
const uint64_t offset,
179234
const string &session
180235
);
236+
237+
int listBase(
238+
const string &bucketName,
239+
const string &path,
240+
const int num = 20,
241+
const string &pattern = "eListBoth",
242+
const int order = 0,
243+
const string &offset = ""
244+
);
245+
246+
int updateBase(
247+
const string &bucketName,
248+
const string &path,
249+
const string &biz_attr = ""
250+
);
251+
252+
int statBase(
253+
const string &bucketName,
254+
const string &path
255+
);
256+
257+
int delBase(
258+
const string &bucketName,
259+
const string &path
260+
);
261+
181262
protected:
182263
CURL * _curl_handle;
264+
uint64_t _timeout;
183265

184266
public:
185267
string response_str;
@@ -191,6 +273,6 @@ class Cosapi
191273
};
192274

193275

194-
}//namespace Tencentyun
276+
}//namespace Qcloud_cos
195277

196278
#endif

0 commit comments

Comments
 (0)