從 中的檔案載入參數 AWS CLI - AWS Command Line Interface

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

從 中的檔案載入參數 AWS CLI

有些參數預期檔案名稱會做為引數, 會從中 AWS CLI 載入資料。其他參數可讓您以在命令列中輸入文字或從檔案讀取的形式指定參數值。無論檔案是必要或選用,您都必須正確編碼檔案,讓 AWS CLI 可以了解它。檔案的編碼必須符合讀取系統的預設地區設定。您可以透過使用 Python locale.getpreferredencoding() 方法判斷。

此方法用於載入單一參數的檔案。如需使用單一檔案載入多個參數的資訊,請參閱 AWS CLI 中的骨架和輸入檔案 AWS CLI

注意

根據預設,Windows PowerShell 會將文字輸出為 UTF-16,這會與 JSON 檔案和許多 Linux 系統所使用的 UTF-8 編碼衝突。我們建議您-Encoding ascii搭配 PowerShell Out-File命令使用 ,以確保 AWS 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 amzn-s3-demo-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 受管政策連接至使用者

將檔案載入為速記語法值

使用值較大或複雜的速記語法時,載入 檔案做為值通常比較容易。若要將檔案載入為速記語法值,格式會稍微變更。您將key=value使用 @= 運算子而非 =運算子。@= 表示 AWS CLI 值應該讀取為檔案路徑,而不是字串。下列範例顯示一個鍵值對,載入檔案做為其值。

Linux or macOS
--option key@=file://template.txt
Windows
--option "key1@=file://template.txt"

下列範例示範為 aws rolesanywhere create-trust-anchor命令載入憑證檔案。

$ aws rolesanywhere create-trust-anchor --name TrustAnchor \ --source sourceData={x509CertificateData@=file://root-ca.crt},sourceType="CERTIFICATE_BUNDLE" \ --enabled

如需速記語法的詳細資訊,請參閱 在 中使用速記語法 AWS CLI