AWS CLI S3 调用 HeadObject 操作时发生客户端错误 (403):禁止访问
AWS CLI S3 A client error (403) occurred when calling the HeadObject operation: Forbidden
我正在尝试设置一个 Amazon Linux AMI (ami-f0091d91) 并有一个脚本 运行 是一个从 S3 存储桶复制的复制命令。
aws --debug s3 cp s3://aws-codedeploy-us-west-2/latest/codedeploy-agent.noarch.rpm .
此脚本在我的本地机器上运行良好,但在 Amazon Image 上失败并出现以下错误:
2016-03-22 01:07:47,110 - MainThread - botocore.auth - DEBUG - StringToSign:
HEAD
Tue, 22 Mar 2016 01:07:47 GMT
x-amz-security-token:AQoDYXdzEPr//////////wEa4ANtcDKVDItVq8Z5OKms8wpQ3MS4dxLtxVq6Om1aWDhLmZhL2zdqiasNBV4nQtVqwyPsRVyxl1Urq1BBCnZzDdl4blSklm6dvu+3efjwjhudk7AKaCEHWlTd/VR3cksSNMFTcI9aIUUwzGW8lD9y8MVpKzDkpxzNB7ZJbr9HQNu8uF/st0f45+ABLm8X4FsBPCl2I3wKqvwV/s2VioP/tJf7RGQK3FC079oxw3mOid5sEi28o0Qp4h/Vy9xEHQ28YQNHXOBafHi0vt7vZpOtOfCJBzXvKbk4zRXbLMamnWVe3V0dArncbNEgL1aAi1ooSQ8+Xps8ufFnqDp7HsquAj50p459XnPedv90uFFd6YnwiVkng9nNTAF+2Jo73+eKTt955Us25Chxvk72nAQsAZlt6NpfR+fF/Qs7jjMGSF6ucjkKbm0x5aCqCw6YknsoE1Rtn8Qz9tFxTmUzyCTNd7uRaxbswm7oHOdsM/Q69otjzqSIztlwgUh2M53LzgChQYx5RjYlrjcyAolRguJjpSq3LwZ5NEacm/W17bDOdaZL3y1977rSJrCxb7lmnHCOER5W0tsF9+XUGW1LMX69EWgFYdn5QNqFk6mcJsZWrR9dkehaQwjLPcv/29QcM+b5u/0goazCtwU=
/aws-codedeploy-us-west-2/latest/codedeploy-agent.noarch.rpm
2016-03-22 01:07:47,111 - MainThread - botocore.endpoint - DEBUG - Sending http request: <PreparedRequest [HEAD]>
2016-03-22 01:07:47,111 - MainThread - botocore.vendored.requests.packages.urllib3.connectionpool - INFO - Starting new HTTPS connection (1): aws-codedeploy-us-west-2.s3.amazonaws.com
2016-03-22 01:07:47,151 - MainThread - botocore.vendored.requests.packages.urllib3.connectionpool - DEBUG - "HEAD /latest/codedeploy-agent.noarch.rpm HTTP/1.1" 403 0
2016-03-22 01:07:47,151 - MainThread - botocore.parsers - DEBUG - Response headers: {'x-amz-id-2': '0mRvGge9ugu+KKyDmROm4jcTa1hAnA5Ax8vUlkKZXoJ//HVJAKxbpFHvOGaqiECa4sgon2F1kXw=', 'server': 'AmazonS3', 'transfer-encoding': 'chunked', 'x-amz-request-id': '6204CD88E880E5DD', 'date': 'Tue, 22 Mar 2016 01:07:46 GMT', 'content-type': 'application/xml'}
2016-03-22 01:07:47,152 - MainThread - botocore.parsers - DEBUG - Response body:
2016-03-22 01:07:47,152 - MainThread - botocore.hooks - DEBUG - Event needs-retry.s3.HeadObject: calling handler <botocore.retryhandler.RetryHandler object at 0x7f421075bcd0>
2016-03-22 01:07:47,152 - MainThread - botocore.retryhandler - DEBUG - No retry needed.
2016-03-22 01:07:47,152 - MainThread - botocore.hooks - DEBUG - Event after-call.s3.HeadObject: calling handler <function enhance_error_msg at 0x7f4211085758>
2016-03-22 01:07:47,152 - MainThread - botocore.hooks - DEBUG - Event after-call.s3.HeadObject: calling handler <awscli.errorhandler.ErrorHandler object at 0x7f421100cc90>
2016-03-22 01:07:47,152 - MainThread - awscli.errorhandler - DEBUG - HTTP Response Code: 403
2016-03-22 01:07:47,152 - MainThread - awscli.customizations.s3.s3handler - DEBUG - Exception caught during task execution: A client error (403) occurred when calling the HeadObject operation: Forbidden
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/awscli/customizations/s3/s3handler.py", line 100, in call
total_files, total_parts = self._enqueue_tasks(files)
File "/usr/local/lib/python2.7/site-packages/awscli/customizations/s3/s3handler.py", line 178, in _enqueue_tasks
for filename in files:
File "/usr/local/lib/python2.7/site-packages/awscli/customizations/s3/fileinfobuilder.py", line 31, in call
for file_base in files:
File "/usr/local/lib/python2.7/site-packages/awscli/customizations/s3/filegenerator.py", line 142, in call
for src_path, extra_information in file_iterator:
File "/usr/local/lib/python2.7/site-packages/awscli/customizations/s3/filegenerator.py", line 314, in list_objects
yield self._list_single_object(s3_path)
File "/usr/local/lib/python2.7/site-packages/awscli/customizations/s3/filegenerator.py", line 343, in _list_single_object
response = self._client.head_object(**params)
File "/usr/local/lib/python2.7/site-packages/botocore/client.py", line 228, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/usr/local/lib/python2.7/site-packages/botocore/client.py", line 488, in _make_api_call
model=operation_model, context=request_context
File "/usr/local/lib/python2.7/site-packages/botocore/hooks.py", line 226, in emit
return self._emit(event_name, kwargs)
File "/usr/local/lib/python2.7/site-packages/botocore/hooks.py", line 209, in _emit
response = handler(**kwargs)
File "/usr/local/lib/python2.7/site-packages/awscli/errorhandler.py", line 70, in __call__
http_status_code=http_response.status_code)
ClientError: A client error (403) occurred when calling the HeadObject operation: Forbidden
2016-03-22 01:07:47,153 - Thread-1 - awscli.customizations.s3.executor - DEBUG - Received print task: PrintTask(message='A client error (403) occurred when calling the HeadObject operation: Forbidden', error=True, total_parts=None, warning=None)
A client error (403) occurred when calling the HeadObject operation: Forbidden
然而,当我 运行 它带有 --no-sign-request
选项时,它完美地工作:
aws --debug --no-sign-request s3 cp s3://aws-codedeploy-us-west-2/latest/codedeploy-agent.noarch.rpm .
谁能解释一下这是怎么回事?
我的 aws cli 复制命令 aws s3 cp s3://bucket/file file
收到错误 A client error (403) occurred when calling the HeadObject operation: Forbidden
。我使用的是 IAM 角色,它使用 Inline Policy
具有完整的 S3 访问权限。
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": "*"
}
]
}
如果我改为从 Managed Policies
授予它完整的 S3 访问权限,则该命令有效。我认为这一定是亚马逊的一个错误,因为这两种情况的政策完全相同。
我想通了。我在创建 EC2 实例的云形成模板中出错。因此,尝试访问上述代码部署存储桶的 EC2 实例位于不同的区域(不是 us-west-2)。似乎存储桶(由亚马逊拥有)的访问策略只允许从它们所属的区域进行访问。
当我修复模板中的错误(参数映射错误)时,错误消失了
其中一个原因可能是您尝试访问需要 V4 签名的区域的存储桶。尝试明确提供区域,如 --region cn-north-1
我遇到过这个问题,在命令中添加 --recursive
会有所帮助。
在这一点上它没有多大意义,因为你(像我一样)只是想复制一个文件,但它确实有用!
在我的案例中,问题出在用户访问策略中的 Resource
语句。
首先我们有 "Resource": "arn:aws:s3:::BUCKET_NAME"
,
但是为了访问存储桶中的对象,您需要在末尾添加 /*
:
"Resource": "arn:aws:s3:::BUCKET_NAME/*"
Bucket access permissions specify which users are allowed access to the objects in a bucket and which types of access they have. Object access permissions specify which users are allowed access to the object and which types of access they have. For example, one user might have only read permission, while another might have read and write permissions.
自己尝试解决这个问题,发现没有HeadBucket权限。看起来好像有,因为那是错误消息告诉你的,但实际上 HEAD
操作需要 ListBucket
权限。
我还发现我的 IAM 策略和存储桶策略存在冲突。请务必检查两者。
就我而言,我在尝试获取 S3 存储桶文件夹上的对象时遇到此错误。但是在那个文件夹中,我的对象不在这里(我放错了文件夹),所以 S3 发送了这条消息。希望对你也有帮助。
我因配置错误的测试事件而收到此错误。我更改了源存储桶 ARN 但忘记编辑默认 S3 存储桶名称。
即确保在测试事件的存储桶部分中正确设置了 ARN 和存储桶名称:
"bucket": {
"arn": "arn:aws:s3:::your_bucket_name",
"name": "your_bucket_name",
"ownerIdentity": {
"principalId": "EXAMPLE"
}
由于我的 EC2 实例的时钟不同步,我收到了这条错误消息。
我能够使用以下方法修复 Ubuntu:
sudo ntpdate ntp.ubuntu.com
sudo apt-get install ntp
我在 GET 请求工作时收到 HEAD 请求的 403。原来是s3权限里的CORS config。我必须添加 HEAD
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>HEAD</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>GET</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
我也遇到过这种行为。就我而言,我发现如果 IAM 策略无权读取对象 (s3:GetObject
),则会引发相同的错误。
我同意你的看法,即 aws 控制台和 cli 引发的错误没有得到很好的解释,可能会造成混淆。
我也遇到过这种情况
我有一个存储桶,其策略使用 AWS4-HMAC-SHA256。原来我的 awscli 没有更新到最新版本。我的是 aws-cli/1.10.8。升级解决问题
pip install awscli --upgrade --user
https://docs.aws.amazon.com/cli/latest/userguide/installing.html
如果 运行 在 credential/role 不清楚的环境中,请确保包含 --profile=yourprofile
标志,以便 cli 知道要使用的凭据。例如:
aws s3 cp s3://yourbucket destination.txt --profile=yourprofile
将成功,而以下产生 HeadObject 错误
aws s3 cp s3://yourbucket destination.txt
您的 config
和 credentials
文件中的配置文件设置参考条目。
关于跨账户S3访问
IAM 用户策略不会覆盖为外部账户中的存储桶定义的策略。
s3:GetObject 必须允许 accountA/user 以及 accountB/bucket
我通过正确设置系统时间解决了这个问题。
确保 aws 存储桶区域正确并且您的系统时间与 aws 区域时间匹配
403 - 表示我知道你是谁,但你无权按照你的要求去做。
就我而言,问题出在策略中 - 在 Visual Editor 中指定策略时我没有选择对象
如果您从另一个 aws 帐户复制文件,请检查您的对象所有者。
在我的例子中,我从另一个没有 acl 的 aws 帐户复制文件,所以文件的所有者是另一个 aws 帐户,这意味着该文件属于原始帐户。
要修复它,请使用 acl 复制或同步 s3 文件,示例:
aws s3 cp --acl bucket-owner-full-control s3://bucket1/key s3://bucket2/key
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowAllS3ActionsInUserFolder",
"Effect": "Allow",
"Action": [
"s3:*"
],
"Resource": [
"arn:aws:s3:::your_bucket_name",
"arn:aws:s3:::your_bucket_name/*"
]
}
]
}
将 "arn:aws:s3:::your_bucket_name"
和 "arn:aws:s3:::your_bucket_name/*"
添加到策略配置中解决了我的问题。
当我遇到这个问题时,我发现我的问题是 'Source Account' 中的文件被 'third party' 复制到那里,而所有者不是源帐户。
我不得不使用 --metadata-directive REPLACE
将对象重新复制到同一个存储桶中
详细解释在Amazon Documentation
Permissions
You need the s3:GetObject permission for this operation. For more
information, see Specifying Permissions in a Policy. If the object you
request does not exist, the error Amazon S3 returns depends on whether
you also have the s3:ListBucket permission.
If you have the s3:ListBucket permission on the bucket, Amazon S3 returns an HTTP status code 404 ("no such key") error.
If you don’t have the s3:ListBucket permission, Amazon S3 returns an HTTP status code 403 ("access denied") error.
The following operation is related to HeadObject:
GetObject
来源:https://docs.aws.amazon.com/AmazonS3/latest/API/API_HeadObject.html
我有一个 lambda 函数做同样的事情,从一个桶复制到另一个桶。
lambda 有权使用源存储桶作为触发器。
配置选项卡
但它还需要使用存储桶进行操作的权限。
权限选项卡
如果 s3 不存在,那么您需要编辑 lambda 使用的 Role 并添加它(参见 s3FullAccess)
在 mybucket
中的任何对象上 运行 HeadObject
时对我有用的最小权限:
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::mybucket/*",
"arn:aws:s3:::mybucket"
]
}
放弃对整个 s3(所有操作,所有存储桶)的访问权限,只是为了解锁自己,这是一种糟糕的做法。
以上403错误一般是由于文件没有“读取”权限。在 S3 中读取文件的读取操作是 s3:GetObject
.
{
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": [
"arn:aws:s3:::mybucketname/path/*",
"arn:aws:s3:::mybucketname"
]
}
解决方案 1:IAM 中的新策略(告诉 Role/User 了解 S3)
您可以使用以下内容创建策略(例如 MY_S3_READER
),并将其附加到执行该工作的用户或角色。 (例如 EC2 实例的 IAM 角色)
这是您保单的确切 JSON:(只需替换 mybucketname
和 path
)
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": [
"arn:aws:s3:::mybucketname/path/*",
"arn:aws:s3:::mybucketname"
]
}
]
}
创建此策略。然后,转到 IAM > 角色 > 附加策略并附加它。
解决方案 2:在 S3 中编辑 Buckey 策略(告诉 S3 知道 User/Role)
转到您在 S3 中的存储桶,然后添加以下示例:(替换 mybucketname
和 myip
)
{
"Version": "2012-10-17",
"Id": "SourceIP",
"Statement": [
{
"Sid": "ValidIpAllowRead",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": [
"arn:aws:s3:::mybucketname",
"arn:aws:s3:::mybucketname/*"
],
"Condition": {
"IpAddress": {
"aws:SourceIp": "myip/32"
}
}
}
]
}
如果您想将此读取权限更改为按用户或角色(而不是 IP 地址),请删除 Condition
部分,并将“主体”更改为 "Principal": { "AWS": "<IAM User/Role's ARN>" },
"。
补充说明
通过 aws s3 cp
或 aws s3 ls
手动检查权限以加快调试速度。
权限更改有时需要长达 30 秒才能生效。耐心点。
请注意,要执行“ls
”(例如 aws s3 ls s3://mybucket/mypath
),您需要 s3:ListBucket
访问权限。
重要 通过 cURL
或类似工具(例如 axios
通过 HTTP(S) URL 访问文件在 AJAX 电话上)要求您授予 IP 访问权限,或手动提供适当的 headers,或先 get a signedUrl from the SDK。
也许这会对某人有所帮助。就我而言,我是 运行 一个 CodeBuild 作业,CodeBuild 执行角色拥有对 S3 的完全访问权限。我试图通过 CLI 列出 S3 存储桶中的密钥。但是,我在 aws-cli
Docker 图像中使用 CLI,并根据本文通过环境变量传递凭据:
https://docs.aws.amazon.com/codebuild/latest/userguide/troubleshooting.html#troubleshooting-versions
无论我尝试什么,任何使用 aws s3api ...
的调用都会失败并出现相同的 403 错误。我的解决方案是转换为普通的 s3 CLI 调用(而不是 s3api CLI 调用):
aws s3 ls s3://bucket/key --recursive
此更改有效。使用 aws s3api did not
.
的调用
可能有很多原因,(最愚蠢的一个)
当指定的对象或文件 在该位置不存在 时,AWS S3 抛出 403 错误。
问题出在您为您正在使用的角色授予的警察许可中,
如果您使用的是 AWS Glue,则需要创建具有这些权限的警察,
https://docs.aws.amazon.com/glue/latest/dg/create-sagemaker-notebook-policy.html
这将解决 "(403) occurred when calling the HeadObject operation: Forbidden"
如果有人最终来到这里并试图在 terraform 中实施解决方案,这是我的:
module "s3_files_label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=0.24.1"
namespace = var.client
environment = var.environment
attributes = ["backend-api-logs"]
}
module "s3_files" {
source = "terraform-aws-modules/s3-bucket/aws"
version = "2.7.0"
bucket = module.s3_files_label.id
acl = "private"
cors_rule = jsonencode([
{
allowed_headers = ["*"]
allowed_methods = ["GET", "PUT", "POST"]
allowed_origins = ["https://${var.route53_domain}"]
expose_headers = []
max_age_seconds = 0
}
])
}
module "s3_user" {
source = "cloudposse/iam-system-user/aws"
version = "0.20.2"
namespace = var.client
environment = var.environment
name = "s3-user"
inline_policies_map = {
s3 = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Action": [
"s3:ListBuckets",
"s3:ListBucket",
"s3:HeadObject",
"s3:GetObject"
],
"Resource": [
"${module.s3_files.s3_bucket_arn}",
"${module.s3_files.s3_bucket_arn}/*"
]
},
{
"Sid": "list",
"Effect": "Allow",
"Action": [
"s3:ListAllMyBuckets"
],
"Resource": [
"*"
]
}
]
}
EOF
}
tags = module.config.tags
}
我遇到了这个问题,但解决方案与上述所有不同 - 我试图通过传输加速端点解决它,但它没有在 S3 存储桶上启用。
除了其他答案之外,错误也可能是由于缺少用于S3存储桶的SSE-KMS的KMS密钥的权限。
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"s3:ListBucket",
"s3:GetObject",
"s3:HeadObject"
],
"Resource": [
"arn:aws:s3:::<your_bucket_name>",
"arn:aws:s3:::<your_bucket_name>/*"
],
"Effect": "Allow"
}
]
}
将此策略分配给分配给您的 ec2(或任何试图访问您的 S3 的服务)的 IAM 角色
对我来说,这是因为我启用了 MFA,并为其提供了访问密钥和密钥,但不允许在我使用的 docker 映像中进行令牌身份验证。这也可能导致此错误。这是一个适用范围很广的错误。
我正在尝试设置一个 Amazon Linux AMI (ami-f0091d91) 并有一个脚本 运行 是一个从 S3 存储桶复制的复制命令。
aws --debug s3 cp s3://aws-codedeploy-us-west-2/latest/codedeploy-agent.noarch.rpm .
此脚本在我的本地机器上运行良好,但在 Amazon Image 上失败并出现以下错误:
2016-03-22 01:07:47,110 - MainThread - botocore.auth - DEBUG - StringToSign:
HEAD
Tue, 22 Mar 2016 01:07:47 GMT
x-amz-security-token:AQoDYXdzEPr//////////wEa4ANtcDKVDItVq8Z5OKms8wpQ3MS4dxLtxVq6Om1aWDhLmZhL2zdqiasNBV4nQtVqwyPsRVyxl1Urq1BBCnZzDdl4blSklm6dvu+3efjwjhudk7AKaCEHWlTd/VR3cksSNMFTcI9aIUUwzGW8lD9y8MVpKzDkpxzNB7ZJbr9HQNu8uF/st0f45+ABLm8X4FsBPCl2I3wKqvwV/s2VioP/tJf7RGQK3FC079oxw3mOid5sEi28o0Qp4h/Vy9xEHQ28YQNHXOBafHi0vt7vZpOtOfCJBzXvKbk4zRXbLMamnWVe3V0dArncbNEgL1aAi1ooSQ8+Xps8ufFnqDp7HsquAj50p459XnPedv90uFFd6YnwiVkng9nNTAF+2Jo73+eKTt955Us25Chxvk72nAQsAZlt6NpfR+fF/Qs7jjMGSF6ucjkKbm0x5aCqCw6YknsoE1Rtn8Qz9tFxTmUzyCTNd7uRaxbswm7oHOdsM/Q69otjzqSIztlwgUh2M53LzgChQYx5RjYlrjcyAolRguJjpSq3LwZ5NEacm/W17bDOdaZL3y1977rSJrCxb7lmnHCOER5W0tsF9+XUGW1LMX69EWgFYdn5QNqFk6mcJsZWrR9dkehaQwjLPcv/29QcM+b5u/0goazCtwU=
/aws-codedeploy-us-west-2/latest/codedeploy-agent.noarch.rpm
2016-03-22 01:07:47,111 - MainThread - botocore.endpoint - DEBUG - Sending http request: <PreparedRequest [HEAD]>
2016-03-22 01:07:47,111 - MainThread - botocore.vendored.requests.packages.urllib3.connectionpool - INFO - Starting new HTTPS connection (1): aws-codedeploy-us-west-2.s3.amazonaws.com
2016-03-22 01:07:47,151 - MainThread - botocore.vendored.requests.packages.urllib3.connectionpool - DEBUG - "HEAD /latest/codedeploy-agent.noarch.rpm HTTP/1.1" 403 0
2016-03-22 01:07:47,151 - MainThread - botocore.parsers - DEBUG - Response headers: {'x-amz-id-2': '0mRvGge9ugu+KKyDmROm4jcTa1hAnA5Ax8vUlkKZXoJ//HVJAKxbpFHvOGaqiECa4sgon2F1kXw=', 'server': 'AmazonS3', 'transfer-encoding': 'chunked', 'x-amz-request-id': '6204CD88E880E5DD', 'date': 'Tue, 22 Mar 2016 01:07:46 GMT', 'content-type': 'application/xml'}
2016-03-22 01:07:47,152 - MainThread - botocore.parsers - DEBUG - Response body:
2016-03-22 01:07:47,152 - MainThread - botocore.hooks - DEBUG - Event needs-retry.s3.HeadObject: calling handler <botocore.retryhandler.RetryHandler object at 0x7f421075bcd0>
2016-03-22 01:07:47,152 - MainThread - botocore.retryhandler - DEBUG - No retry needed.
2016-03-22 01:07:47,152 - MainThread - botocore.hooks - DEBUG - Event after-call.s3.HeadObject: calling handler <function enhance_error_msg at 0x7f4211085758>
2016-03-22 01:07:47,152 - MainThread - botocore.hooks - DEBUG - Event after-call.s3.HeadObject: calling handler <awscli.errorhandler.ErrorHandler object at 0x7f421100cc90>
2016-03-22 01:07:47,152 - MainThread - awscli.errorhandler - DEBUG - HTTP Response Code: 403
2016-03-22 01:07:47,152 - MainThread - awscli.customizations.s3.s3handler - DEBUG - Exception caught during task execution: A client error (403) occurred when calling the HeadObject operation: Forbidden
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/awscli/customizations/s3/s3handler.py", line 100, in call
total_files, total_parts = self._enqueue_tasks(files)
File "/usr/local/lib/python2.7/site-packages/awscli/customizations/s3/s3handler.py", line 178, in _enqueue_tasks
for filename in files:
File "/usr/local/lib/python2.7/site-packages/awscli/customizations/s3/fileinfobuilder.py", line 31, in call
for file_base in files:
File "/usr/local/lib/python2.7/site-packages/awscli/customizations/s3/filegenerator.py", line 142, in call
for src_path, extra_information in file_iterator:
File "/usr/local/lib/python2.7/site-packages/awscli/customizations/s3/filegenerator.py", line 314, in list_objects
yield self._list_single_object(s3_path)
File "/usr/local/lib/python2.7/site-packages/awscli/customizations/s3/filegenerator.py", line 343, in _list_single_object
response = self._client.head_object(**params)
File "/usr/local/lib/python2.7/site-packages/botocore/client.py", line 228, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/usr/local/lib/python2.7/site-packages/botocore/client.py", line 488, in _make_api_call
model=operation_model, context=request_context
File "/usr/local/lib/python2.7/site-packages/botocore/hooks.py", line 226, in emit
return self._emit(event_name, kwargs)
File "/usr/local/lib/python2.7/site-packages/botocore/hooks.py", line 209, in _emit
response = handler(**kwargs)
File "/usr/local/lib/python2.7/site-packages/awscli/errorhandler.py", line 70, in __call__
http_status_code=http_response.status_code)
ClientError: A client error (403) occurred when calling the HeadObject operation: Forbidden
2016-03-22 01:07:47,153 - Thread-1 - awscli.customizations.s3.executor - DEBUG - Received print task: PrintTask(message='A client error (403) occurred when calling the HeadObject operation: Forbidden', error=True, total_parts=None, warning=None)
A client error (403) occurred when calling the HeadObject operation: Forbidden
然而,当我 运行 它带有 --no-sign-request
选项时,它完美地工作:
aws --debug --no-sign-request s3 cp s3://aws-codedeploy-us-west-2/latest/codedeploy-agent.noarch.rpm .
谁能解释一下这是怎么回事?
我的 aws cli 复制命令 aws s3 cp s3://bucket/file file
收到错误 A client error (403) occurred when calling the HeadObject operation: Forbidden
。我使用的是 IAM 角色,它使用 Inline Policy
具有完整的 S3 访问权限。
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": "*"
}
]
}
如果我改为从 Managed Policies
授予它完整的 S3 访问权限,则该命令有效。我认为这一定是亚马逊的一个错误,因为这两种情况的政策完全相同。
我想通了。我在创建 EC2 实例的云形成模板中出错。因此,尝试访问上述代码部署存储桶的 EC2 实例位于不同的区域(不是 us-west-2)。似乎存储桶(由亚马逊拥有)的访问策略只允许从它们所属的区域进行访问。 当我修复模板中的错误(参数映射错误)时,错误消失了
其中一个原因可能是您尝试访问需要 V4 签名的区域的存储桶。尝试明确提供区域,如 --region cn-north-1
我遇到过这个问题,在命令中添加 --recursive
会有所帮助。
在这一点上它没有多大意义,因为你(像我一样)只是想复制一个文件,但它确实有用!
在我的案例中,问题出在用户访问策略中的 Resource
语句。
首先我们有 "Resource": "arn:aws:s3:::BUCKET_NAME"
,
但是为了访问存储桶中的对象,您需要在末尾添加 /*
:
"Resource": "arn:aws:s3:::BUCKET_NAME/*"
Bucket access permissions specify which users are allowed access to the objects in a bucket and which types of access they have. Object access permissions specify which users are allowed access to the object and which types of access they have. For example, one user might have only read permission, while another might have read and write permissions.
自己尝试解决这个问题,发现没有HeadBucket权限。看起来好像有,因为那是错误消息告诉你的,但实际上 HEAD
操作需要 ListBucket
权限。
我还发现我的 IAM 策略和存储桶策略存在冲突。请务必检查两者。
就我而言,我在尝试获取 S3 存储桶文件夹上的对象时遇到此错误。但是在那个文件夹中,我的对象不在这里(我放错了文件夹),所以 S3 发送了这条消息。希望对你也有帮助。
我因配置错误的测试事件而收到此错误。我更改了源存储桶 ARN 但忘记编辑默认 S3 存储桶名称。
即确保在测试事件的存储桶部分中正确设置了 ARN 和存储桶名称:
"bucket": {
"arn": "arn:aws:s3:::your_bucket_name",
"name": "your_bucket_name",
"ownerIdentity": {
"principalId": "EXAMPLE"
}
由于我的 EC2 实例的时钟不同步,我收到了这条错误消息。
我能够使用以下方法修复 Ubuntu:
sudo ntpdate ntp.ubuntu.com
sudo apt-get install ntp
我在 GET 请求工作时收到 HEAD 请求的 403。原来是s3权限里的CORS config。我必须添加 HEAD
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>HEAD</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>GET</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
我也遇到过这种行为。就我而言,我发现如果 IAM 策略无权读取对象 (s3:GetObject
),则会引发相同的错误。
我同意你的看法,即 aws 控制台和 cli 引发的错误没有得到很好的解释,可能会造成混淆。
我也遇到过这种情况
我有一个存储桶,其策略使用 AWS4-HMAC-SHA256。原来我的 awscli 没有更新到最新版本。我的是 aws-cli/1.10.8。升级解决问题
pip install awscli --upgrade --user
https://docs.aws.amazon.com/cli/latest/userguide/installing.html
如果 运行 在 credential/role 不清楚的环境中,请确保包含 --profile=yourprofile
标志,以便 cli 知道要使用的凭据。例如:
aws s3 cp s3://yourbucket destination.txt --profile=yourprofile
将成功,而以下产生 HeadObject 错误
aws s3 cp s3://yourbucket destination.txt
您的 config
和 credentials
文件中的配置文件设置参考条目。
关于跨账户S3访问
IAM 用户策略不会覆盖为外部账户中的存储桶定义的策略。
s3:GetObject 必须允许 accountA/user 以及 accountB/bucket
我通过正确设置系统时间解决了这个问题。
确保 aws 存储桶区域正确并且您的系统时间与 aws 区域时间匹配
403 - 表示我知道你是谁,但你无权按照你的要求去做。
就我而言,问题出在策略中 - 在 Visual Editor 中指定策略时我没有选择对象
如果您从另一个 aws 帐户复制文件,请检查您的对象所有者。
在我的例子中,我从另一个没有 acl 的 aws 帐户复制文件,所以文件的所有者是另一个 aws 帐户,这意味着该文件属于原始帐户。
要修复它,请使用 acl 复制或同步 s3 文件,示例:
aws s3 cp --acl bucket-owner-full-control s3://bucket1/key s3://bucket2/key
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowAllS3ActionsInUserFolder",
"Effect": "Allow",
"Action": [
"s3:*"
],
"Resource": [
"arn:aws:s3:::your_bucket_name",
"arn:aws:s3:::your_bucket_name/*"
]
}
]
}
将 "arn:aws:s3:::your_bucket_name"
和 "arn:aws:s3:::your_bucket_name/*"
添加到策略配置中解决了我的问题。
当我遇到这个问题时,我发现我的问题是 'Source Account' 中的文件被 'third party' 复制到那里,而所有者不是源帐户。
我不得不使用 --metadata-directive REPLACE
将对象重新复制到同一个存储桶中详细解释在Amazon Documentation
Permissions
You need the s3:GetObject permission for this operation. For more information, see Specifying Permissions in a Policy. If the object you request does not exist, the error Amazon S3 returns depends on whether you also have the s3:ListBucket permission.
If you have the s3:ListBucket permission on the bucket, Amazon S3 returns an HTTP status code 404 ("no such key") error. If you don’t have the s3:ListBucket permission, Amazon S3 returns an HTTP status code 403 ("access denied") error.
The following operation is related to HeadObject:
GetObject
来源:https://docs.aws.amazon.com/AmazonS3/latest/API/API_HeadObject.html
我有一个 lambda 函数做同样的事情,从一个桶复制到另一个桶。
lambda 有权使用源存储桶作为触发器。
配置选项卡
但它还需要使用存储桶进行操作的权限。
权限选项卡
如果 s3 不存在,那么您需要编辑 lambda 使用的 Role 并添加它(参见 s3FullAccess)
在 mybucket
中的任何对象上 运行 HeadObject
时对我有用的最小权限:
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::mybucket/*",
"arn:aws:s3:::mybucket"
]
}
放弃对整个 s3(所有操作,所有存储桶)的访问权限,只是为了解锁自己,这是一种糟糕的做法。
以上403错误一般是由于文件没有“读取”权限。在 S3 中读取文件的读取操作是 s3:GetObject
.
{
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": [
"arn:aws:s3:::mybucketname/path/*",
"arn:aws:s3:::mybucketname"
]
}
解决方案 1:IAM 中的新策略(告诉 Role/User 了解 S3)
您可以使用以下内容创建策略(例如 MY_S3_READER
),并将其附加到执行该工作的用户或角色。 (例如 EC2 实例的 IAM 角色)
这是您保单的确切 JSON:(只需替换 mybucketname
和 path
)
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": [
"arn:aws:s3:::mybucketname/path/*",
"arn:aws:s3:::mybucketname"
]
}
]
}
创建此策略。然后,转到 IAM > 角色 > 附加策略并附加它。
解决方案 2:在 S3 中编辑 Buckey 策略(告诉 S3 知道 User/Role)
转到您在 S3 中的存储桶,然后添加以下示例:(替换 mybucketname
和 myip
)
{
"Version": "2012-10-17",
"Id": "SourceIP",
"Statement": [
{
"Sid": "ValidIpAllowRead",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": [
"arn:aws:s3:::mybucketname",
"arn:aws:s3:::mybucketname/*"
],
"Condition": {
"IpAddress": {
"aws:SourceIp": "myip/32"
}
}
}
]
}
如果您想将此读取权限更改为按用户或角色(而不是 IP 地址),请删除 Condition
部分,并将“主体”更改为 "Principal": { "AWS": "<IAM User/Role's ARN>" },
"。
补充说明
通过
aws s3 cp
或aws s3 ls
手动检查权限以加快调试速度。权限更改有时需要长达 30 秒才能生效。耐心点。
请注意,要执行“
ls
”(例如aws s3 ls s3://mybucket/mypath
),您需要s3:ListBucket
访问权限。重要 通过
cURL
或类似工具(例如axios
通过 HTTP(S) URL 访问文件在 AJAX 电话上)要求您授予 IP 访问权限,或手动提供适当的 headers,或先 get a signedUrl from the SDK。
也许这会对某人有所帮助。就我而言,我是 运行 一个 CodeBuild 作业,CodeBuild 执行角色拥有对 S3 的完全访问权限。我试图通过 CLI 列出 S3 存储桶中的密钥。但是,我在 aws-cli
Docker 图像中使用 CLI,并根据本文通过环境变量传递凭据:
https://docs.aws.amazon.com/codebuild/latest/userguide/troubleshooting.html#troubleshooting-versions
无论我尝试什么,任何使用 aws s3api ...
的调用都会失败并出现相同的 403 错误。我的解决方案是转换为普通的 s3 CLI 调用(而不是 s3api CLI 调用):
aws s3 ls s3://bucket/key --recursive
此更改有效。使用 aws s3api did not
.
可能有很多原因,(最愚蠢的一个) 当指定的对象或文件 在该位置不存在 时,AWS S3 抛出 403 错误。
问题出在您为您正在使用的角色授予的警察许可中, 如果您使用的是 AWS Glue,则需要创建具有这些权限的警察, https://docs.aws.amazon.com/glue/latest/dg/create-sagemaker-notebook-policy.html
这将解决 "(403) occurred when calling the HeadObject operation: Forbidden"
如果有人最终来到这里并试图在 terraform 中实施解决方案,这是我的:
module "s3_files_label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=0.24.1"
namespace = var.client
environment = var.environment
attributes = ["backend-api-logs"]
}
module "s3_files" {
source = "terraform-aws-modules/s3-bucket/aws"
version = "2.7.0"
bucket = module.s3_files_label.id
acl = "private"
cors_rule = jsonencode([
{
allowed_headers = ["*"]
allowed_methods = ["GET", "PUT", "POST"]
allowed_origins = ["https://${var.route53_domain}"]
expose_headers = []
max_age_seconds = 0
}
])
}
module "s3_user" {
source = "cloudposse/iam-system-user/aws"
version = "0.20.2"
namespace = var.client
environment = var.environment
name = "s3-user"
inline_policies_map = {
s3 = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Action": [
"s3:ListBuckets",
"s3:ListBucket",
"s3:HeadObject",
"s3:GetObject"
],
"Resource": [
"${module.s3_files.s3_bucket_arn}",
"${module.s3_files.s3_bucket_arn}/*"
]
},
{
"Sid": "list",
"Effect": "Allow",
"Action": [
"s3:ListAllMyBuckets"
],
"Resource": [
"*"
]
}
]
}
EOF
}
tags = module.config.tags
}
我遇到了这个问题,但解决方案与上述所有不同 - 我试图通过传输加速端点解决它,但它没有在 S3 存储桶上启用。
除了其他答案之外,错误也可能是由于缺少用于S3存储桶的SSE-KMS的KMS密钥的权限。
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"s3:ListBucket",
"s3:GetObject",
"s3:HeadObject"
],
"Resource": [
"arn:aws:s3:::<your_bucket_name>",
"arn:aws:s3:::<your_bucket_name>/*"
],
"Effect": "Allow"
}
]
}
将此策略分配给分配给您的 ec2(或任何试图访问您的 S3 的服务)的 IAM 角色
对我来说,这是因为我启用了 MFA,并为其提供了访问密钥和密钥,但不允许在我使用的 docker 映像中进行令牌身份验证。这也可能导致此错误。这是一个适用范围很广的错误。