使用 boto3 客户端更新 ECR 权限时出错
Error while updating ECR permission using boto3 client
我们正在尝试使用 boto3 sdk 更新 ECR 存储库的权限。
import json
import boto3
access_key = "*******"
secret_access = "*******"
ecr_repo_name = 'repo-name'
client = boto3.client('ecr', region_name="eu-west-1",
aws_access_key_id=access_key,
aws_secret_access_key=secret_access)
single_template = {
"Version": "2008-10-17",
"Statement": [
{
"Sid": "ECR_cross_account_access",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::<account_id>:root"
},
"Action": [
"ecr:BatchCheckLayerAvailability",
"ecr:BatchGetImage",
"ecr:CompleteLayerUpload",
"ecr:GetDownloadUrlForLayer",
"ecr:InitiateLayerUpload",
"ecr:PutImage",
"ecr:UploadLayerPart"
]
}
]
}
response = client.put_registry_policy(policyText=json.dumps(single_template))
print(response)
我们收到以下错误。
Traceback (most recent call last):
File "miscs/update_ecr_policy.py", line 89, in <module>
response = client.put_registry_policy(policyText=json.dumps(single_template))
File "/home/nandha/projects/venv3/lib/python3.7/site-packages/botocore/client.py", line 401, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/home/nandha/projects/venv3/lib/python3.7/site-packages/botocore/client.py", line 731, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.errorfactory.InvalidParameterException: An error occurred (InvalidParameterException) when calling the PutRegistryPolicy operation: Invalid parameter at 'PolicyText' failed to satisfy constraint: 'Invalid registry policy provided'
当我们使用 aws cli 命令给出相同的策略时,我们能够成功更新策略。
aws ecr set-repository-policy --repository-name repo_name --policy-text file://policy.json
我们使用 boto3 sdk 更新时出现什么问题以及如何解决这些问题?
你应该使用,
response = client.set_repository_policy(repositoryName=ecr_repo_name, policyText=json.dumps(single_template))
我们正在尝试使用 boto3 sdk 更新 ECR 存储库的权限。
import json
import boto3
access_key = "*******"
secret_access = "*******"
ecr_repo_name = 'repo-name'
client = boto3.client('ecr', region_name="eu-west-1",
aws_access_key_id=access_key,
aws_secret_access_key=secret_access)
single_template = {
"Version": "2008-10-17",
"Statement": [
{
"Sid": "ECR_cross_account_access",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::<account_id>:root"
},
"Action": [
"ecr:BatchCheckLayerAvailability",
"ecr:BatchGetImage",
"ecr:CompleteLayerUpload",
"ecr:GetDownloadUrlForLayer",
"ecr:InitiateLayerUpload",
"ecr:PutImage",
"ecr:UploadLayerPart"
]
}
]
}
response = client.put_registry_policy(policyText=json.dumps(single_template))
print(response)
我们收到以下错误。
Traceback (most recent call last):
File "miscs/update_ecr_policy.py", line 89, in <module>
response = client.put_registry_policy(policyText=json.dumps(single_template))
File "/home/nandha/projects/venv3/lib/python3.7/site-packages/botocore/client.py", line 401, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/home/nandha/projects/venv3/lib/python3.7/site-packages/botocore/client.py", line 731, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.errorfactory.InvalidParameterException: An error occurred (InvalidParameterException) when calling the PutRegistryPolicy operation: Invalid parameter at 'PolicyText' failed to satisfy constraint: 'Invalid registry policy provided'
当我们使用 aws cli 命令给出相同的策略时,我们能够成功更新策略。
aws ecr set-repository-policy --repository-name repo_name --policy-text file://policy.json
我们使用 boto3 sdk 更新时出现什么问题以及如何解决这些问题?
你应该使用,
response = client.set_repository_policy(repositoryName=ecr_repo_name, policyText=json.dumps(single_template))