Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.
Melampirkan kebijakan proyek (SDK)
Anda melampirkan kebijakan proyek ke proyek Label Kustom Amazon Rekognition dengan memanggil PutProjectpolicyoperasi.
Lampirkan beberapa kebijakan proyek ke proyek dengan memanggil setiapPutProjectPolicy
kebijakan proyek yang ingin Anda tambahkan. Anda dapat melampirkan hingga lima kebijakan proyek proyek ke proyek. Jika Anda perlu melampirkan kebijakan proyek lainnya, Anda dapat meminta peningkatan batas.
Saat pertama kali melampirkan kebijakan proyek unik ke proyek, jangan tentukan ID revisi dalam parameterPolicyRevisionId
masukan. Respons dariPutProjectPolicy
adalah ID revisi untuk kebijakan proyek yang dibuat Label Kustom Amazon Rekognition untuk Anda. Anda dapat menggunakan ID revisi untuk memperbarui atau menghapus revisi terbaru dari kebijakan proyek. Label Kustom Amazon Rekognition hanya menyimpan revisi terbaru dari kebijakan proyek. Jika Anda mencoba memperbarui atau menghapus revisi sebelumnya dari kebijakan proyek, Anda mendapatkanInvalidPolicyRevisionIdException
kesalahan.
Untuk memperbarui kebijakan proyek yang ada, tentukan ID revisi kebijakan proyek dalam parameterPolicyRevisionId
masukan. Anda bisa mendapatkan ID revisi untuk kebijakan proyek dalam proyek dengan menelepon ListProjectPolicies.
Setelah melampirkan kebijakan proyek ke proyek sumber, Anda dapat menyalin model dari proyek sumber ke proyek tujuan. Untuk informasi selengkapnya, lihat Menyalin model (SDK).
Untuk menghapus kebijakan proyek dari proyek, hubungi DeleteProjectPolicy. Untuk mendapatkan daftar kebijakan proyek yang dilampirkan ke proyek, hubungi ListProjectPolicies.
Untuk melampirkan kebijakan proyek ke proyek (SDK)
-
Jika Anda belum melakukannya, instal dan mengonfigurasiAWS SDK.AWS CLI Untuk informasi selengkapnya, lihat Langkah 4: Mengatur AWS CLI dan AWS SDKs.
-
Buat dokumen kebijakan proyek.
-
Gunakan kode berikut untuk melampirkan kebijakan proyek ke proyek, diAWS akun kepercayaan, yang berisi versi model yang ingin Anda salin. Untuk mendapatkan proyek ARN, hubungi DescribeProjects. Untuk mendapatkan panggilan ARN versi model DescribeProjectVersions.
- AWS CLI
-
Ubah nilai berikut:
-
project-arn
ke ARN proyek sumber diAWS akun kepercayaan yang berisi versi model yang ingin Anda salin.
-
policy-name
ke nama kebijakan yang Anda pilih.
-
principal
Untuk prinsipal yang ingin Anda hapus akses ke versi model yang Anda tentukanModel version ARN
.
-
project-version-arn
ke versi model ARN ARN yang ingin Anda salin.
Jika Anda ingin memperbarui kebijakan proyek yang ada, tentukanpolicy-revision-id
parameter dan berikan ID revisi kebijakan proyek yang diinginkan.
aws rekognition put-project-policy \
--project-arn project-arn
\
--policy-name policy-name
\
--policy-document '{ "Version":"2012-10-17", "Statement":[{ "Effect":"ALLOW or DENY
", "Principal":{ "AWS":"principal
" }, "Action":"rekognition:CopyProjectVersion", "Resource":"project-version-arn
" }]}' \
--profile custom-labels-access
- Python
-
Gunakan kode berikut. Menyediakan parameter baris perintah berikut:
-
project_arn
- ARN proyek sumber yang ingin Anda lampirkan kebijakan proyek.
-
policy_name
- Nama kebijakan yang Anda pilih.
-
project_policy
- File yang berisi dokumen kebijakan proyek,.
-
policy_revision_id
- (Opsional). Jika Anda ingin memperbarui revisi kebijakan proyek yang ada, tentukan ID revisi kebijakan proyek.
# 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 attach a project policy to an Amazon Rekognition Custom Labels project.
"""
import boto3
import argparse
import logging
import json
from botocore.exceptions import ClientError
logger = logging.getLogger(__name__)
def put_project_policy(rek_client, project_arn, policy_name, policy_document_file, policy_revision_id=None):
"""
Attaches a project policy to an Amazon Rekognition Custom Labels project.
:param rek_client: The Amazon Rekognition Custom Labels Boto3 client.
:param policy_name: A name for the project policy.
:param project_arn: The Amazon Resource Name (ARN) of the source project
that you want to attach the project policy to.
:param policy_document_file: The JSON project policy document to
attach to the source project.
:param policy_revision_id: (Optional) The revision of an existing policy to update.
Pass None to attach new policy.
:return The revision ID for the project policy.
"""
try:
policy_document_json = ""
response = None
with open(policy_document_file, 'r') as policy_document:
policy_document_json = json.dumps(json.load(policy_document))
logger.info(
"Attaching %s project_policy to project %s.",
policy_name, project_arn)
if policy_revision_id is None:
response = rek_client.put_project_policy(ProjectArn=project_arn,
PolicyName=policy_name,
PolicyDocument=policy_document_json)
else:
response = rek_client.put_project_policy(ProjectArn=project_arn,
PolicyName=policy_name,
PolicyDocument=policy_document_json,
PolicyRevisionId=policy_revision_id)
new_revision_id = response['PolicyRevisionId']
logger.info(
"Finished creating project policy %s. Revision ID: %s",
policy_name, new_revision_id)
return new_revision_id
except ClientError as err:
logger.exception(
"Couldn't attach %s project policy to project %s: %s }",
policy_name, 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 (ARN) of the project "
"that you want to attach the project policy to."
)
parser.add_argument(
"policy_name", help="A name for the project policy."
)
parser.add_argument(
"project_policy", help="The file containing the project policy JSON"
)
parser.add_argument(
"--policy_revision_id", help="The revision of an existing policy to update. "
"If you don't supply a value, a new project policy is created.",
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()
print(f"Attaching policy to {args.project_arn}")
session = boto3.Session(profile_name='custom-labels-access')
rekognition_client = session.client("rekognition")
# Attach a new policy or update an existing policy.
response = put_project_policy(rekognition_client,
args.project_arn,
args.policy_name,
args.project_policy,
args.policy_revision_id)
print(
f"project policy {args.policy_name} attached to project {args.project_arn}")
print(f"Revision ID: {response}")
except ClientError as err:
print("Problem attaching project policy: %s", err)
if __name__ == "__main__":
main()
- Java V2
-
Gunakan kode berikut. Menyediakan parameter baris perintah berikut:
-
project_arn
- ARN proyek sumber yang ingin Anda lampirkan kebijakan proyek.
-
project_policy_name
- Nama kebijakan yang Anda pilih.
-
project_policy_document
- File yang berisi dokumen kebijakan proyek.
-
project_policy_revision_id
- (Opsional). Jika Anda ingin memperbarui revisi kebijakan proyek yang ada, tentukan ID revisi kebijakan proyek.
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package com.example.rekognition;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
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.PutProjectPolicyRequest;
import software.amazon.awssdk.services.rekognition.model.RekognitionException;
public class PutProjectPolicy {
public static final Logger logger = Logger.getLogger(PutProjectPolicy.class.getName());
public static void putMyProjectPolicy(RekognitionClient rekClient, String projectArn, String projectPolicyName,
String projectPolicyFileName, String projectPolicyRevisionId) throws IOException {
try {
Path filePath = Path.of(projectPolicyFileName);
String policyDocument = Files.readString(filePath);
String[] logArguments = new String[] { projectPolicyFileName, projectPolicyName };
PutProjectPolicyRequest putProjectPolicyRequest = null;
logger.log(Level.INFO, "Attaching Project policy: {0} to project: {1}", logArguments);
// Attach the project policy.
if (projectPolicyRevisionId == null) {
putProjectPolicyRequest = PutProjectPolicyRequest.builder().projectArn(projectArn)
.policyName(projectPolicyName).policyDocument(policyDocument).build();
} else {
putProjectPolicyRequest = PutProjectPolicyRequest.builder().projectArn(projectArn)
.policyName(projectPolicyName).policyRevisionId(projectPolicyRevisionId)
.policyDocument(policyDocument)
.build();
}
rekClient.putProjectPolicy(putProjectPolicyRequest);
logger.log(Level.INFO, "Attached Project policy: {0} to project: {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> <policy_document> <project_policy_revision_id>\n\n" + "Where:\n"
+ " project_arn - The ARN of the project that you want to attach the project policy to.\n\n"
+ " project_policy_name - A name for the project policy.\n\n"
+ " project_policy_document - The file name of the project policy.\n\n"
+ " project_policy_revision_id - (Optional) The revision ID of the project policy that you want to update.\n\n";
if (args.length < 3 || args.length > 4) {
System.out.println(USAGE);
System.exit(1);
}
String projectArn = args[0];
String projectPolicyName = args[1];
String projectPolicyDocument = args[2];
String projectPolicyRevisionId = null;
if (args.length == 4) {
projectPolicyRevisionId = args[3];
}
try {
RekognitionClient rekClient = RekognitionClient.builder()
.credentialsProvider(ProfileCredentialsProvider.create("custom-labels-access"))
.region(Region.US_WEST_2)
.build();
// Attach the project policy.
putMyProjectPolicy(rekClient, projectArn, projectPolicyName, projectPolicyDocument,
projectPolicyRevisionId);
System.out.println(
String.format("project policy %s: attached to project: %s", projectPolicyName, projectArn));
rekClient.close();
} catch (RekognitionException rekError) {
logger.log(Level.SEVERE, "Rekognition client error: {0}", rekError.getMessage());
System.exit(1);
}
catch (IOException intError) {
logger.log(Level.SEVERE, "Exception while reading policy document: {0}", intError.getMessage());
System.exit(1);
}
}
}
-
Salin versi model dengan mengikuti petunjuk diMenyalin model (SDK).