最近百度網盤將空間升級到了永久1TB,鑒於百度的實力用做數據備份空間不錯,不過百度網盤沒有Linux下的客戶端,上傳管理文件需通過百度開放雲平台訪問PCS資源的系列接口。
1.首先加入百度開發者:http://developer.baidu.com/dev#/create
2.任意創建一個應用獲取API Key並開通PCS API權限,開放API > API管理 > API列表 > API服務 > PCS API > 開啟
3.通過剛剛新建應用的API Key獲取device code和user_code
|
1
2
|
curl -k -L -d
"client_id=<api_key>&response_type=device_code&scope=basic,netdisk"
\
"https://openapi.baidu.com/oauth/2.0/device/code"
|
4.在瀏覽器打開https://openapi.baidu.com/device輸入獲取到的user_code並連接,然后通過device code獲取refresh_token和access_token:
|
1
2
|
curl -k -L -d
"grant_type=device_token&code=<device_code>&client_id=<api_key>&client_secret=<api_secret>"
\
"https://openapi.baidu.com/oauth/2.0/token"
|
5.此時通過access_token就可對網盤文件進行操作了,獲取到access_token的有效期為30天,過期后通過refresh_token重新獲取access_token
|
1
2
3
|
curl -k -L -d "grant_type=refresh_token&refresh_token=<refresh_token>&client_id=<api_key>\
&client_secret=<api_secret>"\
"https://openapi.baidu.com/oauth/2.0/token"
|
查看網盤配額:
|
1
2
|
curl -k -L "https:
//pcs
.baidu.com
/rest/2
.0
/pcs/quota
?method=\
info&access_token=<access_token>"
|
上傳文件:
|
1
2
|
curl -k -L -F
"file=@haiyun.me.tar.gz"
"https:
//c
.pcs.baidu.com
/rest/2
.0
/pcs/file
?method=upload&\
access_token=<access_token>&path=
/apps/pcsupload/haiyun
.me.
tar
.gz"
|
下載文件:
|
1
2
|
curl -k -O "https:
//d
.pcs.baidu.com
/rest/2
.0
/pcs/file
?method=download&access_token=<access_token>&\
path=
/apps/pcsupload/haiyun
.me.
tar
.gz"
|
刪除文件:
|
1
2
|
curl -k -L "https:
//pcs
.baidu.com
/rest/2
.0
/pcs/file
?method=delete&access_token=<access_token>\
&path=
/apps/pcsupload/haiyun
.me.
tar
.gz"
|
復制文件:
|
1
2
|
curl -k -L "https:
//c
.pcs.baidu.com
/rest/2
.0
/pcs/file
?method=copy&access_token=<access_token>\
&from=
/apps/pcsupload/haiyun
.me.
tar
.gz&to=
/apps/pcsupload/www
.haiyun.me.
tar
.gz"
|
列出目錄內文件:
|
1
2
|
curl -k -L "https:
//pcs
.baidu.com
/rest/2
.0
/pcs/file
?method=list&access_token=<access_token>\
&path=
/apps/pcsupload/
"
|
更多操作:http://developer.baidu.com/wiki/index.php?title=docs/pcs/rest/file_data_apis_list
