Amazon Kendra 從 2026 年 7 月 30 日開始, 將不再開放給新客戶。如果您想要使用 服務,請在 7 月 30 日之前註冊。對於類似 的功能 Amazon Kendra,探索 Amazon Bedrock 知識庫。進一步了解。
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
建立 Amazon S3 資料來源
下列範例示範如何建立 Amazon S3 資料來源。這些範例假設您已建立 索引和 IAM 角色,具有從索引讀取資料的許可。如需 IAM 角色的詳細資訊,請參閱IAM 存取角色。如需建立索引的詳細資訊,請參閱建立索引。
- 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)。
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 API 來監控進度。當資料來源狀態為 時ACTIVE,資料來源即可使用。
下列範例示範取得資料來源的狀態。
- CLI
-
aws kendra describe-data-source \
--index-id index ID \
--id data source ID
- Python
-
下列 Python 程式碼片段會取得 S3 資料來源的相關資訊。如需完整範例,請參閱 開始使用 (適用於 Python (Boto3) 的 AWS SDK)。
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 來同步索引與資料來源。
下列範例示範同步資料來源。
- CLI
-
aws kendra start-data-source-sync-job \
--index-id index ID \
--id data source ID
- Python
-
下列 Python 程式碼片段會 Amazon S3 同步資料來源。如需完整範例,請參閱 開始使用 (適用於 Python (Boto3) 的 AWS SDK)。
print("Synchronize the data source.")
sync_response = kendra.start_data_source_sync_job(
Id = "data-source-id",
IndexId = "index-id"
)