Listing project policies (SDK) - Rekognition

Listing project policies (SDK)

You can use the ListProjectPolicies operation to list the project policies that are attached to an Amazon Rekognition Custom Labels project.

To list the project policies attached to a project (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 list the project policies.

    AWS CLI

    Change project-arn to the Amazon Resource Name of the project for which you want to list the attached project policies.

    aws rekognition list-project-policies \ --project-arn project-arn \ --profile custom-labels-access
    Python

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

    • project_arn — the Amazon Resource Name of the project for which you want to list the attached project policies.

    For example: python list_project_policies.py project_arn

    # 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 list the project policies in an Amazon Rekogntion Custom Labels project. """ import argparse import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def display_project_policy(project_policy): """ Displays information about a Custom Labels project policy. :param project_policy: The project policy (ProjectPolicy) that you want to display information about. """ print(f"Policy name: {(project_policy['PolicyName'])}") print(f"Project Arn: {project_policy['ProjectArn']}") print(f"Document: {(project_policy['PolicyDocument'])}") print(f"Revision ID: {(project_policy['PolicyRevisionId'])}") print() def list_project_policies(rek_client, project_arn): """ Describes an Amazon Rekognition Custom Labels project, or all projects. :param rek_client: The Amazon Rekognition Custom Labels Boto3 client. :param project_arn: The Amazon Resource Name of the project you want to use. """ try: max_results = 5 pagination_token = '' finished = False logger.info("Listing project policies in: %s.", project_arn) print('Projects\n--------') while not finished: response = rek_client.list_project_policies( ProjectArn=project_arn, MaxResults=max_results, NextToken=pagination_token) for project in response['ProjectPolicies']: display_project_policy(project) if 'NextToken' in response: pagination_token = response['NextToken'] else: finished = True logger.info("Finished listing project policies.") except ClientError as err: logger.exception( "Couldn't list policies for - %s: %s", project_arn,err.response['Error']['Message']) raise def add_arguments(parser): """ Adds command line arguments to the parser. :param parser: The command line parser. """ parser.add_argument( "project_arn", help="The Amazon Resource Name of the project for which you want to list project policies." ) 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() print(f"Listing project policies in: {args.project_arn}") # List the project policies. session = boto3.Session(profile_name='custom-labels-access') rekognition_client = session.client("rekognition") list_project_policies(rekognition_client, args.project_arn) except ClientError as err: print(f"Problem list project_policies: {err}") if __name__ == "__main__": main()
    Java V2

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

    • project_arn — The ARN of the project that has the project polices you want to list.

    /* 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.ListProjectPoliciesRequest; import software.amazon.awssdk.services.rekognition.model.ListProjectPoliciesResponse; import software.amazon.awssdk.services.rekognition.model.ProjectPolicy; import software.amazon.awssdk.services.rekognition.model.RekognitionException; public class ListProjectPolicies { public static final Logger logger = Logger.getLogger(ListProjectPolicies.class.getName()); public static void listMyProjectPolicies(RekognitionClient rekClient, String projectArn) { try { logger.log(Level.INFO, "Listing project policies for project: {0}", projectArn); // List the project policies. Boolean finished = false; String nextToken = null; while (Boolean.FALSE.equals(finished)) { ListProjectPoliciesRequest listProjectPoliciesRequest = ListProjectPoliciesRequest.builder() .maxResults(5) .projectArn(projectArn) .nextToken(nextToken) .build(); ListProjectPoliciesResponse response = rekClient.listProjectPolicies(listProjectPoliciesRequest); for (ProjectPolicy projectPolicy : response.projectPolicies()) { System.out.println(String.format("Name: %s", projectPolicy.policyName())); System.out.println(String.format("Revision ID: %s\n", projectPolicy.policyRevisionId())); } nextToken = response.nextToken(); if (nextToken == null) { finished = true; } } logger.log(Level.INFO, "Finished listing project policies for project: {0}", projectArn); } 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> \n\n" + "Where:\n" + " project_arn - The ARN of the project with the project policies that you want to list.\n\n"; ; if (args.length != 1) { System.out.println(USAGE); System.exit(1); } String projectArn = args[0]; try { RekognitionClient rekClient = RekognitionClient.builder() .credentialsProvider(ProfileCredentialsProvider.create("custom-labels-access")) .region(Region.US_WEST_2) .build(); // List the project policies. listMyProjectPolicies(rekClient, projectArn); rekClient.close(); } catch (RekognitionException rekError) { logger.log(Level.SEVERE, "Rekognition client error: {0}", rekError.getMessage()); System.exit(1); } } }