

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# AppSpec “文件” 部分（仅限 EC2/本地部署）
<a name="reference-appspec-file-structure-files"></a>

提供 CodeDeploy 有关在部署的 In **stall** 事件期间应在实例上安装应用程序修订版中的哪些文件的信息。仅当您要在部署期间将修订中的文件复制到实例上的位置时，才需要此部分。

此部分具有以下结构：

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

可以设置多个 `destination` 和 `source` 对。

`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：当前正在部署的应用程序修订的文件版本将替换实例上已有的版本。
+ RETAIN：保留实例上已有的文件版本，并将其用作新部署的一部分。

使用 `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` 目录中。

## “files”部分示例
<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` 与包含三个文件的名为 `my-folder` 的文件夹一起出现在 `source` 的根的捆绑包中：
+ `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
```