Skip to content

Commit a03d4f3

Browse files
committed
```
feat(aliyun-oss-put): 支持脚本目录配置文件和Content-Length头 - 新增脚本所在目录下的 aliyun-oss-put.env / aliyun-oss-put.env.local 配置文件支持 - 配置优先级调整为:shell环境变量 > 当前工作目录配置 > 脚本目录配置 - 添加 *.env.local 到 .gitignore 以避免提交本地配置文件 - 实现 Content-Length 头的添加和签名,提高上传请求的完整性 ```
1 parent 7fcb47d commit a03d4f3

2 files changed

Lines changed: 33 additions & 7 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ bin/
4141
.agent/rules
4242

4343
.env.local
44+
*.env.local

scripts/bash/aliyun-oss-put.sh

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
# - CLI 参数:提供本地文件路径与目标对象信息。
88
# - 环境变量:提供凭证与可复用的默认配置。
99
# - 当前工作目录下的 aliyun-oss-put.env / aliyun-oss-put.env.local。
10+
# - 脚本所在目录下的 aliyun-oss-put.env / aliyun-oss-put.env.local。
1011
#
1112
# 配置优先级:
1213
# 1. 当前 shell 中已存在的环境变量
13-
# 2. aliyun-oss-put.env.local
14-
# 3. aliyun-oss-put.env
14+
# 2. 当前工作目录下的 aliyun-oss-put.env.local
15+
# 3. 当前工作目录下的 aliyun-oss-put.env
16+
# 4. 脚本所在目录下的 aliyun-oss-put.env.local
17+
# 5. 脚本所在目录下的 aliyun-oss-put.env
1518
#
1619
# 退出码:
1720
# 0 成功
@@ -95,7 +98,8 @@ Environment variables:
9598
ALIYUN_OSS_CONTENT_TYPE Optional default for --content-type.
9699
97100
Dotenv loading:
98-
The script reads aliyun-oss-put.env first and then aliyun-oss-put.env.local from the current working directory.
101+
The script reads aliyun-oss-put.env first and then aliyun-oss-put.env.local
102+
from the script directory, then repeats the same order for the current working directory.
99103
Existing shell environment variables always win over file-based values.
100104
101105
Examples:
@@ -230,11 +234,20 @@ load_env_file_if_present() {
230234
done < "$env_file_path"
231235
}
232236

233-
# 读取脚本专属 dotenv 文件,避免与同目录其他脚本共用通用 .env 时发生冲突。
234-
# 先读共享配置,再读 .local,本地文件因此拥有更高优先级。
237+
# 读取脚本专属 dotenv 文件,支持“脚本目录默认配置 + 当前工作目录覆盖”模式。
238+
# 优先级:
239+
# 1. 当前 shell 环境变量
240+
# 2. 当前工作目录 aliyun-oss-put.env.local
241+
# 3. 当前工作目录 aliyun-oss-put.env
242+
# 4. 脚本目录 aliyun-oss-put.env.local
243+
# 5. 脚本目录 aliyun-oss-put.env
235244
load_dotenv_files() {
236245
local work_dir="$PWD"
246+
local script_dir
247+
script_dir="$(cd "$(dirname "$0")" && pwd)"
237248
INITIAL_ENV_VARS="$(env | LC_ALL=C cut -d= -f1)"
249+
load_env_file_if_present "$script_dir/aliyun-oss-put.env"
250+
load_env_file_if_present "$script_dir/aliyun-oss-put.env.local"
238251
load_env_file_if_present "$work_dir/aliyun-oss-put.env"
239252
load_env_file_if_present "$work_dir/aliyun-oss-put.env.local"
240253
}
@@ -462,6 +475,7 @@ build_canonical_headers() {
462475

463476
append_canonical_header "content-md5" "$FILE_MD5_BASE64"
464477
append_canonical_header "content-type" "$CONTENT_TYPE"
478+
append_canonical_header "content-length" "$FILE_SIZE"
465479

466480
if [ "$OVERWRITE_MODE" != "true" ]; then
467481
append_canonical_header "x-oss-forbid-overwrite" "true"
@@ -477,15 +491,24 @@ build_canonical_headers() {
477491
printf '%s' "$canonical_headers"
478492
}
479493

494+
# 构造 Authorization 头里的 AdditionalHeaders 字段。
495+
# 当前上传实现显式发送了 Content-Length,因此这里固定将其纳入签名。
496+
build_additional_headers_value() {
497+
printf '%s' "content-length"
498+
}
499+
480500
# 按 OSS V4 规则构造 canonical request。
481501
build_canonical_request() {
482502
local canonical_headers
503+
local additional_headers_value
483504

484505
canonical_headers="$(build_canonical_headers)"
506+
additional_headers_value="$(build_additional_headers_value)"
485507

486-
printf 'PUT\n%s\n\n%s\n\n%s' \
508+
printf 'PUT\n%s\n\n%s\n\n%s\n%s' \
487509
"$CANONICAL_URI" \
488510
"$canonical_headers" \
511+
"$additional_headers_value" \
489512
"$CONTENT_SHA256"
490513
}
491514

@@ -599,6 +622,7 @@ perform_upload() {
599622
local string_to_sign
600623
local signature
601624
local authorization_header
625+
local additional_headers_value
602626
local -a curl_args
603627
local http_status
604628
local curl_exit_code
@@ -611,8 +635,9 @@ perform_upload() {
611635
canonical_request="$(build_canonical_request)"
612636
string_to_sign="$(build_string_to_sign "$canonical_request")"
613637
signature="$(build_signature "$string_to_sign")"
638+
additional_headers_value="$(build_additional_headers_value)"
614639

615-
authorization_header="OSS4-HMAC-SHA256 Credential=${ALIYUN_ACCESS_KEY_ID}/${SHORT_DATE}/${REGION_ID}/oss/aliyun_v4_request,Signature=${signature}"
640+
authorization_header="OSS4-HMAC-SHA256 Credential=${ALIYUN_ACCESS_KEY_ID}/${SHORT_DATE}/${REGION_ID}/oss/aliyun_v4_request,AdditionalHeaders=${additional_headers_value},Signature=${signature}"
616641

617642
print_debug_signing "$canonical_request" "$string_to_sign" "$signature"
618643
log_verbose "Request URL: $REQUEST_URL"

0 commit comments

Comments
 (0)