Ubuntu WSL 中的 运行 bash 脚本时不一致的 S3 参数验证错误

Inconsisent S3 parameter validation error when running bash script in Ubuntu WSL

我正在按照此处的说明试验 AWS Lambda:https://github.com/awsdocs/aws-lambda-developer-guide/tree/main/sample-apps/java-basic

部分设置说明需要运行此脚本 (2-deploy.sh):

#!/bin/bash
set -eo pipefail
ARTIFACT_BUCKET=$(cat bucket-name.txt)
TEMPLATE=template.yml
if [  ]
then
  if [  = mvn ]
  then
    TEMPLATE=template-mvn.yml
    mvn package
  fi
else
  gradle build -i
fi

echo $TEMPLATE #debug
echo $ARTIFACT_BUCKET #debug
aws cloudformation package --template-file $TEMPLATE --s3-bucket $ARTIFACT_BUCKET --output-template-file out.yml
aws cloudformation deploy --template-file out.yml --stack-name java-basic --capabilities CAPABILITY_NAMED_IAM

当我 运行 WSL 中的脚本时,出现参数验证错误:

$ ./2-deploy.sh mvn
  ... # Maven output

template-mvn.yml              
lambda-artifacts-<alphanumeric string>                                    
Unable to upload artifact target/java-basic-1.0-SNAPSHOT.jar referenced by CodeUri parameter of function resource.
Parameter validation failed:                                                     "
": Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$" or be an ARN matching the regex "^arn:(aws).*:(s3|s3-object-lambda):[a-z\-0-9]+:[0-9]{12}:accesspoint[/:][a-zA-Z0-9\-]{1,63}$|^arn:(aws).*:s3-outposts:[a-z\-0-9]+:[0-9]{12}:outpost[/:][a-zA-Z0-9\-]{1,63}[/:]accesspoint[/:][a-zA-Z0-9\-]{1,63}$"

但是,当我手动 运行 带有调试输出的文件的最后两行时,它可以正常工作:

adam@DESKTOP-ON98ECK:/mnt/c/Users/Ben/IdeaProjects/LakeStockingAlerter/sample-apps/java-basic$ aws cloudformation package --template-file template-mvn.yml --s3-bucket lambda-artifacts-<alphanumeric string>--output-template-file out.yml         
Uploading to ... 260754 / 260754.0  (100.00%)
Successfully packaged artifacts and wrote output template to file out.yml.
Execute the following command to deploy the packaged template
aws cloudformation deploy --template-file /mnt/c/Users/Ben/IdeaProjects/LakeStockingAlerter/sample-apps/java-basic/out.yml --stack-name <YOUR STACK NAME>
adam@DESKTOP-ON98ECK:/mnt/c/Users/Ben/IdeaProjects/LakeStockingAlerter/sample-apps/java-basic$ aws cloudformation deploy --template-file out.yml --stack-name java-basic --capabilities CAPABILITY_NAMED_IAM

Waiting for changeset to be created...
Waiting for stack create/update to complete
Successfully created/updated stack - java-basic  

为什么当 运行 作为 bash 脚本时会失败? None 存储在变量中的字符对于 bash 是特殊的,所以我没有发现变量替换有问题。该脚本还使用 Unix 风格的行结尾。

根据评论。

问题是 bucket-name.txt 文件包含一个 额外的行 。随后 $ARTIFACT_BUCKET 包含换行符,导致错误。