从文件加载 Amazon CLI 参数 - Amazon Command Line Interface
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

从文件加载 Amazon CLI 参数

有些参数需要文件名作为变量,Amazon CLI 将从这些变量中加载数据。其他参数允许您将参数值指定为在命令行上键入的文本或从文件中读取的文本。无论文件是必需的还是可选的,您都必须对文件进行正确编码,以便 Amazon CLI 能够理解它。该文件的编码必须与读取系统的默认区域设置相匹配。您可以通过使用 Python locale.getpreferredencoding() 方法来确定这一点。

注意

默认情况下,Windows PowerShell 将文本输出为 UTF-16,这与 JSON 文件和许多 Linux 系统使用的 UTF-8 编码冲突。建议您将 -Encoding ascii 与 PowerShell Out-File 命令一起使用,以确保 Amazon CLI 可以读取生成的文件。

如何从文件加载参数

有时,从文件加载参数值(而不是尝试将其全部键入为命令行参数值)很方便,例如当参数为复杂的 JSON 字符串时。要指定包含该值的文件,请按以下格式指定文件 URL。

file://complete/path/to/file
  • 前两个斜杠“/”字符是规范的一部分。如果所需的路径以“/”开头,结果为三个斜杠字符:file:///folder/file

  • URL 提供包含实际参数内容的文件的路径。

  • 使用带空格或特殊字符的文件时,请遵循终端的引用和转义规则

以下示例中的文件路径被解读为相对于当前工作目录。

Linux or macOS
// Read from a file in the current directory $ aws ec2 describe-instances --filters file://filter.json // Read from a file in /tmp $ aws ec2 describe-instances --filters file:///tmp/filter.json // Read from a file with a filename with whitespaces $ aws ec2 describe-instances --filters 'file://filter content.json'
Windows command prompt
// Read from a file in C:\temp C:\> aws ec2 describe-instances --filters file://C:\temp\filter.json // Read from a file with a filename with whitespaces C:\> aws ec2 describe-instances --filters "file://C:\temp\filter content.json"

file:// 前缀选项支持包含“~/”、“./”和“../”的 Unix 式扩展。在 Windows 上,“~/”表达式将展开到您的用户目录(存储在 %USERPROFILE% 环境变量中)。例如,在 Windows 10 上,您通常在 C:\Users\UserName\ 下有一个用户目录。

仍必须对作为另一个 JSON 文档的值嵌入的 JSON 文档进行转义。

$ aws sqs create-queue --queue-name my-queue --attributes file://attributes.json

attributes.json

{ "RedrivePolicy": "{\"deadLetterTargetArn\":\"arn:aws:sqs:us-west-2:0123456789012:deadletter\", \"maxReceiveCount\":\"5\"}" }

二进制文件

对于将二进制数据用作参数的命令,请使用 fileb:// 前缀指定该数据为二进制内容。接受二进制数据的命令包括:

  • aws ec2 run-instances: --user-data 参数。

  • aws s3api put-object: --sse-customer-key 参数。

  • aws kms decrypt: --ciphertext-blob 参数。

以下示例使用 Linux 命令行工具生成一个二进制 256 位 AES 密钥,然后将该密钥提供给 Amazon S3 以对上载的文件服务器端进行加密。

$ dd if=/dev/urandom bs=1 count=32 > sse.key 32+0 records in 32+0 records out 32 bytes (32 B) copied, 0.000164441 s, 195 kB/s $ aws s3api put-object \ --bucket my-bucket \ --key test.txt \ --body test.txt \ --sse-customer-key fileb://sse.key \ --sse-customer-algorithm AES256 { "SSECustomerKeyMD5": "iVg8oWa8sy714+FjtesrJg==", "SSECustomerAlgorithm": "AES256", "ETag": "\"a6118e84b76cf98bf04bbe14b6045c6c\"" }

有关引用包含 JSON 格式参数的文件的更多示例,请参阅将 IAM 托管式策略附加到用户