

# 爬网程序与注册的 Amazon S3 位置位于不同账户（跨账户爬取）时所需的设置
<a name="cross-account-crawling"></a>

要允许爬网程序使用 Lake Formation 凭证访问不同账户中的数据存储，您必须先向 Lake Formation 注册 Amazon S3 数据位置。然后，通过执行以下步骤向爬网程序的账户授予数据位置权限。

您可以使用 AWS 管理控制台 或 AWS CLI 完成以下步骤。

------
#### [ AWS 管理控制台 ]

1. 在注册 Amazon S3 位置的账户（账户 B）中：

   1. 向 Lake Formation 注册 Amazon S3 路径。有关更多信息，请参阅[注册 Amazon S3 位置](https://docs.aws.amazon.com/lake-formation/latest/dg/register-location.html)。

   1.  向将运行爬网程序的账户（账户 A）授予 **Data location**（数据位置）权限。有关更多信息，请参阅[授予数据位置权限](https://docs.aws.amazon.com/lake-formation/latest/dg/granting-location-permissions-local.html)。

   1. 在 Lake Formation 中创建一个空数据库，将基础位置作为目标 Amazon S3 位置。有关更多信息，请参阅[创建数据库](https://docs.aws.amazon.com/lake-formation/latest/dg/creating-database.html)。

   1. 授予账户 A（将运行爬网程序的账户）访问您在上一步中创建的数据库的权限。有关更多信息，请参阅[授予数据库权限](https://docs.aws.amazon.com/lake-formation/latest/dg/granting-database-permissions.html)。

1. 在创建并将运行爬网程序的账户（账户 A）中：

   1.  使用 AWS RAM 控制台，接受从外部账户（账户 B）共享的数据库。有关更多信息，请参阅 [Accepting a resource share invitation from AWS Resource Access Manager](https://docs.aws.amazon.com/lake-formation/latest/dg/accepting-ram-invite.html)（接受来自 RAMlong 的资源共享邀请）。

   1.  为爬网程序创建 IAM 角色。将 `lakeformation:GetDataAccess` 策略添加到该角色。

   1.  在 Lake Formation 控制台（[https://console.aws.amazon.com/lakeformation/](https://console.aws.amazon.com/lakeformation/)）中，授予用于爬网程序运行的 IAM 角色目标 Amazon S3 位置上的 **Data location**（数据位置）权限，以使爬网程序能够从 Lake Formation 中的目标读取数据。有关更多信息，请参阅[授予数据位置权限](https://docs.aws.amazon.com/lake-formation/latest/dg/granting-location-permissions-local.html)。

   1.  在共享数据库上创建资源链接。有关更多信息，请参阅[创建资源链接](https://docs.aws.amazon.com/lake-formation/latest/dg/create-resource-link-database.html)。

   1.  授予爬网程序角色对共享数据库和（`Describe`）资源链接的访问权限（`Create`）。资源链接在爬网程序的输出中指定。

   1.  在 AWS Glue 控制台（[https://console.aws.amazon.com/glue/](https://console.aws.amazon.com/glue/)）中配置爬网程序时，选择选项 **Use Lake Formation credentials for crawling Amazon S3 data source**（使用 Lake Formation 凭证爬取 Amazon S3 数据来源）。

      对于跨账户爬取，请指定向 Lake Formation 注册的目标 Amazon S3 位置的 AWS 账户 ID。对于账户内爬取，accountId 字段是可选的。  
![Lake Formation 配置部分，其中账户 ID 字段显示为 111111111111。](http://docs.aws.amazon.com/zh_cn/glue/latest/dg/images/cross-account-crawler.png)

------
#### [ AWS CLI ]

```
aws glue --profile demo create-crawler --debug --cli-input-json '{
    "Name": "prod-test-crawler",
    "Role": "arn:aws:iam::111122223333:role/service-role/AWSGlueServiceRole-prod-test-run-role",
    "DatabaseName": "prod-run-db",
    "Description": "",
    "Targets": {
    "S3Targets":[
                {
                 "Path": "s3://amzn-s3-demo-bucket"
                }
                ]
                },
   "SchemaChangePolicy": {
      "UpdateBehavior": "LOG",
      "DeleteBehavior": "LOG"
  },
  "RecrawlPolicy": {
    "RecrawlBehavior": "CRAWL_EVERYTHING"
  },
  "LineageConfiguration": {
    "CrawlerLineageSettings": "DISABLE"
  },
  "LakeFormationConfiguration": {
    "UseLakeFormationCredentials": true,
    "AccountId": "111111111111"
  },
  "Configuration": {
           "Version": 1.0,
           "CrawlerOutput": {
             "Partitions": { "AddOrUpdateBehavior": "InheritFromTable" },
             "Tables": {"AddOrUpdateBehavior": "MergeNewColumns" }
           },
           "Grouping": { "TableGroupingPolicy": "CombineCompatibleSchemas" }
         },
  "CrawlerSecurityConfiguration": "",
  "Tags": {
    "KeyName": ""
  }
}'
```

------

**注意**  
只有 Amazon S3 和 Data Catalog 目标支持使用 Lake Formation 凭证的爬网程序。
对于使用 Lake Formation 凭证售卖的目标，基础 Amazon S3 位置必须属于同一个存储桶。例如，只要所有目标位置都在同一个存储桶 (amzn-s3-demo-bucket1) 下，客户就可以使用多个目标（s3://amzn-s3-demo-bucket1/folder1、s3://amzn-s3-demo-bucket1/folder2）。不允许指定不同的存储桶（s3://amzn-s3-demo-bucket1/folder1、s3://amzn-s3-demo-bucket2/folder2）。
目前，对于 Data Catalog 目标爬网程序，只允许具有单个目录表的单个目录目标。