

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# ナレッジベースからドキュメントの内容を取得する
<a name="kb-test-get-document-content"></a>

`GetDocumentContent` API を使用すると、Amazon Bedrock ナレッジベースに取り込まれたドキュメントのコンテンツを取得できます。この API は、ドキュメントの元のコンテンツまたは抽出されたコンテンツをダウンロードまたは表示するための一時的で安全なアクセスを提供する署名付き URL を返します。

これは、次の場合に便利です。
+ `Retrieve` API レスポンスで参照されるソースドキュメントにアクセスする
+ ナレッジベースから元のファイル (PDF、Word、HTML など) をダウンロードする
+ JSON 形式でドキュメントの抽出/解析されたテキストコンテンツを取得する
+ `Retrieve` API レスポンスの背後にあるソースドキュメントをユーザーが表示またはダウンロードできるようにするアプリケーションを構築する

## 仕組み
<a name="kb-get-doc-content-how-it-works"></a>

1. ナレッジベース ID、データソース ID、ドキュメント ID `GetDocumentContent`を使用して を呼び出します。

1. サービスはアクセス許可 (ナレッジベースで設定された ACL ベースのアクセスコントロールを含む) を検証します。

1. API は、署名付き URL とドキュメントの MIME タイプを返します。

1. 署名付き URL を使用してドキュメントコンテンツをダウンロードします。URL は **5 分**後に期限切れになります。

## IAM アクセス許可
<a name="kb-get-doc-content-iam"></a>

を呼び出すには、ナレッジベースリソースに対する `bedrock:Retrieve`および `bedrock:GetDocumentContent` IAM アクションの両方`GetDocumentContent`が必要です。これは、API がドキュメントコンテンツを返す前に取得レベルのアクセスを内部的に検証するためです。IAM ポリシーに両方のアクションが含まれていることを確認します。

```
{
    "Effect": "Allow",
    "Action": [
        "bedrock:Retrieve",
        "bedrock:GetDocumentContent"
    ],
    "Resource": "arn:aws:bedrock:{{region}}:{{account-id}}:knowledge-base/{{kb-id}}"
}
```

## 使用例
<a name="kb-get-doc-content-examples"></a>

### ACL が有効になっている同じアカウント
<a name="kb-get-doc-content-same-account-acl"></a>

ナレッジベースで ACL ベースのアクセスコントロールが有効になっている場合は、ユーザーの ID `userContext`を渡して、ドキュメントレベルのアクセス許可チェックを確認します。

```
import boto3
import requests

client = boto3.client('bedrock-agent-runtime')

# Step 1: Retrieve relevant documents
retrieve_response = client.retrieve(
    knowledgeBaseId='{{KBID1234567}}',
    retrievalQuery={'text': 'What is the refund policy?'}
)

# Step 2: Get the full document content for the top result
result = retrieve_response['retrievalResults'][0]

doc_response = client.get_document_content(
    knowledgeBaseId='{{KBID1234567}}',
    dataSourceId=result['metadata']['_data_source_id'],
    documentId=result['documentId'],
    outputFormat='RAW',
    userContext={
        'userId': '{{user-email}}',
        'groups': [
            {'id': '{{group-engineering}}'},
            {'id': '{{group-project-alpha}}'}
        ]
    }
)

# Step 3: Download the document
download = requests.get(doc_response['presignedUrl'])
with open('document.pdf', 'wb') as f:
    f.write(download.content)
```

### ACL が有効になっていない同じアカウント
<a name="kb-get-doc-content-same-account-no-acl"></a>

ACLsは、 を省略します`userContext`。

```
import boto3
import requests

client = boto3.client('bedrock-agent-runtime')

# Step 1: Retrieve relevant documents
retrieve_response = client.retrieve(
    knowledgeBaseId='{{KBID1234567}}',
    retrievalQuery={'text': 'What is the refund policy?'}
)

# Step 2: Get the full document content
result = retrieve_response['retrievalResults'][0]

doc_response = client.get_document_content(
    knowledgeBaseId='{{KBID1234567}}',
    dataSourceId=result['metadata']['_data_source_id'],
    documentId=result['documentId'],
    outputFormat='RAW'
)

# Step 3: Download the document
download = requests.get(doc_response['presignedUrl'])
with open('document.pdf', 'wb') as f:
    f.write(download.content)
```

### ACL が有効になっていないクロスアカウント
<a name="kb-get-doc-content-cross-account"></a>

クロスアカウントアクセスの場合、ナレッジベースの所有者は、発信者のアカウントのアクセス許可を付与する**リソースポリシー**をナレッジベースにアタッチする必要があります。次に、呼び出し元はフルナレッジベース ARN を使用します。

**ステップ 1: KB 所有者がナレッジベースにリソースポリシーをアタッチする**

ナレッジベースを所有するアカウント ( など`999999999999`) は、発信者アカウント ( など`111111111111`) にアクセスを許可するリソースポリシーをアタッチする必要があります。

```
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "111111111111"
            },
            "Action": [
                "bedrock:Retrieve",
                "bedrock:GetDocumentContent"
            ],
            "Resource": "arn:aws:bedrock:us-east-1:999999999999:knowledge-base/{{KBID1234567}}"
        }
    ]
}
```

これは `PutKnowledgeBaseResourcePolicy` API または Amazon Bedrock コンソールを通じて行われます。

**ステップ 2: 発信者アカウントに API を呼び出す IAM アクセス許可がある**

発信者の IAM ロール/ユーザー (アカウント 内`111111111111`) には、クロスアカウント KB ARN に対するアクションを許可する IAM ポリシーが必要です。

```
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "bedrock:Retrieve",
                "bedrock:GetDocumentContent"
            ],
            "Resource": "arn:aws:bedrock:us-east-1:999999999999:knowledge-base/{{KBID1234567}}"
        }
    ]
}
```

**ステップ 3: 完全な KB ARN を使用して API を呼び出す**

```
import boto3
import requests

client = boto3.client('bedrock-agent-runtime')

CROSS_ACCOUNT_KB_ARN = 'arn:aws:bedrock:us-east-1:999999999999:knowledge-base/{{KBID1234567}}'

# Step 1: Retrieve relevant documents using the KB ARN
retrieve_response = client.retrieve(
    knowledgeBaseId=CROSS_ACCOUNT_KB_ARN,
    retrievalQuery={'text': 'What is the refund policy?'}
)

# Step 2: Get the full document content using the same ARN
result = retrieve_response['retrievalResults'][0]

doc_response = client.get_document_content(
    knowledgeBaseId=CROSS_ACCOUNT_KB_ARN,
    dataSourceId=result['metadata']['_data_source_id'],
    documentId=result['documentId'],
    outputFormat='RAW'
)

# Step 3: Download the document
download = requests.get(doc_response['presignedUrl'])
with open('document.pdf', 'wb') as f:
    f.write(download.content)
```

リソースポリシー (KB 所有者側) と IAM ポリシー (発信者側) の両方が設定されている必要があります。のいずれかが欠落している場合、アクセスは拒否されます。