

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

# 刪除專案政策 (SDK)
<a name="md-delete-project-policy.title"></a>

您可以使用 [DeleteProjectPolicy](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_DeleteProjectPolicy) 操作，從 Amazon Rekognition 自訂標籤專案刪除現有專案政策的修訂版。如果您要刪除連接至專案之專案政策的所有修訂版，請使用 [ListProjectPolicy](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_ListProjectPolicies) 來取得連接至專案的每個專案政策的修訂 ID。然後呼叫每個政策名稱的 `DeletePolicy`。

**刪除專案政策的修訂版 (SDK)**

1. 如果您尚未這麼做，請安裝並設定 AWS CLI 和 AWS SDKs。如需詳細資訊，請參閱[步驟 4：設定 AWS CLI 和 AWS SDKs](su-awscli-sdk.md)。

1. 使用下列程式碼刪除專案政策。

   DeletePolicy 需要`ProjectARN`、`PolicyName` 和 `PolicyRevisionId`。此 API 則需要 `ProjectARN` 和 `PolicyName`。`PolicyRevisionId` 為選用，但可以因原子更新的目的而包含在內。

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

   變更下列值：
   + `policy-name` 至要刪除的專案政策的名稱。
   + `policy-revision-id` 至要刪除的專案政策的修訂 ID。
   + `project-arn` 至包含要刪除的專案政策修訂版之專案的 Amazon Resource Name。

   ```
   aws rekognition delete-project-policy \
       --policy-name policy-name \
       --policy-revision-id policy-revision-id \
       --project-arn project-arn \
     --profile custom-labels-access
   ```

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

   使用以下程式碼。請提供以下命令列參數：
   + `policy-name` — 要刪除的專案政策的名稱。
   + `project-arn` — 包含要刪除的專案政策的專案的 Amazon Resource Name。
   + `policy-revision-id` — 要刪除的專案政策的修訂 ID。

   例如：`python delete_project_policy.py policy_name project_arn` *policy\$1revision\$1id* 

   ```
   # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
   # SPDX-License-Identifier: Apache-2.0
   
   """
   Purpose
   Amazon Rekognition Custom Labels model example used in the service documentation:
   https://docs.aws.amazon.com/rekognition/latest/customlabels-dg/md-copy-model-sdk.html
   Shows how to delete a revision of a project policy.
   """
   
   import argparse
   import logging
   import boto3
   from botocore.exceptions import ClientError
   
   logger = logging.getLogger(__name__)
   
   
   def delete_project_policy(rekognition_client, policy_name,  project_arn, policy_revision_id=None):
       """
       Deletes a project policy.
   
       :param rekognition_client: A Boto3 Amazon Rekognition client.
       :param policy_name: The name of the project policy that you want to delete.
       :param policy_revision_id: The revsion ID for the project policy that you want to delete.
       :param project_arn: The Amazon Resource Name of the project that contains the project policy
       that you want to delete.
       """
       try:
           logger.info("Deleting project policy: %s", policy_name)
   
           if policy_revision_id is None:
               rekognition_client.delete_project_policy(
                   PolicyName=policy_name,
                   ProjectArn=project_arn)
   
           else:
               rekognition_client.delete_project_policy(
                   PolicyName=policy_name,
                   PolicyRevisionId=policy_revision_id,
                   ProjectArn=project_arn)
   
           logger.info("Deleted project policy: %s", policy_name)
       except ClientError:
           logger.exception("Couldn't delete project policy.")
           raise
   
   
   def confirm_project_policy_deletion(policy_name):
       """
       Confirms deletion of the project policy. Returns True if delete entered.
       :param model_arn: The ARN of the model that you want to delete.
       """
       print(
           f"Are you sure you wany to delete project policy {policy_name} ?\n", policy_name)
   
       delete = input("Enter delete to delete your project policy: ")
       if delete == "delete":
           return True
       else:
           return False
   
   
   def add_arguments(parser):
       """
       Adds command line arguments to the parser.
       :param parser: The command line parser.
       """
   
       parser.add_argument(
           "policy_name", help="The ARN of the project that contains the project policy that you want to delete."
       )
   
       parser.add_argument(
           "project_arn", help="The ARN of the project project policy you want to delete."
       )
   
       parser.add_argument(
           "--policy_revision_id", help="(Optional) The revision ID of the project policy that you want to delete.",
           required=False
       )
   
   
   def main():
   
       logging.basicConfig(level=logging.INFO,
                           format="%(levelname)s: %(message)s")
   
       try:
   
           # Get command line arguments.
           parser = argparse.ArgumentParser(usage=argparse.SUPPRESS)
           add_arguments(parser)
           args = parser.parse_args()
   
           if confirm_project_policy_deletion(args.policy_name) is True:
               print(f"Deleting project_policy: {args.policy_name}")
   
               session = boto3.Session(profile_name='custom-labels-access')
               rekognition_client = session.client("rekognition")
   
               # Delete the project policy.
   
               delete_project_policy(rekognition_client,
                                     args.policy_name,
                                     args.project_arn,
                                     args.policy_revision_id)
   
               print(f"Finished deleting project policy: {args.policy_name}")
           else:
               print(f"Not deleting project policy {args.policy_name}")
       except ClientError as err:
           print(f"Couldn't delete project policy in {args.policy_name}: {err}")
   
   
   
   if __name__ == "__main__":
       main()
   ```

