【问题标题】:Hugo multilingual site deploy to aws s3 error "AccessDenied"Hugo 多语言站点部署到 aws s3 错误“AccessDenied”
【发布时间】:2020-02-27 07:50:17
【问题描述】:

我在 AWS S3 上托管一个静态网站,通过 AWS Cloudfront 提供服务。网站是用 Hugo 以多语言模式构建的。因此它从索引页面“/”重定向到默认语言索引页面 url /en 。在此页面上,我收到错误访问被拒绝。当我手动输入 URL /en/index.html 页面加载正常。

我应该如何设置 AWS 存储桶或 Hugo 以正确显示页面?

【问题讨论】:

    标签: amazon-s3 hugo


    【解决方案1】:

    在这篇文章中寻找答案 CloudFront + S3 Website: "The specified key does not exist" when an implicit index document should be displayed 我需要在 Cloudfront 设置中使用存储桶的网站托管端点作为源域名

    【讨论】:

      【解决方案2】:

      一种方法是关注@qwelp 共享的链接,该解决方案指出,如果您使用的是 S3 REST API 端点而不是静态网站托管,则需要将 CloudFront 的来源从“S3”到“自定义”来源。

      解决这个问题的另一种方法是实现一个 lambda 边缘函数,该函数重定向所有用户请求,该请求请求雨果子目录中的文件。引用于AWS Knowledge Center here;

      使用 Lambda@Edge 能够将 CloudFront 与 S3 源访问一起使用 标识并在子目录 URL 上提供默认根对象。

      来自 AWS 知识中心

      为什么 CloudFront 不从 子目录?问题 为什么 Amazon CloudFront 没有返回我的默认值 子文件夹或子目录中的根对象?分辨率 默认 CloudFront 的根对象功能仅支持 您的分发指向的来源。 CloudFront 不返回 子目录中的默认根对象。有关详细信息,请参阅 指定默认根对象。

      如果您的 CloudFront 分配必须返回默认根对象 从子文件夹或子目录中,您可以将 Lambda@Edge 与 你的分布。有关示例配置,请参阅实施 Amazon S3 支持的 Amazon CloudFront 中的默认目录索引 使用 Lambda@Edge 的起源。

      重要提示:您在使用 Lambda@Edge 时需要支付额外费用。如需更多信息,请参阅 Lambda@Edge 定价详情。

      AWS Knowledge Center here 中所述 “在此示例中,您使用 CloudFront 边缘的计算能力来检查来自客户端的请求。然后重新编写请求,以便 CloudFront 为任何请求请求一个默认索引对象(在本例中为 index.html)以 '/' 结尾的请求 URI。”

      JS 中的 Lambda 边缘函数:

      'use strict';
      exports.handler = (event, context, callback) => {
          
          // Extract the request from the CloudFront event that is sent to Lambda@Edge 
          var request = event.Records[0].cf.request;
      
          // Extract the URI from the request
          var olduri = request.uri;
      
          // Match any '/' that occurs at the end of a URI. Replace it with a default index
          var newuri = olduri.replace(/\/$/, '\/index.html');
          
          // Log the URI as received by CloudFront and the new URI to be used to fetch from origin
          console.log("Old URI: " + olduri);
          console.log("New URI: " + newuri);
          
          // Replace the received URI with the URI that includes the index page
          request.uri = newuri;
          
          // Return to CloudFront
          return callback(null, request);
      
      };

      确保您已授予 Lambda@Edge 函数 IAM 权限

      {
          "Version": "2012-10-17",
          "Statement": [
              {
                  "Effect": "Allow",
                  "Action": [
                      "logs:CreateLogGroup",
                      "logs:CreateLogStream",
                      "logs:PutLogEvents"
                  ],
                  "Resource": [
                      "arn:aws:logs:*:*:*"
                  ]
              }
          ]
      }

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题,我最终将我的 S3 存储桶设置为公共存储桶,我不想支付额外的 Lambda 来处理这个问题。

        这是post 解释情况。

        【讨论】:

          猜你喜欢
          • 2017-04-12
          • 1970-01-01
          • 2019-09-25
          • 2021-01-24
          • 2020-04-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多