本帖最后由 逸笙 于 2021-2-25 13:57 编辑
很多人有阿里云盘了,(不是讲的teambition)
阿里云盘还没开放分享文件,但可以秒传别人传过的文件,基于此,做了个小工具
代码如下:
- #/bin/bash/
- # Aliyundrive rapid upload
- # by 逸笙
- # 保存sha1的文件,需要以 文件名,sha1,字节大小 的格式一行一个
- # 会临时保存refresh token在下方文件中,注意删除
- tokenTmpfile="AliyundriveRapidUpload.tokenTmp"
- authUrl='https://websv.aliyundrive.com/token/refresh'
- apiUrl='https://api.aliyundrive.com/v2'
- function inputRefreshToken
- {
- refreshToken=`cat "${tokenTmpfile}"`
- if [ g"${refreshToken}" = g"" ]; then
- read -p "输入refresh_token: " refreshToken
- fi
- }
- function getAccessToken
- {
- res=`curl -s "${authUrl}" \
- -H 'Content-Type: application/json; charset=utf-8' \
- -d '{"refresh_token": "'"${refreshToken}"'"}'`
- # echo ${res}
- if [[ "${res}" =~ "access_token" ]]; then
- accessToken=`echo ${res} | awk -F 'access_token' '{print $2}' | awk -F '"' '{print $3}'`
- refreshToken=`echo ${res} | awk -F 'refresh_token' '{print $2}' | awk -F '"' '{print $3}'`
- driveId=`echo ${res} | awk -F 'default_drive_id' '{print $2}' | awk -F '"' '{print $3}'`
- #echo "refreshToken=\'${refreshToken}\'" > ${tokenTmpfile}
- echo "${refreshToken}" > ${tokenTmpfile}
- else
- echo 'Error in getting access token: '
- echo ${res}
- exit
- fi
- }
- # $1 parentId
- # $2 folder name
- function createFolder
- {
- url="${apiUrl}/file/create"
- fid="$1"
- n="$2"
- if [[ "${n}" =~ "/" ]]; then
- folder=${n%/*}
- n=${n##*/}
- fid=`createFolder "${fid}" "${folder}"`
- fi
- res=`curl -s "${url}" \
- -H 'Content-Type: application/json; charset=utf-8' \
- -H 'authorization: Bearer '"${accessToken}" \
- -d '{
- "check_name_mode": "refuse",
- "drive_id": "'"${driveId}"'",
- "parent_file_id": "'"${fid}"'",
- "type": "folder",
- "name": "'"${n}"'"
- }'`
- #echo ${res}
- if [[ "${res}" =~ "file_id" ]]; then
- fileId=`echo "${res}" | awk -F '"file_id' '{print $2}' | awk -F '"' '{print $3}'`
- echo ${fileId}
- else
- echo ${res}
- exit
- fi
- }
- # $1 parentId
- # $2 file name
- # $3 file sha1
- # $4 file size
- function createFile
- {
- url="${apiUrl}/file/create"
- res=`curl -s "${url}" \
- -H 'Content-Type: application/json; charset=utf-8' \
- -H 'authorization: Bearer '"${accessToken}" \
- -d '{
- "check_name_mode": "refuse",
- "content_hash": "'"$3"'",
- "content_hash_name": "sha1",
- "content_type": "",
- "drive_id": "'"${driveId}"'",
- "ignoreError": false,
- "parent_file_id": "'"$1"'",
- "type": "file",
- "size": '"$4"',
- "name": "'"$2"'",
- "part_info_list": [{"part_number": 1}]
- }'`
- echo ${res}
- }
- function main
- {
- inputRefreshToken
- getAccessToken
- if [ g"$1" = g"" ]; then
- read -p "输入文件:" sha1filename
- else
- sha1filename=$1
- fi
- # 预留指定目录
- # 目前根目录
- parentId="root"
- while read line
- do
- #echo $line
- rapid=0
- oname=`echo ${line} | awk -F ',' '{print $1}'`
- sha1=`echo ${line} | awk -F ',' '{print $2}'`
- size=`echo ${line} | awk -F ',' '{print $3}'`
- if [[ "${oname}" =~ "/" ]]; then
- folder=${oname%/*}
- name=${oname##*/}
- folderId=`createFolder "${parentId}" "${folder}"`
- #echo ${folder},${name},${folderId}
- else
- folderId=${parentId}
- name=${oname}
- fi
- result=`createFile "${folderId}" "${name}" "${sha1}" "${size}"`
- # 预留access token过期
- #if []; then
- # getAccessToken
- # result=`createFile "${folderId}" "${name}" "${sha1}" "${size}"`
- #fi
- rapid=`echo ${result} | awk -F 'rapid_upload":' '{print $2}' | awk -F ',' '{print $1}'`
- if [ g"${rapid}" = g"true" ]; then
- echo " 秒传 ${oname}"
- else
- if [[ "${result}" =~ "upload_url" ]]; then
- echo "无人上传 ${oname}"
- else
- exist=`echo ${result} | awk -F 'exist":' '{print $2}' | awk -F ',' '{print $1}' | awk -F '}' '{print $1}'`
- if [ g"${exist}" = g"true" ]; then
- echo " 已有 ${oname}"
- else
- echo "${result}"
- fi
- fi
- fi
- done < ${sha1filename}
- }
- main "$1"
复制代码
用法:bash /root/AliyunRapidUpload.sh /root/filesha1.txt
或:bash /root/AliyunRapidUpload.sh 再输入sha1文件
从目标文件中读取 文件名,sha1,大小,然后尝试秒传(目前上传到根目录)
此目标文件里面的格式为 文件名(英文逗号)文件的sha1(英文逗号)文件的字节大小,一行放一个,如:
WePE_64_V2.0.exe,C2648E0A0003681102F1D790EAC93CA2978CD91E,217464180
aaaaaaaaaaaaa.exe,C2648E0A0003681102F1D790EAC93CA2978CD91E,217464180
bbbbbbbbbbbb.exe,C2648E0A0003681102F1D790EAC93CA2978CD91E,217464180
其实就是csv。
让你输入的token会保存在最开始指定的那个临时文件中,注意安全。
具体sha1文件就要靠大家集中提供了。
另写了个配套生成sha1文件的东东:https://scfonedrive.github.io/Aliyundrive/getSha1.html
|