Skip to content

Commit

Permalink
now sdk could store multiple regions cache entries
Browse files Browse the repository at this point in the history
  • Loading branch information
bachue committed Apr 11, 2024
1 parent 61a722c commit de96cfd
Show file tree
Hide file tree
Showing 62 changed files with 2,808 additions and 1,180 deletions.
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ ENDIF()

AUX_SOURCE_DIRECTORY(b64 DIR_SRCS_B64)
AUX_SOURCE_DIRECTORY(cJSON DIR_SRCS_CJSON)
AUX_SOURCE_DIRECTORY(hashmap DIR_SRCS_HASHMAP)
AUX_SOURCE_DIRECTORY(qiniu DIR_SRCS_QINIU)
MESSAGE(STATUS "Src file: ${DIR_SRCS_B64} ${DIR_SRCS_CJSON} ${DIR_SRCS_QINIU}")
MESSAGE(STATUS "Src file: ${DIR_SRCS_B64} ${DIR_SRCS_CJSON} ${DIR_SRCS_HASHMAP} ${DIR_SRCS_QINIU}")

# 编译可执行程序
# ADD_EXECUTABLE(${PROJECT_NAME} ${DIR_SRCS})
# 如果要生成静态链接库
ADD_LIBRARY(${PROJECT_NAME}_static STATIC ${DIR_SRCS_B64} ${DIR_SRCS_CJSON} ${DIR_SRCS_QINIU})
ADD_LIBRARY(${PROJECT_NAME}_static STATIC ${DIR_SRCS_B64} ${DIR_SRCS_CJSON} ${DIR_SRCS_HASHMAP} ${DIR_SRCS_QINIU})

