

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

# AppSpec 'files' 區段 （僅限 EC2/內部部署部署）
<a name="reference-appspec-file-structure-files"></a>

向 CodeDeploy 提供部署安裝****事件期間應該在執行個體上安裝的應用程式修訂版中哪些檔案的相關資訊。唯有您在部署期間將修訂中的檔案複製至執行個體上的位置時，會需要本區段。

本區段的結構如下：

```
files:
  - source: source-file-location-1
    destination: destination-file-location-1
file_exists_behavior: DISALLOW|OVERWRITE|RETAIN
```

您可以設定多個 `source` 和 `destination` 配對。

`source` 說明識別要從修訂複製至執行個體的檔案或目錄：
+ 如果 `source` 是指檔案，只會將指定的檔案複製至執行個體。
+ 如果 `source` 是指目錄，會將該目錄中的所有檔案都複製至執行個體。
+ 如果 `source`是 Amazon Linux、RHEL 和 Ubuntu Server 執行個體的單斜線 ("/"，或 Windows Server 執行個體的 "\$1")，則修訂中的所有檔案都會複製到執行個體。

在 中使用的路徑`source`與 `appspec.yml` 檔案相對，應該位於修訂的根目錄。如需修訂版檔案結構的詳細資訊，請參閱 [規劃 CodeDeploy 的修訂](application-revisions-plan.md)。

`destination` 說明識別執行個體上應該複製檔案的位置。這必須是完整路徑，例如 `/root/destination/directory`（在 Linux、RHEL 和 Ubuntu 上） 或 `c:\destination\folder`（在 Windows 上）。

`source` 和 `destination` 各以字串指定。

`file_exists_behavior` 指示為選用，並指定 CodeDeploy 如何處理已存在於部署目標位置但不屬於先前成功部署的檔案。此設定可以採用下列任何值：
+ DISALLOW：部署失敗。如果未指定任何選項，這也是預設行為。
+ OVERWRITE：目前部署之應用程式修訂版的檔案版本會取代執行個體上已存在的版本。
+ 保留：執行個體上已存在的 檔案版本會保留並做為新部署的一部分使用。

使用 `file_exists_behavior`設定時，請了解此設定：
+ 只能指定一次，並套用至 下列出的所有檔案和目錄`files:`。
+ 優先於 `--file-exists-behavior` AWS CLI 選項和 `fileExistsBehavior` API 選項 （兩者皆為選用）。

以下是 Amazon Linux、Ubuntu Server 或 RHEL 執行個體的範例`files`區段。

```
files:
  - source: Config/config.txt
    destination: /webapps/Config
  - source: source
    destination: /webapps/myApp
```

在本範例中，會在 **Install** 事件期間執行下列兩個操作：

1. 將修訂中的 `Config/config.txt` 檔案複製至執行個體上的 `/webapps/Config/config.txt` 路徑。

1. 將修訂 `source` 目錄中的所有檔案都遞迴複製至執行個體上的 `/webapps/myApp` 目錄。

## 「檔案」區段範例
<a name="reference-appspec-file-structure-files-examples"></a>

下列範例顯示如何指定 `files` 區段。雖然這些範例描述了 Windows Server 檔案和目錄 （資料夾） 結構，但它們可以輕鬆適應 Amazon Linux、Ubuntu Server 和 RHEL 執行個體。

**注意**  
只有 EC2/現場部署會使用 `files`區段。它不適用於 AWS Lambda 部署。

在下列範例中，假設這些檔案出現在 `source` 根目錄的套件中：
+ `appspec.yml`
+ `my-file.txt`
+ `my-file-2.txt`
+ `my-file-3.txt`

```
# 1) Copy only my-file.txt to the destination folder c:\temp.
#
files:
  - source: .\my-file.txt
    destination: c:\temp
#
# Result:
#   c:\temp\my-file.txt
#
# ---------------------
#
# 2) Copy only my-file-2.txt and my-file-3.txt to the destination folder c:\temp.
#
files:
  - source: my-file-2.txt
    destination: c:\temp
  - source: my-file-3.txt
    destination: c:\temp
#
# Result:
#   c:\temp\my-file-2.txt
#   c:\temp\my-file-3.txt
#
# ---------------------
#
# 3) Copy my-file.txt, my-file-2.txt, and my-file-3.txt (along with the appspec.yml file) to the destination folder c:\temp.
#
files:
  - source: \
    destination: c:\temp
#
# Result:
#   c:\temp\appspec.yml
#   c:\temp\my-file.txt
#   c:\temp\my-file-2.txt
#   c:\temp\my-file-3.txt
```

在下列範例中，假設 `appspec.yml` 出現在 `source` 根目錄以及名為 `my-folder` 資料夾 (包含三個檔案) 的套件中：
+ `appspec.yml`
+ `my-folder\my-file.txt`
+ `my-folder\my-file-2.txt`
+ `my-folder\my-file-3.txt`

```
# 4) Copy the 3 files in my-folder (but do not copy my-folder itself) to the destination folder c:\temp. 
#
files:
  - source: .\my-folder
    destination: c:\temp
#
# Result:
#   c:\temp\my-file.txt
#   c:\temp\my-file-2.txt
#   c:\temp\my-file-3.txt
#
# ---------------------
#
# 5) Copy my-folder and its 3 files to my-folder within the destination folder c:\temp.
#
files:
  - source: .\my-folder
    destination: c:\temp\my-folder
#
# Result:
#   c:\temp\my-folder\my-file.txt
#   c:\temp\my-folder\my-file-2.txt
#   c:\temp\my-folder\my-file-3.txt
#
# ---------------------
#
# 6) Copy the 3 files in my-folder to other-folder within the destination folder c:\temp.
#
files:
  - source: .\my-folder
    destination: c:\temp\other-folder
#
# Result:
#   c:\temp\other-folder\my-file.txt
#   c:\temp\other-folder\my-file-2.txt
#   c:\temp\other-folder\my-file-3.txt	
#
# ---------------------
#
# 7) Copy only my-file-2.txt and my-file-3.txt to my-folder within the destination folder c:\temp.
#
files:
  - source: .\my-folder\my-file-2.txt
    destination: c:\temp\my-folder
  - source: .\my-folder\my-file-3.txt
    destination: c:\temp\my-folder
#
# Result:
#   c:\temp\my-folder\my-file-2.txt
#   c:\temp\my-folder\my-file-3.txt
#
# ---------------------
#
# 8) Copy only my-file-2.txt and my-file-3.txt to other-folder within the destination folder c:\temp.
#
files:
  - source: .\my-folder\my-file-2.txt
    destination: c:\temp\other-folder
  - source: .\my-folder\my-file-3.txt
    destination: c:\temp\other-folder
#
# Result:
#   c:\temp\other-folder\my-file-2.txt
#   c:\temp\other-folder\my-file-3.txt
#
# ---------------------
#
# 9) Copy my-folder and its 3 files (along with the appspec.yml file) to the destination folder c:\temp. If any of the files already exist on the instance, overwrite them.
#
files:
  - source: \
    destination: c:\temp
file_exists_behavior: OVERWRITE
#
# Result:
#   c:\temp\appspec.yml
#   c:\temp\my-folder\my-file.txt
#   c:\temp\my-folder\my-file-2.txt
#   c:\temp\my-folder\my-file-3.txt
```