

**本文件 AWS CLI 僅適用於 第 1 版。**

我們已宣布即將end-of-support。 AWS CLI 我們建議您遷移至 第 2 AWS CLI 版。如需日期、其他詳細資訊和如何遷移的資訊，請參閱 [公告](https://aws.amazon.com/blogs/developer/cli-v1-maintenance-mode-announcement/)。如需 第 2 版的相關文件 AWS CLI，請參閱 第 [2 版使用者指南](https://docs.aws.amazon.com/cli/latest/userguide/)。

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

# 從 中的檔案載入參數 AWS CLI
<a name="cli-usage-parameters-file"></a>

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

此方法用於載入單一參數的檔案。如需使用單一檔案載入多個參數的資訊，請參閱 [AWS CLI 中的骨架和輸入檔案 AWS CLI](cli-usage-skeleton.md)。

**注意**  
根據預設，Windows PowerShell 會將文字輸出為 UTF-16，這會與 JSON 檔案和許多 Linux 系統所使用的 UTF-8 編碼衝突。我們建議您`-Encoding ascii`搭配 PowerShell `Out-File`命令使用 ，以確保 AWS CLI 可以讀取產生的檔案。

**Topics**
+ [如何從檔案載入參數](#cli-usage-parameters-file-how)
+ [二進位檔案](#cli-usage-parameters-file-binary)
+ [遠端檔案](#cli-usage-parameters-file-remote)
+ [將檔案載入為速記語法值](#cli-usage-parameters-file-shorthand)

## 如何從檔案載入參數
<a name="cli-usage-parameters-file-how"></a>

從檔案載入參數值有時很方便，而不要嘗試完全以命令列參數值輸入，例如當參數是複雜的 JSON 字串時。若要指定包含數值的檔案，請以下列格式指定檔案 URL。

```
file://complete/path/to/file
```
+ 前兩個斜線「/」字元是規格的一部分。如果所需的路徑以「/」開頭，則結果為三個斜線字元：`file:///folder/file`。
+ 此 URL 提供檔案的路徑，該檔案包含實際的參數內容。
+ 使用帶有空格或特殊字符的檔案時，請遵循您終端適用的[引用和逸出規則](cli-usage-parameters-quoting-strings.md)。

**注意**  
對於已經預期 URL 的參數，例如識別 CloudFormation 範本 URL 的參數，此行為會自動停用。您也可以在 AWS CLI 組態檔案中停用 [`cli_follow_urlparam`](cli-configure-files.md#cli-config-cli_follow_urlparam)設定來停用此行為。

在下列範例中，檔案路徑是以相對於目前的工作目錄來解譯。

------
#### [ 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 中，通常在 `%USERPROFILE%` 下有一個使用者目錄。

您仍然必須逸出內嵌為其他 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\"}"
}
```

## 二進位檔案
<a name="cli-usage-parameters-file-binary"></a>

對於以二進位資料作為參數的命令，使用 `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\""
}
```

## 遠端檔案
<a name="cli-usage-parameters-file-remote"></a>

 AWS CLI 也支援使用 `http://`或 `https://` URL，從託管在網際網路上的檔案載入參數。以下範例參考存放在 Amazon S3 儲存貯體中的檔案。這使您可以存取任何電腦地參數檔案，但需要將容器放在可公開存取的位置。

```
$ aws ec2 run-instances \
    --image-id ami-12345678 \
    --block-device-mappings http://amzn-s3-demo-bucket.s3.amazonaws.com/filename.json
```

前面的範例假設檔案 `filename.json` 包含以下 JSON 資料。

```
[
  {
    "DeviceName": "/dev/sdb",
    "Ebs": {
      "VolumeSize": 20,
      "DeleteOnTermination": false,
      "VolumeType": "standard"
    }
  }
]
```

關於參考包含 JSON 格式參數的檔案的另一個範例，請參閱 [將 IAM 受管政策連接至使用者](cli-services-iam.md#cli-services-iam-policy)。

## 將檔案載入為速記語法值
<a name="cli-usage-parameters-file-shorthand"></a>

使用值較大或複雜的速記語法時，通常更容易將檔案載入為值。若要將檔案載入為速記語法值，格式會稍微變更。不是 `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](cli-usage-shorthand.md)。