

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

# 與另一個 共用自訂模型 AWS 帳戶
<a name="custom-copy-sharing"></a>

使用 Amazon Comprehend，您可以與他人共用自訂模型，讓他們可以將模型匯入其 AWS 帳戶。當使用者匯入其中一個自訂模型時，會在其帳戶中建立新的自訂模型。他們的新模型會複製您共用的模型。

若要共用自訂模型，請將授權其他人匯入的政策連接到該模型。然後，您會提供這些使用者所需的詳細資訊。

**注意**  
當其他使用者匯入您已共用的自訂模型時，他們必須使用包含模型的相同 AWS 區域 - 例如美國東部 （維吉尼亞北部）。

**Topics**
+ [開始之前](#custom-copy-sharing-prerequisites)
+ [自訂模型的資源型政策](#custom-copy-sharing-example-policy)
+ [步驟 1：將資源型政策新增至自訂模型](#custom-copy-sharing-adding-policy)
+ [步驟 2：提供其他人匯入所需的詳細資訊](#custom-copy-sharing-details)

## 開始之前
<a name="custom-copy-sharing-prerequisites"></a>

您必須先在 Amazon Comprehend 中擁有訓練過的自訂分類器或自訂實體識別器，才能共用模型 AWS 帳戶。如需訓練自訂模型的詳細資訊，請參閱 [自訂分類](how-document-classification.md)或 [自訂實體辨識](custom-entity-recognition.md)。

### 所需的許可
<a name="custom-copy-sharing-prerequisites-permissions"></a>

#### IAM 政策陳述式
<a name="custom-copy-sharing-prerequisites-permissions-iam"></a>

在將資源型政策新增至自訂模型之前，您需要 (IAM) 中的 AWS Identity and Access Management 許可。您的使用者、群組或角色必須連接政策，才能建立、取得和刪除模型政策，如下列範例所示。

**Example 管理自訂模型之資源型政策的 IAM 政策**  

```
{
  "Effect": "Allow",
  "Action": [
    "comprehend:PutResourcePolicy",
    "comprehend:DeleteResourcePolicy",
    "comprehend:DescribeResourcePolicy"
  ],
  "Resource": "arn:aws:comprehend:us-west-2:111122223333:document-classifier/foo/version/*"
}
```

如需建立 IAM 政策的相關資訊，請參閱《[IAM 使用者指南》中的建立 IAM 政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create.html)。 **如需有關連接 IAM 政策的資訊，請參閱《[IAM 使用者指南》中的新增和移除 IAM 身分許可](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage-attach-detach.html)。 **

#### AWS KMS 金鑰政策陳述式
<a name="custom-copy-sharing-prerequisites-permissions-kms"></a>

如果您要共用加密的模型，則可能需要新增 的許可 AWS KMS。此要求取決於您在 Amazon Comprehend 中用來加密模型的 KMS 金鑰類型。

**AWS 擁有的金鑰** 由 AWS 服務擁有和管理。如果您使用 AWS 擁有的金鑰，則不需要新增 的許可 AWS KMS，而且可以略過本節。

**客戶受管金鑰**是您在 中建立、擁有和管理的金鑰 AWS 帳戶。如果您使用客戶受管金鑰，則必須將陳述式新增至 KMS 金鑰政策。

政策陳述式會授權一或多個實體 （例如使用者或帳戶） 執行解密模型所需的 AWS KMS 操作。

您可以使用條件索引鍵來協助防止混淆代理人問題。如需詳細資訊，請參閱[預防跨服務混淆代理人](cross-service-confused-deputy-prevention.md)。

在政策中使用下列條件金鑰來驗證存取 KMS 金鑰的實體。當使用者匯入模型時， 會 AWS KMS 檢查來源模型版本的 ARN 是否符合條件。如果您未在政策中包含條件，則指定的委託人可以使用 KMS 金鑰來解密任何模型版本：
+ [aws：SourceArn](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-sourcearn) – 使用此條件索引鍵搭配 `kms:GenerateDataKey`和 `kms:Decrypt`動作。
+ [kms:EncryptionContext](https://docs.aws.amazon.com/kms/latest/developerguide/policy-conditions.html#conditions-kms-encryption-context) – 使用此條件索引鍵搭配 `kms:GenerateDataKey`、 `kms:Decrypt`和 `kms:CreateGrant`動作。

在下列範例中，政策授權 AWS 帳戶 `444455556666` 使用 擁有 AWS 帳戶 的指定分類器模型第 1 版`111122223333`。

**Example 存取特定分類器模型版本的 KMS 金鑰政策**    
****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
          "AWS":
                "arn:aws:iam::444455556666:root"
      },
      "Action": [
          "kms:Decrypt",
          "kms:GenerateDataKey"
      ],
      "Resource": "*",
      "Condition": {
          "StringEquals": {
              "aws:SourceArn":
                "arn:aws:comprehend:us-west-2:111122223333:document-classifier/classifierName/version/1"
          }
      }
    },
    {
      "Effect": "Allow",
      "Principal": {
          "AWS":  "arn:aws:iam::444455556666:root"
      },
      "Action": "kms:CreateGrant",
      "Resource": "*",
      "Condition": {
        "StringEquals": {
           "kms:EncryptionContext:aws:comprehend:arn":
              "arn:aws:comprehend:us-west-2:111122223333:document-classifier/classifierName/version/1"
        }
      }
    }
  ]
}
```

下列範例政策授權來自 的使用者 **ExampleUser ** AWS 帳戶 `123456789012` AWS 帳戶 `444455556666`和來自 的 **ExampleRole**，透過 Amazon Comprehend 服務存取此 KMS 金鑰。

**Example 允許存取 Amazon Comprehend 服務的 KMS 金鑰政策 （替代 1)。**  

下列範例政策授權 AWS 帳戶 `444455556666` 使用上一個範例的替代語法，透過 Amazon Comprehend 服務存取此 KMS 金鑰。

**Example 允許存取 Amazon Comprehend 服務的 KMS 金鑰政策 （替代 2)。**    
****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
          "AWS": "arn:aws:iam::444455556666:root"
      },
      "Action": [
          "kms:Decrypt",
          "kms:GenerateDataKey",
          "kms:CreateGrant"
      ],
      "Resource": "*",
      "Condition": {
          "StringLike": {
              "kms:EncryptionContext:aws:comprehend:arn": "arn:aws:comprehend:*"
          }
      }
    }
  ]
}
```

如需詳細資訊，請參閱*《AWS Key Management Service 開發人員指南》*中的[在 AWS KMS中使用金鑰政策](https://docs.aws.amazon.com/kms/latest/developerguide/key-policies.html)。

## 自訂模型的資源型政策
<a name="custom-copy-sharing-example-policy"></a>

在另一個 中的 Amazon Comprehend 使用者可從您的帳戶 AWS 帳戶 AWS 匯入自訂模型之前，您必須授權他們這樣做。若要授權這些政策，請將*資源型政策*新增至您要共用的模型版本。以資源為基礎的政策是您連接到資源的 IAM 政策 AWS。

當您將資源政策連接至自訂模型版本時，政策會授權使用者、群組或角色對模型版本執行`comprehend:ImportModel`動作。

**Example 自訂模型版本的資源型政策**  
此範例會在 `Principal` 屬性中指定授權的實體。資源「\$1」是指您連接政策的特定模型版本。    
****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "comprehend:ImportModel",
      "Resource": "*",
      "Principal": {
        "AWS": [
                "arn:aws:iam::111122223333:root",
                "arn:aws:iam::444455556666:user/ExampleUser",
                "arn:aws:iam::123456789012:role/ExampleRole"
         ]
      }
    }
  ]
}
```
對於您連接到自訂模型的政策， `comprehend:ImportModel`是 Amazon Comprehend 支援的唯一動作。  
如需資源型政策的詳細資訊，請參閱《*IAM 使用者指南*》中的[身分型政策和資源型政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_identity-vs-resource.html)。

## 步驟 1：將資源型政策新增至自訂模型
<a name="custom-copy-sharing-adding-policy"></a>

您可以使用 AWS 管理主控台 AWS CLI或 Amazon Comprehend API 來新增資源型政策。

### AWS 管理主控台
<a name="custom-copy-sharing-adding-policy-console"></a>

您可以在 中使用 Amazon Comprehend AWS 管理主控台。

**新增以資源為基礎的政策**

1. 登入 AWS 管理主控台 並開啟位於 https：//[https://console.aws.amazon.com/comprehend/](https://console.aws.amazon.com/comprehend/) 的 Amazon Comprehend 主控台

1. 在左側導覽選單的**自訂**下，選擇包含自訂模型的頁面：

   1. 如果您要共用自訂文件分類器，請選擇**自訂分類**。

   1. 如果您要共用自訂實體辨識器，請選擇**自訂實體辨識**。

1. 在模型清單中，選擇模型名稱以開啟其詳細資訊頁面。

1. 在**版本**下，選擇您要共用的模型版本名稱。

1. 在版本詳細資訊頁面上，選擇**標籤、VPC 和政策**索引標籤。

1. 在**資源型政策**區段中，選擇**編輯**。

1. 在**編輯資源型政策頁面上**，執行下列動作：

   1. 針對**政策名稱**，輸入可協助您在建立政策之後辨識政策的名稱。

   1. 在**授權**下，指定下列一或多個實體，以授權它們匯入您的模型：    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/comprehend/latest/dg/custom-copy-sharing.html)

1. 在**共用**下，您可以複製模型版本的 ARN，以協助您與將匯入模型的人員共用。當有人從不同的 匯入自訂模型時 AWS 帳戶，需要模型版本 ARN。

1. 選擇**儲存**。Amazon Comprehend 會建立以資源為基礎的政策，並將其連接到您的模型。

### AWS CLI
<a name="custom-copy-sharing-adding-policy-cli"></a>

若要使用 將資源型政策新增至自訂模型 AWS CLI，請使用 [PutResourcePolicy](https://docs.aws.amazon.com/comprehend/latest/dg/API_PutResourcePolicy.html) 命令。 命令接受下列參數：
+ `resource-arn` – 自訂模型的 ARN，包括模型版本。
+ `resource-policy` – JSON 檔案，定義要連接到自訂模型的資源型政策。

  您也可以提供政策做為內嵌 JSON 字串。若要為您的政策提供有效的 JSON，請使用雙引號括住屬性名稱和值。如果 JSON 內文也以雙引號括住，您會逸出政策內的雙引號。
+ `policy-revision-id` – Amazon Comprehend 指派給您正在更新之政策的修訂 ID。如果您要建立沒有先前版本的新政策，請勿使用此參數。Amazon Comprehend 會為您建立修訂 ID。

**Example 使用 `put-resource-policy`命令將資源型政策新增至自訂模型**  
此範例在名為 **policyFile.json** 的 JSON 檔案中定義政策，並將政策與模型建立關聯。模型是名為 **mycf1** 的分類器版本 **v2**。  

```
$ aws comprehend put-resource-policy \
> --resource-arn arn:aws:comprehend:us-west-2:111122223333:document-classifier/mycf1/version/v2 \
> --resource-policy file://policyFile.json \
> --policy-revision-id revision-id
```
資源政策的 JSON 檔案包含下列內容：  
+ *動作* – 政策授權具名委託人使用 `comprehend:ImportModel`。
+ *資源* – 自訂模型的 ARN。資源「\$1」是指您在 `put-resource-policy` 命令中指定的模型版本。
+ *委託人* – 政策授權`jane`來自 AWS 帳戶 444455556666 的使用者和來自 AWS 帳戶 123456789012 的所有使用者。  
****  

```
{
"Version":"2012-10-17",		 	 	 
 "Statement":[
    {"Sid":"ResourcePolicyForImportModel",
     "Effect":"Allow",
     "Action":["comprehend:ImportModel"],
     "Resource":"*",
     "Principal":
         {"AWS":
            ["arn:aws:iam::444455556666:user/jane",
             "123456789012"]
         }
   }
 ]
}
```

### Amazon Comprehend API
<a name="custom-copy-sharing-adding-policy-api"></a>

若要使用 Amazon Comprehend API 將資源型政策新增至自訂模型，請使用 [PutResourcePolicy](https://docs.aws.amazon.com/comprehend/latest/dg/API_PutResourcePolicy.html) API 操作。

您也可以在建立模型的 API 請求中，將政策新增至自訂模型。若要這樣做，請在提交 [CreateDocumentClassifier](https://docs.aws.amazon.com/comprehend/latest/dg/API_CreateDocumentClassifier.html) 或 [CreateEntityRecognizer](https://docs.aws.amazon.com/comprehend/latest/dg/API_CreateEntityRecognizer.html) 請求時，提供 ModelPolicy 參數的政策 JSON。

## 步驟 2：提供其他人匯入所需的詳細資訊
<a name="custom-copy-sharing-details"></a>

現在您已將資源型政策新增至自訂模型，您已授權其他 Amazon Comprehend 使用者將模型匯入其 AWS 帳戶。不過，在他們可以匯入之前，您必須提供他們下列詳細資訊：
+ 模型版本的 Amazon Resource Name (ARN)。
+ 包含模型的 AWS 區域 。匯入模型的任何人都必須使用相同的 AWS 區域 。
+ 模型是否已加密，如果是，則為您使用的 AWS KMS 金鑰類型： AWS 擁有的金鑰 或客戶受管金鑰。
+ 如果您的模型使用客戶受管金鑰加密，則必須提供 KMS 金鑰的 ARN。匯入模型的任何人都必須在其 IAM 服務角色中包含 ARN AWS 帳戶。此角色授權 Amazon Comprehend 在匯入期間使用 KMS 金鑰解密模型。

如需其他使用者如何匯入模型的詳細資訊，請參閱 [從另一個 匯入自訂模型 AWS 帳戶](custom-copy-importing.md)。