------
#### [ Java V2 ]

   使用以下程式碼。請提供以下命令列參數：
   + `policy-name` — 要刪除的專案政策的名稱。
   + `project-arn` — 包含要刪除的專案政策的專案的 Amazon Resource Name。
   + `policy-revision-id` — 要刪除的專案政策的修訂 ID。

   ```
   /*
      Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
      SPDX-License-Identifier: Apache-2.0
   */
   
   package com.example.rekognition;
   
   import java.util.logging.Level;
   import java.util.logging.Logger;
   
   import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
   import software.amazon.awssdk.regions.Region;
   import software.amazon.awssdk.services.rekognition.RekognitionClient;
   import software.amazon.awssdk.services.rekognition.model.DeleteProjectPolicyRequest;
   
   import software.amazon.awssdk.services.rekognition.model.RekognitionException;
   
   public class DeleteProjectPolicy {
   
       public static final Logger logger = Logger.getLogger(DeleteProjectPolicy.class.getName());
   
       public static void deleteMyProjectPolicy(RekognitionClient rekClient, String projectArn,
               String projectPolicyName,
               String projectPolicyRevisionId)
               throws InterruptedException {
   
           try {
               String[] logArguments = new String[] { projectPolicyName, projectPolicyRevisionId };
   
               logger.log(Level.INFO, "Deleting: Project policy: {0} revision: {1}", logArguments);
   
               // Delete the project policy.
   
               DeleteProjectPolicyRequest deleteProjectPolicyRequest = DeleteProjectPolicyRequest.builder()
                       .policyName(projectPolicyName)
                       .policyRevisionId(projectPolicyRevisionId)
                       .projectArn(projectArn).build();
   
               rekClient.deleteProjectPolicy(deleteProjectPolicyRequest);
   
               logger.log(Level.INFO, "Deleted: Project policy: {0} revision: {1}", logArguments);
   
           } catch (
   
           RekognitionException e) {
               logger.log(Level.SEVERE, "Client error occurred: {0}", e.getMessage());
               throw e;
           }
   
       }
   
       public static void main(String args[]) {
   
           final String USAGE = "\n" + "Usage: " + "<project_arn> <project_policy_name> <project_policy_revision_id>\n\n"
                   + "Where:\n"
                   + "   project_arn - The ARN of the project that has the project policy that you want to delete.\n\n"
                   + "   project_policy_name - The name of the project policy that you want to delete.\n\n"
                   + "   project_policy_revision_id - The revision of the project policy that you want to delete.\n\n";
   
           if (args.length != 3) {
               System.out.println(USAGE);
               System.exit(1);
           }
   
           String projectArn = args[0];
           String projectPolicyName = args[1];
           String projectPolicyRevisionId = args[2];
   
           try {
   
               RekognitionClient rekClient = RekognitionClient.builder()
               .credentialsProvider(ProfileCredentialsProvider.create("custom-labels-access"))
               .region(Region.US_WEST_2)
               .build();
   
               // Delete the project policy.
               deleteMyProjectPolicy(rekClient, projectArn, projectPolicyName, projectPolicyRevisionId);
   
               System.out.println(String.format("project policy deleted: %s revision: %s", projectPolicyName,
                       projectPolicyRevisionId));
   
               rekClient.close();
   
           } catch (RekognitionException rekError) {
               logger.log(Level.SEVERE, "Rekognition client error: {0}", rekError.getMessage());
               System.exit(1);
           }
   
           catch (InterruptedException intError) {
               logger.log(Level.SEVERE, "Exception while sleeping: {0}", intError.getMessage());
               System.exit(1);
           }
   
       }
   
   }
   ```

------