# 调用 PutBucketPolicy 操作时发生错误 (MalformedPolicy)：策略中的主体无效

- **ID:** `aws/s3-bucket-policy-invalid-principal`
- **领域:** aws
- **类别:** config_error
- **错误码:** `MalformedPolicy`
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

S3 存储桶策略中的 Principal 元素引用了一个不存在的 IAM ARN，或使用了不支持的格式（例如，使用 'AWS: *' 而不是 'AWS: *' 或特定 ARN）。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| AWS CLI 2.15.0 | active | — | — |
| AWS SDK for Python 1.34.0 | active | — | — |
| AWS SDK for JavaScript 3.600.0 | active | — | — |

## 解决方案

1. ```
   使用 AWS CLI 测试策略：`aws s3api put-bucket-policy --bucket my-bucket --policy file://policy.json` 并检查错误消息中无效的主体 ARN。然后将其更正为有效的 IAM 用户/角色 ARN（例如 'arn:aws:iam::123456789012:user/username'）。
   ```
2. ```
   如果使用服务主体，请确保格式为 'Service': 's3.amazonaws.com'（或其他服务）而不是 ARN。例如：`"Principal": { "Service": "cloudfront.amazonaws.com" }`。
   ```
3. ```
   在应用策略之前使用 AWS Policy Simulator 进行验证：https://policysim.aws.amazon.com/
   ```

## 无效尝试

- **** — Adding a wildcard principal like 'Principal': 'AWS: *' with a typo (e.g., extra space) will still fail; the exact format 'AWS: *' is required. (65% 失败率)
- **** — Using a principal ARN that is in a different AWS account without proper cross-account trust setup will cause this error. (50% 失败率)
- **** — Assuming the error is a syntax issue in the Effect or Action field, while the real problem is the Principal format. (40% 失败率)
