

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

# 创建 Amazon S3 数据源
<a name="create-ds-s3"></a>

以下示例演示如何创建 Amazon S3 数据源。这些示例假设您已经创建了一个索引和一个有权从索引中读取数据的 IAM 角色。有关该 IAM 角色的更多信息，请参阅[IAM 访问角色](https://docs.aws.amazon.com/kendra/latest/dg/iam-roles.html#iam-roles-ds)。有关创建索引的更多信息，请参阅[创建索引](https://docs.aws.amazon.com/kendra/latest/dg/create-index.html)。

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

```
aws kendra create-data-source \
 --index-id index ID \
 --name example-data-source \
 --type S3 \
 --configuration '{"S3Configuration":{"BucketName":"bucket name"}}' 
 --role-arn 'arn:aws:iam::account id:role:/role name
```

------
#### [ Python ]

以下 Python 代码片段创建了一个 Amazon S3 数据源。有关完整示例，请参阅[入门 (适用于 Python (Boto3) 的 AWS SDK)](gs-python.md)。

```
print("Create an Amazon S3 data source.")
    
    # Provide a name for the data source
    name = "getting-started-data-source"
    # Provide an optional description for the data source
    description = "Getting started data source."
    # Provide the IAM role ARN required for data sources
    role_arn = "arn:aws:iam::${accountID}:role/${roleName}"
    # Provide the data soource connection information
    s3_bucket_name = "S3-bucket-name"
    type = "S3"
    # Configure the data source
    configuration = {"S3DataSourceConfiguration":
        {
            "BucketName": s3_bucket_name
        }
    }

    data_source_response = kendra.create_data_source(
        Configuration = configuration,
        Name = name,
        Description = description,
        RoleArn = role_arn,
        Type = type,
        IndexId = index_id
    )
```

------

创建数据来源可能需要一些时间。您可以使用 [DescribeDataSource](https://docs.aws.amazon.com/kendra/latest/APIReference/API_DescribeDataSource.html)API 监控进度。当数据来源状态为 `ACTIVE` 时，数据来源就已准备就绪。

以下示例演示如何获取数据来源的状态。

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

```
aws kendra describe-data-source \
 --index-id index ID \
 --id data source ID
```

------
#### [ Python ]

以下 Python 代码片段用于获取有关 S3 数据来源的信息。有关完整示例，请参阅[入门 (适用于 Python (Boto3) 的 AWS SDK)](gs-python.md)。

```
print("Wait for Amazon Kendra to create the data source.")

    while True:
        data_source_description = kendra.describe_data_source(
            Id = "data-source-id",
            IndexId = "index-id"
        )
        status = data_source_description["Status"]
        print(" Creating data source. Status: "+status)
        time.sleep(60)
        if status != "CREATING":
            break
```

------

此数据来源没有计划，因此不会自动运行。要为数据源编制索引，[StartDataSourceSyncJob](https://docs.aws.amazon.com/kendra/latest/APIReference/API_StartDataSourceSyncJob.html)需要调用将索引与数据源同步。

以下示例演示如何同步数据来源。

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

```
aws kendra start-data-source-sync-job \
 --index-id index ID \
 --id data source ID
```

------
#### [ Python ]

以下 Python 代码片段同步了一个 Amazon S3 数据来源。有关完整示例，请参阅[入门 (适用于 Python (Boto3) 的 AWS SDK)](gs-python.md)。

```
print("Synchronize the data source.")

    sync_response = kendra.start_data_source_sync_job(
        Id = "data-source-id",
        IndexId = "index-id"
    )
```

------