使用BOTO3客户端更新ECR权限时出错

发布于 2025-01-31 06:16:09 字数 2063 浏览 4 评论 0原文

我们正在尝试使用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更新以及如何修复它们时,该问题是什么问题?

We are trying to update the permission of an ECR repository using boto3 sdk.

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)

We are getting the following error.

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'

When we give the same policy using aws cli command, we are able to update the policy successfully.

aws ecr set-repository-policy --repository-name repo_name --policy-text file://policy.json

What is the issue when we update using boto3 sdk and how to fix them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残月升风 2025-02-07 06:16:09

我应该使用set_repository_policy来设置存储库策略。

import json
conv_policy_text = json.dumps(policy_json)
client.set_repository_policy(repositoryName=CURRENT_ECR_NAME, policyText=conv_policy_text)

I should have used set_repository_policy function for setting repository policy.

import json
conv_policy_text = json.dumps(policy_json)
client.set_repository_policy(repositoryName=CURRENT_ECR_NAME, policyText=conv_policy_text)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文