upload-to-qiniu.sh
4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env bash
# 通用七牛云上传工具 - mlaj 项目
# 用法: ./scripts/upload-to-qiniu.sh <local_file> <remote_path>
set -euo pipefail
# 项目根目录
repo_root="$(cd "$(dirname "$0")/.." && pwd)"
# 七牛配置
QINIU_BUCKET="${QINIU_BUCKET:-ipadbiz}"
QINIU_CONFIG_DIR="$HOME/.qshell"
QINIU_ACCOUNT_CONF="account.json"
# 代理设置(可选)
USE_PROXY=${USE_PROXY:-false}
PROXY_HOST=${PROXY_HOST:-"127.0.0.1:7890"}
log_info() {
echo "[qiniu-upload] $1"
}
# 初始化 qshell 账户
init_account() {
if ! command -v qshell >/dev/null 2>&1; then
echo "错误: 未检测到 qshell,请先安装 https://developer.qiniu.com/kodo/tools/1302/qshell"
exit 1
fi
if [ ! -f "$QINIU_CONFIG_DIR/$QINIU_ACCOUNT_CONF" ]; then
log_info "首次使用,请输入七牛云账号信息:"
read -p "Access Key: " AK
read -p "Secret Key: " SK
qshell account "$AK" "$SK" > "$QINIU_CONFIG_DIR/$QINIU_ACCOUNT_CONF"
log_info "账户信息已保存到 $QINIU_CONFIG_DIR/$QINIU_ACCOUNT_CONF"
fi
}
# 单文件上传
upload_single_file() {
local local_file="$1"
local remote_path="$2"
# 转换为绝对路径
if [[ ! "$local_file" = /* ]]; then
local_file="$repo_root/$local_file"
fi
if [ ! -f "$local_file" ]; then
echo "错误: 文件不存在 $local_file"
exit 1
fi
local file_name=$(basename "$local_file")
log_info "准备上传: $file_name"
log_info "本地路径: $local_file"
log_info "远程路径: $remote_path"
# 设置代理
if [ "$USE_PROXY" = "true" ]; then
export HTTP_PROXY="http://$PROXY_HOST"
export HTTPS_PROXY="http://$PROXY_HOST"
log_info "使用代理: $PROXY_HOST"
fi
# 使用 rput 直接上传文件
if qshell rput "$QINIU_BUCKET" "$remote_path" "$local_file" --overwrite=true; then
log_info "✅ 上传成功: https://cdn.ipadbiz.cn/$remote_path"
else
echo "错误: 上传失败"
exit 1
fi
}
# 批量上传(使用配置文件)
upload_batch() {
local config_file="$1"
if [ ! -f "$config_file" ]; then
echo "错误: 配置文件不存在 $config_file"
exit 1
fi
execute_upload "$config_file"
log_info "✅ 批量上传完成"
}
# 执行上传(统一处理代理)
execute_upload() {
local config_file="$1"
if [ "$USE_PROXY" = "true" ]; then
export HTTP_PROXY="http://$PROXY_HOST"
export HTTPS_PROXY="http://$PROXY_HOST"
log_info "使用代理: $PROXY_HOST"
fi
qshell qupload "$config_file"
}
# 显示帮助信息
show_help() {
cat << EOF
通用七牛云上传工具
用法:
$0 <local_file> <remote_path> 单文件上传
$0 <config_file> 批量上传(指定配置文件)
$0 init 初始化七牛账户
$0 help 显示此帮助信息
参数说明:
local_file 本地文件路径(相对或绝对路径)
remote_path 远程路径,如: mlaj/video/bg.mp4
config_file 配置文件路径
环境变量:
QINIU_BUCKET 七牛空间名(默认: mlaj)
USE_PROXY=true 启用代理
PROXY_HOST=127.0.0.1:7890 代理地址
示例:
# 单文件上传
$0 ./assets/video/bg.mp4 mlaj/video/welcome-bg.mp4
# 批量上传
$0 scripts/qiniu/configs/welcome-video.conf
# 使用代理上传
USE_PROXY=true $0 ./local/file.mp4 mlaj/video/file.mp4
配置文件格式:
{
"src_dir": "./assets/video",
"bucket": "mlaj",
"key_prefix": "mlaj/video/",
"overwrite": true,
"check_exists": true
}
EOF
}
# 主逻辑
main() {
init_account
case "${1:-}" in
init)
log_info "账户初始化完成"
;;
help|--help|-h)
show_help
;;
"")
show_help
exit 1
;;
*)
if [ $# -eq 1 ]; then
# 单个参数,视为配置文件
upload_batch "$1"
elif [ $# -eq 2 ]; then
# 两个参数,单文件上传
upload_single_file "$1" "$2"
else
echo "错误: 参数数量不正确"
show_help
exit 1
fi
;;
esac
}
main "$@"