# 如果要生成动态链接库
ADD_LIBRARY(${PROJECT_NAME} SHARED ${DIR_SRCS_B64} ${DIR_SRCS_CJSON} ${DIR_SRCS_QINIU})
ADD_LIBRARY(${PROJECT_NAME} SHARED ${DIR_SRCS_B64} ${DIR_SRCS_CJSON} ${DIR_SRCS_HASHMAP} ${DIR_SRCS_QINIU})
TARGET_INCLUDE_DIRECTORIES(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

LIST(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
Expand Down
70 changes: 45 additions & 25 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@ C-SDK 使用 [cURL](http://curl.haxx.se/) 进行网络相关操作。无论是

如果在项目构建过程中出现环境相关的编译错误和链接错误,请确认这些选项是否都已经正确配置,以及所依赖的库是否都已经正确的安装。

#### 通过 CMake 编译

如果想在 CMake 项目里使用 C-SDK,可以直接在 CMake 里导入 C-SDK:

```cmake
INCLUDE(FetchContent)
FetchContent_Declare(
qiniu
GIT_REPOSITORY https://github.com/qiniu/c-sdk.git
GIT_TAG v7.7.0
)
FetchContent_MakeAvailable(qiniu)
FIND_PACKAGE(CURL REQUIRED)
FIND_PACKAGE(OpenSSL REQUIRED)
TARGET_LINK_LIBRARIES(${PROJECT_NAME} PRIVATE qiniu m)
TARGET_LINK_LIBRARIES(${PROJECT_NAME} PRIVATE ${CURL_LIBRARIES})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} PRIVATE ${OPENSSL_LIBRARIES})
```

<a name="appkey"></a>

Expand Down Expand Up @@ -146,7 +166,7 @@ void stat(Qiniu_Client* client, const char* bucket, const char* key)
{
Qiniu_RS_StatRet ret;
Qiniu_Error err = Qiniu_RS_Stat(client, &ret, bucket, key);
if (err.code != 200) {
if (err.code != Qiniu_OK.code) {
debug(client, err);
return;
}
Expand All @@ -169,7 +189,7 @@ typedef struct _Qiniu_Error {
typedef struct _Qiniu_RS_StatRet {
const char* hash;
const char* mimeType;
Qiniu_Int64 fsize;
Qiniu_Int64 fsize;
Qiniu_Int64 putTime;
} Qiniu_RS_StatRet;
```
Expand Down Expand Up @@ -259,7 +279,7 @@ char* upload(Qiniu_Client* client, char* uptoken, const char* key, const char* l
Qiniu_Error err;
Qiniu_Io_PutRet putRet;
err = Qiniu_Io_PutFile(client, &putRet, uptoken, key, localFile, NULL);
if (err.code != 200) {
if (err.code != Qiniu_OK.code) {
debug(client, err);
return NULL;
}
Expand Down Expand Up @@ -413,7 +433,7 @@ void stat(Qiniu_Client* client, const char* bucket, const char* key)
{
Qiniu_RS_StatRet ret;
Qiniu_Error err = Qiniu_RS_Stat(client, &ret, bucket, key);
if (err.code != 200) {
if (err.code != Qiniu_OK.code) {
debug(client, err);
return;
}
Expand All @@ -427,7 +447,7 @@ void stat(Qiniu_Client* client, const char* bucket, const char* key)
typedef struct _Qiniu_RS_StatRet {
const char* hash;
const char* mimeType;
Qiniu_Int64 fsize;
Qiniu_Int64 fsize;
Qiniu_Int64 putTime;
} Qiniu_RS_StatRet;
```
Expand All @@ -442,7 +462,7 @@ typedef struct _Qiniu_RS_StatRet {
void delete(Qiniu_Client* client, const char* bucket, const char* key)
{
Qiniu_Error err = Qiniu_RS_Delete(client, bucket, key);
if (err.code != 200) {
if (err.code != Qiniu_OK.code) {
debug(client, err);
return;
}
Expand All @@ -456,12 +476,12 @@ void delete(Qiniu_Client* client, const char* bucket, const char* key)
复制和移动操作,需要指定源路径和目标路径。

```{c}
void copy(Qiniu_Client* client,
const char* bucketSrc, const char* keySrc,
void copy(Qiniu_Client* client,
const char* bucketSrc, const char* keySrc,
const char* bucketDest, const char* keyDest)
{
Qiniu_Error err = Qiniu_RS_Copy(client, bucketSrc, keySrc, bucketDest, keyDest);
if (err.code != 200) {
if (err.code != Qiniu_OK.code) {
debug(client, err);
return;
}
Expand All @@ -470,12 +490,12 @@ void copy(Qiniu_Client* client,
```

```{c}
void move(Qiniu_Client* client,
const char* bucketSrc, const char* keySrc,
void move(Qiniu_Client* client,
const char* bucketSrc, const char* keySrc,
const char* bucketDest, const char* keyDest)
{
Qiniu_Error err = Qiniu_RS_Move(client, bucketSrc, keySrc, bucketDest, keyDest);
if (err.code != 200) {
if (err.code != Qiniu_OK.code) {
debug(client, err);
return;
}
Expand All @@ -494,7 +514,7 @@ void move(Qiniu_Client* client,
调用`Qiniu_RS_BatchStat`可以批量查看多个文件的属性信息。

```{c}
void batchStat(Qiniu_Client* client,
void batchStat(Qiniu_Client* client,
Qiniu_RS_EntryPath* entries, Qiniu_ItemCount entryCount)
{
Qiniu_RS_BatchStatRet* rets = calloc(entryCount, sizeof(Qiniu_RS_BatchStatRet));
Expand All @@ -504,7 +524,7 @@ void batchStat(Qiniu_Client* client,
while (curr < entryCount) {
printf("\ncode: %d\n", rets[curr].code);
if (rets[curr].code != 200) {
if (rets[curr].code != Qiniu_OK.code) {
printf("error: %s\n", rets[curr].error);
} else {
printf("hash: %s\n", rets[curr].data.hash);
Expand All @@ -517,7 +537,7 @@ void batchStat(Qiniu_Client* client,
free(rets);
if (err.code != 200) {
if (err.code != Qiniu_OK.code) {
debug(client, err);
return;
}
Expand Down Expand Up @@ -551,7 +571,7 @@ typedef struct _Qiniu_RS_BatchStatRet {
typedef struct _Qiniu_RS_StatRet {
const char* hash;
const char* mimeType;
Qiniu_Int64 fsize;
Qiniu_Int64 fsize;
Qiniu_Int64 putTime;
} Qiniu_RS_StatRet;
```
Expand All @@ -563,7 +583,7 @@ typedef struct _Qiniu_RS_StatRet {
调用`Qiniu_RS_BatchDelete`可以批量删除多个文件。

```{c}
void batchDelete(Qiniu_Client* client,
void batchDelete(Qiniu_Client* client,
Qiniu_RS_EntryPath* entries, Qiniu_ItemCount entryCount)
{
Qiniu_RS_BatchItemRet* rets = calloc(entryCount, sizeof(Qiniu_RS_BatchItemRet));
Expand All @@ -573,15 +593,15 @@ void batchDelete(Qiniu_Client* client,
while (curr < entryCount) {
printf("\ncode: %d\n", rets[curr].code);
if (rets[curr].code != 200) {
if (rets[curr].code != Qiniu_OK.code) {
printf("error: %s\n", rets[curr].error);
}
curr++;
}
free(rets);
if (err.code != 200) {
if (err.code != Qiniu_OK.code) {
debug(client, err);
return;
}
Expand All @@ -604,7 +624,7 @@ typedef struct _Qiniu_RS_BatchItemRet {
调用`Qiniu_RS_BatchCopy`可以批量复制多个文件。

```{c}
void batchCopy(Qiniu_Client* client,
void batchCopy(Qiniu_Client* client,
Qiniu_RS_EntryPathPair* entryPairs, Qiniu_ItemCount entryCount)
{
Qiniu_RS_BatchItemRet* rets = calloc(entryCount, sizeof(Qiniu_RS_BatchItemRet));
Expand All @@ -614,14 +634,14 @@ void batchCopy(Qiniu_Client* client,
while (curr < entryCount) {
printf("\ncode: %d\n", rets[curr].code);
if (rets[curr].code != 200) {
if (rets[curr].code != Qiniu_OK.code) {
printf("error: %s\n", rets[curr].error);
}
curr++;
}
free(rets);
if (err.code != 200) {
if (err.code != Qiniu_OK.code) {
debug(client, err);
return;
}
Expand All @@ -644,7 +664,7 @@ typedef struct _Qiniu_RS_EntryPathPair {
批量移动和批量复制很类似,唯一的区别就是调用`Qiniu_RS_BatchMove`

```{c}
void batchMove(Qiniu_Client* client,
void batchMove(Qiniu_Client* client,
Qiniu_RS_EntryPathPair* entryPairs, Qiniu_ItemCount entryCount)
{
Qiniu_RS_BatchItemRet* rets = calloc(entryCount, sizeof(Qiniu_RS_BatchItemRet));
Expand All @@ -654,15 +674,15 @@ void batchMove(Qiniu_Client* client,
while (curr < entryCount) {
printf("\ncode: %d\n", rets[curr].code);
if (rets[curr].code != 200) {
if (rets[curr].code != Qiniu_OK.code) {
printf("error: %s\n", rets[curr].error);
}
curr++;
}
free(rets);
if (err.code != 200) {
if (err.code != Qiniu_OK.code) {
debug(client, err);
return;
}
Expand Down
20 changes: 10 additions & 10 deletions docs/gist/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const char bucket[] = "a";

/* @gist debug */
void debug(Qiniu_Client* client, Qiniu_Error err)
void debug(Qiniu_Client *client, Qiniu_Error err)
{
printf("error code: %d, message: %s\n", err.code, err.message);
printf("respose header:\n%s", Qiniu_Buffer_CStr(&client->respHeader));
Expand All @@ -13,12 +13,13 @@ void debug(Qiniu_Client* client, Qiniu_Error err)
/* @endgist */

/* @gist upload */
char* upload(Qiniu_Client* client, char* uptoken, const char* key, const char* localFile)
char *upload(Qiniu_Client *client, char *uptoken, const char *key, const char *localFile)
{
Qiniu_Error err;
Qiniu_Io_PutRet putRet;
err = Qiniu_Io_PutFile(client, &putRet, uptoken, key, localFile, NULL);
if (err.code != 200) {
if (err.code != Qiniu_OK.code)
{
debug(client, err);
return NULL;
}
Expand All @@ -27,7 +28,7 @@ char* upload(Qiniu_Client* client, char* uptoken, const char* key, const char* l
/* @endgist */

/* @gist simple-upload */
int simple_upload(Qiniu_Client* client, char* uptoken, const char* key, const char* localFile)
int simple_upload(Qiniu_Client *client, char *uptoken, const char *key, const char *localFile)
{
Qiniu_Error err;
err = Qiniu_Io_PutFile(client, NULL, uptoken, key, localFile, NULL);
Expand All @@ -36,7 +37,7 @@ int simple_upload(Qiniu_Client* client, char* uptoken, const char* key, const ch
/* @endgist */

/* @gist resumable-upload */
int resumable_upload(Qiniu_Client* client, char* uptoken, const char* key, const char* localFile)
int resumable_upload(Qiniu_Client *client, char *uptoken, const char *key, const char *localFile)
{
Qiniu_Error err;
Qiniu_Rio_PutExtra extra;
Expand All @@ -52,13 +53,12 @@ int main()
/* @gist init */
Qiniu_Client client;

Qiniu_Global_Init(-1); /* 全局初始化函数,整个进程只需要调用一次 */
Qiniu_Global_Init(-1); /* 全局初始化函数,整个进程只需要调用一次 */
Qiniu_Client_InitNoAuth(&client, 1024); /* HTTP客户端初始化。HTTP客户端是线程不安全的,不要在多个线程间共用 */
/* @endgist */

/* @gist init */
Qiniu_Client_Cleanup(&client); /* 每个HTTP客户端使用完后释放 */
Qiniu_Global_Cleanup(); /* 全局清理函数,只需要在进程退出时调用一次 */
/* @endgist */
Qiniu_Client_Cleanup(&client); /* 每个HTTP客户端使用完后释放 */
Qiniu_Global_Cleanup(); /* 全局清理函数,只需要在进程退出时调用一次 */
/* @endgist */
}

Loading

0 comments on commit de96cfd

Please sign in to comment.