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
97100Dotenv 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
101105Examples:
@@ -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
235244load_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。
481501build_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