尝试授予对 S3 存储桶的云端访问权限,但得到“策略无效操作”

Yang Liu

我正在尝试授予对存储我的网站的 S3 存储桶的云端访问权限。

有我正在关注的代码:https ://github.com/JoshM1994/cdk-static-website/blob/main/packages/infrastructure/lib/cdk-static-website-stack.ts

这是我的代码:

// Amazon S3 bucket to store  website
const websiteBucket = new S3.Bucket(this, "eCommerceWebsite", {
  bucketName: `${props.websiteDomain}-${props.env.account}-${props.env.region}`,
  websiteIndexDocument: "index.html",
  websiteErrorDocument: "error.html",
  removalPolicy: CDK.RemovalPolicy.DESTROY,
  // autoDeleteObjects: true,

  accessControl: S3.BucketAccessControl.PRIVATE,
  encryption: S3.BucketEncryption.S3_MANAGED,
  publicReadAccess: false,
  blockPublicAccess: S3.BlockPublicAccess.BLOCK_ALL,
});

const hostedZone = Route53.HostedZone.fromLookup(this, "HostedZoneId", {
  domainName: props.websiteDomain,
});

const certificateManagerCertificate = new ACM.Certificate(
  this,
  "CertificateManagerCertificate",
  {
    domainName: props.websiteDomain,
    validation: ACM.CertificateValidation.fromDns(hostedZone),
  }
);

// Create a special CloudFront user called an origin access identity (OAI)
// and associate it with the CloudFront distribution.

const cloudFrontOAI = new CloudFront.OriginAccessIdentity(
  this,
  "eCommerceWebsiteOriginAccessIdentityID"
);

const cloudfrontUserAccessPolicy = new IAM.PolicyStatement();
cloudfrontUserAccessPolicy.addActions("s3:GetBucket");
cloudfrontUserAccessPolicy.addPrincipals(cloudFrontOAI.grantPrincipal);
cloudfrontUserAccessPolicy.addResources(websiteBucket.arnForObjects("*"));
websiteBucket.addToResourcePolicy(cloudfrontUserAccessPolicy);

//websiteBucket.grantRead(cloudFrontOAI.grantPrincipal);

const cloudFrontDistribution = new CloudFront.Distribution(
  this,
  "CloudFrontDistribution",
  {
    domainNames: [props.websiteDomain],
    defaultBehavior: {
      origin: new CloudFrontOrigins.S3Origin(websiteBucket, {
        // CloudFront can use the OAI to access the files in the S3 bucket and serve them to users.
        // Users can’t use a direct URL to the S3 bucket to access a file there.
        originAccessIdentity: cloudFrontOAI,
      }),
      compress: true,
      allowedMethods: CloudFront.AllowedMethods.ALLOW_GET_HEAD,
      cachedMethods: CloudFront.CachedMethods.CACHE_GET_HEAD,
      viewerProtocolPolicy: CloudFront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
      cachePolicy: CloudFront.CachePolicy.CACHING_OPTIMIZED,
    },
    errorResponses: [
      {
        httpStatus: 403,
        responsePagePath: "/index.html",
        responseHttpStatus: 200,
        ttl: CDK.Duration.minutes(0),
      },
      {
        httpStatus: 404,
        responsePagePath: "/index.html",
        responseHttpStatus: 200,
        ttl: CDK.Duration.minutes(0),
      },
    ],
    priceClass: CloudFront.PriceClass.PRICE_CLASS_ALL,
    enabled: true,
    certificate: certificateManagerCertificate,
    minimumProtocolVersion: CloudFront.SecurityPolicyProtocol.TLS_V1_2_2021,
    httpVersion: CloudFront.HttpVersion.HTTP2,
    defaultRootObject: "index.html",
    enableIpv6: true,
  }
);

但是,尝试使用 CDK 部署时出现错误。错误是:

The following resource(s) failed to update: [eCommerceWebsitePolicy92E398F1].

Policy has invalid action (Service: Amazon S3; Status Code: 400; Error Code: MalformedPolicy; Request ID: PHX7S4KTFBYQS57H; S3 Extended Request ID: fAXj+BqXjzDtyO3nqcJ+G9ZILWMrftZYRocZ/q1othOw7xdG7J1pTN5MU2rNJi978+sbQInZzN8=; Proxy: null)

这是生成的云层:

"eCommerceWebsitePolicy92E398F1": {
   "Type": "AWS::S3::BucketPolicy",
   "Properties": {
    "Bucket": {
     "Ref": "eCommerceWebsite1384DBEE"
    },
    "PolicyDocument": {
     "Statement": [
      {
       "Action": "s3:GetBucket",
       "Effect": "Allow",
       "Principal": {
        "CanonicalUser": {
         "Fn::GetAtt": [
          "eCommerceWebsiteOriginAccessIdentityIDF6581C41",
          "S3CanonicalUserId"
         ]
        }
       },
       "Resource": {
        "Fn::Join": [
         "",
         [
          {
           "Fn::GetAtt": [
            "eCommerceWebsite1384DBEE",
            "Arn"
           ]
          },
          "/*"
         ]
        ]
       }
      }
     ],
     "Version": "2012-10-17"
    }
   },

这里出了什么问题?

马尔钦

没有动作被调用s3:GetBucket可能你想要s3:GetObjects3:ListBucket

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

AWS:无法通过s3存储桶策略授予s3访问权限

S3:如何授予对多个存储桶的访问权限?

如何使用简单的s3存储桶策略授予对加密存储桶的跨帐户访问权限?

如何使用IAM策略AWS仅向S3存储桶的根帐户用户授予访问权限?

仅授予特定用户对S3存储桶的访问权限,无公共访问权限

为什么即使我在存储桶策略中授予用户权限,用户也无法访问我们的 S3 存储桶?

跨帐户角色授予S3存储桶访问权限-权限被拒绝

尝试使用aws-cdk授予s3 Log Delivery Group的完全权限

授予访问权限以读取Amazon S3存储桶中的子目录

授予对 S3 存储桶中所有内容的访问权限

我应该授予什么 aws 权限来访问其他 s3 存储桶

如何授予AWS用户访问s3存储桶托管的网站的权限?

您希望何时授予对 s3 存储桶的公共读取访问权限

是否可以使用存储桶策略授予对AWS S3中另一个账户拥有的对象的访问权限?

向没有AWS账户的用户授予对AWS S3存储桶/文件夹的访问权限

AWS S3 试图了解授予策略的权限

尝试上传到具有`PutObject`权限的S3存储桶时,为什么会得到`AccessDenied`?

如何以编程方式授予s3存储桶调用lambda的权限?

如何授予Lambda将文件上传到terraform中的s3存储桶的权限?

根据角色名称授予对S3资源的访问权限

S3向用户授予对象访问权限

当我尝试在 s3 权限选项卡中添加读取访问策略时出现无效的 json 错误

S3存储桶AWS您无法授予公共访问权限,因为已为此帐户启用了阻止公共访问设置

AWS:S3 存储桶 AWS 您无法授予公共访问权限,因为此帐户已启用阻止公共访问设置

如何使用boto3为IAM用户授予S3存储桶的完全权限?

如何使用经过身份验证的经过身份验证的用户授予对非公共Amazon S3存储桶文件夹的访问权限

授予 aws s3 对 fargate 集群的读取权限

尝试设置Amazon的S3存储桶:403禁止的错误和设置权限

为什么我的lambda函数尝试访问S3存储桶时会被拒绝访问?