Deleting a project policy (SDK) - Rekognition

Deleting a project policy (SDK)

You can use the DeleteProjectPolicy operation to delete a revision of an existing project policy from an Amazon Rekognition Custom Labels project. If you want to delete all revisions of a project policy that are attached to a project, use ListProjectPolicies to get the revision IDs of each project policy attached to the project. Then call DeletePolicy for each policy name.

To delete a revision of a project policy (SDK)
  1. If you haven't already done so, install and configure the AWS CLI and the AWS SDKs. For more information, see Step 4: Set up the AWS CLI and AWS SDKs.

  2. Use the following code to delete a project policy.

    DeletePolicy takes ProjectARN, PolicyName and PolicyRevisionId. ProjectARN and PolicyName are required for this API. PolicyRevisionId is optional, but can be included for the purposes of atomic updates.

    AWS CLI

    Change the following values:

    • policy-name to the name of the project policy that you want to delete.

    • policy-revision-id to the revision ID of the project policy that you want to delete.

    • project-arn to the Amazon Resource Name of the project that contains the revision of the project policy that you want to delete.

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

    Use the following code. Supply the following command line parameters:

    • policy-name – The name of the project policy that you want to delete.

    • project-arn – The Amazon Resource Name of the project that contains the project policy that you want to delete.

    • policy-revision-id – The revision ID of the project policy that you want to delete.

    For example: python delete_project_policy.py policy_name project_arn policy_revision_id

    # 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

    Use the following code. Supply the following command line parameters:

    • policy-name – The name of the project policy that you want to delete.

    • project-arn – The Amazon Resource Name of the project that contains the project policy that you want to delete.

    • policy-revision-id – The revision ID of the project policy that you want to delete.

    /* 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); } } }