没有合适的资源?快使用搜索试试~ 我知道了~
百度pcs操作实例c语言版.doc
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 91 浏览量
2022-07-03
04:13:41
上传
评论
收藏 116KB DOC 举报
温馨提示
试读
25页
百度pcs操作实例c语言版
资源推荐
资源详情
资源评论
百度 pcs 操作实例 c 语言版
闲着没事,写了个操作百度 pcs 的例子,实现了上传,下载,删除等基本功能,
百度 pcs 实现起来比较简单,只有在实现上传的时候遇到点麻烦, upload 中 file
的描述不够明确。
下面是主要代码,使用了 cJSON 和 libcurl。
main.c
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "trans_inf.h"
#include "cJSON.h"
#define ROOT_DIR "/apps/zztpcs" //这是我应用的目录名,需要改成自己的
#define PCS_DEBUG printf
char* access_token = "xxxx"; //access token,我固定了
struct pcs_stat
{
int isdir;
long long size;
int ctime;
int mtime;
};
char curdir[512] = ROOT_DIR;
int parse_pcs_stat(char* msg, struct pcs_stat* buf)
{
int ret = -1;
int i;
cJSON *root = NULL;
cJSON *list = NULL;
cJSON *item = NULL;
cJSON *node = NULL;
memset(buf, 0, sizeof(struct pcs_stat));
root = cJSON_Parse(msg);
if(NULL == root)
{
return;
}
node = cJSON_GetObjectItem(root,"request_id");
剑道独尊
www.wo-cn.com
if(NULL == node)
{
goto END;
}
node = cJSON_GetObjectItem(root,"error_msg");
if(node)
{
printf("err:%s\n", node->valuestring);
goto END;
}
list = cJSON_GetObjectItem(root,"list");
if(NULL == list)
{
goto END;
}
for(i=0;i<cJSON_GetArraySize(list);i++)
{
item=cJSON_GetArrayItem(list,i);
if(item)
{
node = cJSON_GetObjectItem(item,"isdir");
if(node)
{
buf->isdir = node->valueint;
}
node = cJSON_GetObjectItem(item,"ctime");
if(node)
{
buf->ctime = node->valueint;
}
node = cJSON_GetObjectItem(item,"mtime");
if(node)
{
buf->mtime = node->valueint;
}
node = cJSON_GetObjectItem(item,"size");
if(node)
剑道独尊
www.wo-cn.com
{
buf->size = node->valueint;
}
}
}
ret = 0;
END:
if(root)
{
cJSON_Delete(root);
}
return ret;
}
int pcs_stat(char* path, struct pcs_stat* buf)
{
int ret = -1;
char cmdbuf[1024];
char* msg = NULL;
sprintf(cmdbuf, "https://pcs.baidu.com/rest/2.0/pcs/file?
method=meta&access_token=%s&path=%s",
access_token,
path);
getcmd(cmdbuf, &msg);
{
if(msg)
{
if(parse_pcs_stat(msg, buf) == 0)
{
ret = 0;
}
free(msg);
msg = NULL;
}
}
return ret;
}
剑道独尊
www.wo-cn.com
int parse_mkdir(char* msg)
{
int ret = -1;
cJSON *root = NULL;
cJSON *node = NULL;
root = cJSON_Parse(msg);
if(NULL == root)
{
return;
}
node = cJSON_GetObjectItem(root,"request_id");
if(NULL == node)
{
goto END;
}
node = cJSON_GetObjectItem(root,"error_msg");
if(node)
{
printf("err:%s\n", node->valuestring);
goto END;
}
node = cJSON_GetObjectItem(root,"fs_id");
if(NULL == node)
{
goto END;
}
ret = 0;
END:
if(root)
{
cJSON_Delete(root);
}
return ret;
}
int pcs_mkdir(char* path)
{
int ret = -1;
char cmdbuf[1024];
剑道独尊
www.wo-cn.com
char* msg = NULL;
sprintf(cmdbuf, "https://pcs.baidu.com/rest/2.0/pcs/file?
method=mkdir&access_token=%s&path=%s",
access_token,
path);
postcmd(cmdbuf, "a", &msg);
{
if(msg)
{
if(parse_mkdir(msg) == 0)
{
ret = 0;
}
free(msg);
msg = NULL;
}
}
return ret;
}
int parse_ls(char* msg)
{
int ret = -1;
int i;
cJSON *root = NULL;
cJSON *list = NULL;
cJSON *item = NULL;
cJSON *node = NULL;
root = cJSON_Parse(msg);
if(NULL == root)
{
return;
}
node = cJSON_GetObjectItem(root,"request_id");
if(NULL == node)
{
goto END;
}
node = cJSON_GetObjectItem(root,"error_msg");
if(node)
{
printf("err:%s\n", node->valuestring);
goto END;
剑道独尊
www.wo-cn.com
剩余24页未读,继续阅读
资源评论
oligaga
- 粉丝: 52
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功