最近把博客重新捡起来了,但是发现在Typora写完博客之后,上传到typecho时还要把文件单独传一遍,于是写了一个小脚本,可以把本地的文件同步到腾讯云的COS里。
#!/bin/bash
# 百度翻译API,需要自己申请appid和apikey
appid=
apikey=
salt=1435660288
# coscmd的路径
cmd="/usr/local/bin/coscmd"
# 存放图片的目录
cos_base_path="blog/article_img/"
# cos的加速域名 /结尾
base_url="https://example.com/"
q=${1// /}
sign=$(echo -n "${appid}${q}${salt}${apikey}" | md5)
baidu_slug="https://api.fanyi.baidu.com/api/trans/vip/translate?q=${q}&from=zh&to=en&appid=${appid}&salt=${salt}&sign=${sign}"
result=$(curl "${baidu_slug}" | sed -n 's/.*"dst":"\([^"]*\)".*/\1/p')
dir=$(echo "${result}" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
declare -a filenames
for i in "$@"; do
if [ "${1}"x != "${i}"x ]; then
$cmd upload "$i" "${cos_base_path}${dir}/"
if [ $? -eq 0 ]; then
if [[ ${2} =~ /([^/]+)\.([^/]+)$ ]]; then
filenames+=("${base_url}${cos_base_path}${dir}/${BASH_REMATCH[1]}.${BASH_REMATCH[2]}")
fi
fi
fi
done
echo "Upload Success:"
for name in "${filenames[@]}"; do
echo $name
done
将代码放入sh里,然后在typora中配置自定义上传命令,例如:
~/typora-upload.sh "${filename}"
这里要注意一下,${filename}一定要带引号,防止文件名有空格导致脚本g掉
小米8 Explore Edition (透明色)
@罐子,我是直接用的官方的coscmd配合shell写的,你要是用golang的话应该要自己用sdk实现
小米8 Explore Edition (透明色)