

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

# 步骤 3：将组件发布到 Amazon IoT Greengrass 服务
<a name="publish-component-defer-updates"></a>

在本节中，你将 Hello World 组件发布到 Amazon IoT Greengrass 云服务。在 Amazon IoT Greengrass 云服务中提供组件后，您可以将其部署到核心设备。您可以使用 GDK CLI 将组件从开发计算机发布到 Amazon IoT Greengrass 云服务。GDK CLI 会上传组件的配方和构件。

**将 Hello World 组件发布到该 Amazon IoT Greengrass 服务**

1. 运行以下命令，使用 GDK CLI 构建组件。[组件构建命令](greengrass-development-kit-cli-component.md#greengrass-development-kit-cli-component-build)基于 GDK CLI 配置文件创建配方和构件。在此过程中，GDK CLI 会创建一个包含组件源代码的 ZIP 文件。

   ```
   gdk component build
   ```

   您应该会看到类似于以下示例的消息。

   ```
   [2022-04-28 11:20:16] INFO - Getting project configuration from gdk-config.json
   [2022-04-28 11:20:16] INFO - Found component recipe file 'recipe.yaml' in the  project directory.
   [2022-04-28 11:20:16] INFO - Building the component 'com.example.BatteryAwareHelloWorld' with the given project configuration.
   [2022-04-28 11:20:16] INFO - Using 'zip' build system to build the component.
   [2022-04-28 11:20:16] WARNING - This component is identified as using 'zip' build system. If this is incorrect, please exit and specify custom build command in the 'gdk-config.json'.
   [2022-04-28 11:20:16] INFO - Zipping source code files of the component.
   [2022-04-28 11:20:16] INFO - Copying over the build artifacts to the greengrass component artifacts build folder.
   [2022-04-28 11:20:16] INFO - Updating artifact URIs in the recipe.
   [2022-04-28 11:20:16] INFO - Creating component recipe in 'C:\Users\finthomp\greengrassv2\com.example.BatteryAwareHelloWorld\greengrass-build\recipes'.
   ```

1. 运行以下命令将组件发布到 Amazon IoT Greengrass 云服务。[组件发布命令](greengrass-development-kit-cli-component.md#greengrass-development-kit-cli-component-publish)将组件的 ZIP 文件构件上传到 S3 存储桶。然后，它会在组件配方中更新 ZIP 文件的 S3 URI，并将配方上传到 Amazon IoT Greengrass 服务。在此过程中，GDK CLI 会检查 Amazon IoT Greengrass 云服务中已有哪个版本的 Hello World 组件，因此它可以选择该版本之后的下一个补丁版本。如果该组件尚不存在，GDK CLI 将使用版本 `1.0.0`。

   ```
   gdk component publish
   ```

   您应该会看到类似于以下示例的消息。输出会显示 GDK CLI 创建的组件版本。

   ```
   [2022-04-28 11:20:29] INFO - Getting project configuration from gdk-config.json
   [2022-04-28 11:20:29] INFO - Found component recipe file 'recipe.yaml' in the  project directory.
   [2022-04-28 11:20:29] INFO - Found credentials in shared credentials file: ~/.aws/credentials
   [2022-04-28 11:20:30] INFO - No private version of the component 'com.example.BatteryAwareHelloWorld' exist in the account. Using '1.0.0' as the next version to create.
   [2022-04-28 11:20:30] INFO - Publishing the component 'com.example.BatteryAwareHelloWorld' with the given project configuration.
   [2022-04-28 11:20:30] INFO - Uploading the component built artifacts to s3 bucket.
   [2022-04-28 11:20:30] INFO - Uploading component artifacts to S3 bucket: greengrass-component-artifacts-us-west-2-123456789012. If this is your first time using this bucket, add the 's3:GetObject' permission to each core device's token exchange role to allow it to download the component artifacts. For more information, see https://docs.aws.amazon.com/greengrass/v2/developerguide/device-service-role.html.
   [2022-04-28 11:20:30] INFO - Not creating an artifacts bucket as it already exists.
   [2022-04-28 11:20:30] INFO - Updating the component recipe com.example.BatteryAwareHelloWorld-1.0.0.
   [2022-04-28 11:20:31] INFO - Creating a new greengrass component com.example.BatteryAwareHelloWorld-1.0.0
   [2022-04-28 11:20:31] INFO - Created private version '1.0.0' of the component in the account.'com.example.BatteryAwareHelloWorld'.
   ```

1. 从输出中复制 S3 存储桶名称。您稍后会使用桶名称来允许核心设备从该桶下载组件构件。

1. （可选）在 Amazon IoT Greengrass 控制台中查看组件以验证其是否成功上传。执行以下操作：

   1. 在 [Amazon IoT Greengrass 控制台](https://console.amazonaws.cn/greengrass)导航菜单中，选择**组件**。

   1. 在**组件**页面上，选择**我的组件**选项卡，然后选择 **com.example.BatteryAwareHelloWorld**。

      在此页面上，您可以看到组件的配方以及有关该组件的其他信息。

1. <a name="core-device-allow-s3-bucket-access-console-intro-1"></a>允许核心设备访问 S3 存储桶中的组件构件。

   <a name="core-device-allow-s3-bucket-access-console-intro-2"></a>每台核心设备都有一个[核心设备 IAM 角色](device-service-role.md)，允许其与云进行交互 Amazon IoT 并将日志发送到 Amazon 云端。默认情况下，此设备角色不允许访问 S3 存储桶，因此您必须创建并附加一个允许核心设备从 S3 存储桶检索组件构件的策略。

   <a name="core-device-allow-s3-bucket-access-console-intro-3"></a>如果设备的角色已允许访问 S3 存储桶，则可跳过此步骤。否则，请创建允许访问的 IAM 策略并将其附加到该角色，如下所示：

   1. <a name="core-device-allow-s3-bucket-access-console-step-1"></a>在 [IAM 控制台](https://console.amazonaws.cn/iam)的导航菜单中，选择**策略**，然后选择**创建策略**。

   1. 在 **JSON** 选项卡中，将占位符内容替换为以下策略。{{greengrass-component-artifacts-us-west-2-123456789012}}替换为 GDK CLI 上传组件工件的 S3 存储桶的名称。

      例如，如果您在 GDK CLI 配置文件中指定了 **greengrass-component-artifacts** 和 **us-west-2**，而您的 Amazon Web Services 账户 ID 为 **123456789012**，则 GDK CLI 将使用名为 `greengrass-component-artifacts-us-west-2-123456789012` 的 S3 存储桶。

------
#### [ JSON ]

****  

      ```
      {
        "Version":"2012-10-17",		 	 	 
        "Statement": [
          {
            "Effect": "Allow",
            "Action": [
              "s3:GetObject"
            ],
            "Resource": "arn:aws:s3:::{{greengrass-component-artifacts-us-west-2-123456789012}}/*"
          }
        ]
      }
      ```

------

   1. <a name="core-device-allow-s3-bucket-access-console-step-3"></a>选择**下一步**。

   1. <a name="core-device-allow-s3-bucket-access-console-step-4"></a>在**策略详细信息部分**，对于**名称**，请输入 **MyGreengrassV2ComponentArtifactPolicy**。

   1. <a name="core-device-allow-s3-bucket-access-console-step-5"></a>选择**创建策略**。

   1. <a name="core-device-allow-s3-bucket-access-console-step-6"></a>在 [IAM 控制台](https://console.amazonaws.cn/iam)导航菜单中，选择**角色**，然后选择核心设备的角色名称。您在安装 C Amazon IoT Greengrass ore 软件时指定了此角色名称。如果您未指定名称，则默认使用 `GreengrassV2TokenExchangeRole`。

   1. <a name="core-device-allow-s3-bucket-access-console-step-7"></a>在**权限**下，选择**添加权限**，然后选择**附加策略**。

   1. <a name="core-device-allow-s3-bucket-access-console-step-8"></a>在**添加权限**页面，选中与您创建的策略 `MyGreengrassV2ComponentArtifactPolicy` 对应的复选框，然后选择**添加权限**。