使用 Seafile 可以创建一个公共上传链接(例如https://cloud.seafile.com/u/d/98233edf89/
)以通过浏览器上传文件而无需身份验证。
Seafile webapi 不支持任何不带身份验证令牌的上传。
如何从命令行使用 curl 或 python 脚本中的这种链接?
使用 Seafile 可以创建一个公共上传链接(例如https://cloud.seafile.com/u/d/98233edf89/
)以通过浏览器上传文件而无需身份验证。
Seafile webapi 不支持任何不带身份验证令牌的上传。
如何从命令行使用 curl 或 python 脚本中的这种链接?
需要 2 小时才能找到 curl 的解决方案,它需要两个步骤:
repo-id
如下所示:curl 'https://cloud.seafile.com/ajax/u/d/98233edf89/upload/?r=f3e30b25-aad7-4e92-b6fd-4665760dd6f5' -H 'Accept: application/json' -H 'X-Requested-With: XMLHttpRequest'
答案是 (json) 在下一个上传帖子中使用的 id 链接,例如:
{"url": "https://cloud.seafile.com/seafhttp/upload-aj/c2b6d367-22e4-4819-a5fb-6a8f9d783680"}
curl 'https://cloud.seafile.com/seafhttp/upload-aj/c2b6d367-22e4-4819-a5fb-6a8f9d783680' -F file=@./tmp/index.html -F filename=index.html -F parent_dir="/my-repo-dir/"
答案又是 json,例如
[{"name": "index.html", "id": "0a0742facf24226a2901d258a1c95e369210bcf3", "size": 10521}]
完毕 ;)
#!/usr/bin/env bash
# this script depend on jq,check it first
RED='\033[0;31m'
NC='\033[0m' # No Color
if ! command -v jq &> /dev/null
then
echo -e "${RED}jq could not be found${NC}, installed and restart plz!\n"
exit
fi
usage () { echo "Usage : $0 -u <username> -p <password> -h <seafile server host> -f <upload file path> -d <parent dir default value is /> -r <repo id> -t <print debug info switch off/on,default off>"; }
# parse args
while getopts "u:p:h:f:d:r:t:" opts; do
case ${opts} in
u) USER=${OPTARG} ;;
p) PASSWORD=${OPTARG} ;;
h) HOST=${OPTARG} ;;
f) FILE=${OPTARG} ;;
d) PARENT_DIR=${OPTARG} ;;
r) REPO=${OPTARG} ;;
t) DEBUG=${OPTARG} ;;
*) usage; exit;;
esac
done
# those args must be not null
if [ ! "$USER" ] || [ ! "$PASSWORD" ] || [ ! "$HOST" ] || [ ! "$FILE" ] || [ ! "$REPO" ]
then
usage
exit 1
fi
# optional args,set default value
[ -z "$DEBUG" ] && DEBUG=off
[ -z "$PARENT_DIR" ] && PARENT_DIR=/
# print vars key and value when DEBUG eq on
[[ "on" == "$DEBUG" ]] && echo -e "USER:${USER} PASSWORD:${PASSWORD} HOST:${HOST} FILE:${FILE} PARENT_DIR:${PARENT_DIR} REPO:${REPO} DEBUG:${DEBUG}"
# login and get token
TOKEN=$(curl -s --location --request POST "${HOST}/api2/auth-token/" --header 'Content-Type: application/x-www-form-urlencoded' --data-urlencode "username=${USER}" --data-urlencode "password=${PASSWORD}" | jq -r ".token")
[ -z "$TOKEN" ] && echo -e "${RED}login seafile faild${NC}, call your administrator plz!\n" && exit 1
# gen upload link
UPLOAD_LINK=$(curl -s --header "Authorization: Token ${TOKEN}" "${HOST}/api2/repos/${REPO}/upload-link/?p=${PARENT_DIR}" | jq -r ".")
[ -z "$UPLOAD_LINK" ] && echo -e "${RED}get upload link faild${NC}, call your administrator plz!\n" && exit 1
# upload file
UPLOAD_RESULT=$(curl -s --header "Authorization: Token ${TOKEN}" -F file="@${FILE}" -F filename=$(basename ${FILE}) -F parent_dir="${PARENT_DIR}" -F replace=1 "${UPLOAD_LINK}?ret-json=1")
[ -z "$UPLOAD_RESULT" ] && echo -e "${RED}faild to upload ${FILE}${NC}, call your administrator plz!\n" && exit 1
# print upload result
[[ "on" == "$DEBUG" ]] && echo -e "TOKEN:${TOKEN} UPLOAD_LINK:${UPLOAD_LINK} UPLOAD_RESULT:${UPLOAD_RESULT}"
另存为seafile-upload.sh
# ubuntu
apt install -y jq
# centos
# yum install -y jq
chmod +x ./seafile-upload.sh
./seafile-upload.sh -u <username> -p <password> -h <seafile server host> -f <upload file path> -d <parent dir default value is /,must be start with /> -r <repo id> -t <print debug info switch off/on,default off>