简体   繁体   English

如何从命令行使用 Seafile 生成的没有身份验证令牌的上传链接

[英]How to use a Seafile generated upload-link w/o authentication token from command line

With Seafile one is able to create a public upload link (eg https://cloud.seafile.com/u/d/98233edf89/ ) to upload files via Browser w/o authentication.使用 Seafile 可以创建一个公共上传链接(例如https://cloud.seafile.com/u/d/98233edf89/ )以通过浏览器上传文件而无需身份验证。

Seafile webapi does not support any upload w/o authentication token. Seafile webapi 不支持任何无身份验证令牌的上传。

How can I use such kind of link from command line with curl or from python script?如何从带有 curl 的命令行或从 python 脚本使用这种链接?

needed 2 hours to find a solution with curl, it needs two steps:需要 2 个小时才能找到 curl 的解决方案,它需要两个步骤:

  1. make a get-request to the public uplink url with the repo-id as query parameter as follows:使用repo-id作为查询参数向公共上行链路 url 发出获取请求,如下所示:

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'

The answer is (json) a id-link to use in next upload-post eg:答案是 (json) 在下一个上传帖子中使用的 id 链接,例如:

{"url": "https://cloud.seafile.com/seafhttp/upload-aj/c2b6d367-22e4-4819-a5fb-6a8f9d783680"}

  1. Use this link to initiate the upload post:使用此链接启动上传帖子:

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/"

The answer is json again, eg答案又是 json,例如

[{"name": "index.html", "id": "0a0742facf24226a2901d258a1c95e369210bcf3", "size": 10521}]

done ;)完成;)

#!/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}"

save as seafile-upload.sh另存为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>

my blog 060-轻量级基于curl的seafile上传脚本我的博客060-轻量级基于curl的seafile上传剧本

official document web-api/v2.1/file-upload.md官方文档web-api/v2.1/file-upload.md

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PyPI 在从 Windows 上传时拒绝令牌身份验证 - PyPI rejects token authentication on upload from Windows 如何制作一个具有并行运行命令行工具功能的python模块(不使用if __name__ ==&#39;__main__&#39;:因此可导入)? - How to make a python module with function to run command line tools in parallel (w/o using if __name__ == '__main__': so it's importable)? 如何从命令行在ubuntu中查找机器生成的PDF的DPI - How to find DPI of a machine generated PDF in ubuntu from command line 如何从命令行上传静态文件到flask应用程序 - How to upload and serve static files to flask app from the command line 如何从命令行“上传和部署”到Elastic Beanstalk? - How can I “upload and deploy” to Elastic Beanstalk from the command line? 如何在不带音译的Django中使用prepopulated_fields? - How to use prepopulated_fields in Django w/o transliteration? 从命令行使用令牌授权 - Authorization From Command Line Using Token Spotify-从命令行访问令牌 - Spotify - access token from command line 如何告诉 Buildout 从 URL (w/o pypi) 安装一个 egg - How to tell Buildout to install a egg from a URL (w/o pypi) 从命令行使用dkimpy - Use dkimpy from command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM