

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

# 建立 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"
    )
```

------