【问题标题】:Can not access index.html from S3 bucket when it's in a folder当它位于文件夹中时,无法从 S3 存储桶访问 index.html
【发布时间】:2018-07-28 06:56:14
【问题描述】:

我有一个名为 mywebsite.com(不是真实姓名)的 S3 存储桶。在存储桶中,我有一个网站的静态内容,位于一个名为 build 的文件夹中。

build文件夹ls build/的内容:

asset-manifest.json     index.html              manifest.json           static
favicon.ico             lu1.png                 service-worker.js

我公开了存储桶:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::mywebsite.com/*"
        }
    ]
}

所以我应该可以从这里访问存储桶:

http://mywebsite.com.s3-website.eu-west-2.amazonaws.com(不是真实网址)

然后,对于Index documentError document,我尝试指定:

build/index.html

但是,如果我尝试这样做,我会得到:

The IndexDocument Suffix is not well formed

所以我将Index documentError document 保留为index.html

但是现在找不到索引文件了:

404 Not Found
Code: NoSuchKey
Message: The specified key does not exist.
Key: index.html

我尝试了hereherehere 的建议,但错误仍然存​​在。

这似乎是一个非常普遍的问题。那么,您是如何做到让 S3 能够在文件夹中看到您的 index.html 的呢?

【问题讨论】:

    标签: amazon-web-services amazon-s3


    【解决方案1】:

    索引文档index.html 用于每个文件夹。

    因此,当您请求 http://mywebsite.com.s3-website.eu-west-2.amazonaws.com/ 时,它将呈现 /index.html

    如果您请求http://mywebsite.com.s3-website.eu-west-2.amazonaws.com/build/,它将呈现/build/index.html

    所以每个文件夹都需要它自己的index.html 文件,如果你想拥有这种行为。您不能将一个 build/index.html 用于所有文件夹。

    关于错误文档的附加说明

    错误文档总是相对于根呈现。

    因此,如果您请求 /some/folder/that/does/not/exist/,如果 404.html 被配置为您的错误文档,它将呈现 /404.html

    【讨论】:

      【解决方案2】:

      编辑 经过一些广泛的测试。我认为前面的规则不适用于“/”(根)路径并创建一个连续的重定向,然后我想出了这个适用于我的规则。

      <RoutingRules> 
      <RoutingRule>
      <Condition>
          <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
      </Condition>
      <Redirect>
          <ReplaceKeyPrefixWith>build/</ReplaceKeyPrefixWith>
      </Redirect> </RoutingRule> </RoutingRules>
      

      您需要自定义重定向规则将 /index.html 重定向到 /build/index.html

      <RoutingRules>
        <RoutingRule>
         <Condition>
           <KeyPrefixEquals> 
             index.html
           </KeyPrefixEquals>
          </Condition>
        <Redirect>
           <ReplaceKeyPrefixWith>build/index.html</ReplaceKeyPrefixWith></Redirect>
        </RoutingRule>
      </RoutingRules>
      

      参考:https://docs.aws.amazon.com/AmazonS3/latest/dev/how-to-page-redirect.html#advanced-conditional-redirects

      【讨论】:

        猜你喜欢
        • 2021-08-21
        • 1970-01-01
        • 2020-06-15
        • 1970-01-01
        • 2018-05-05
        • 2017-05-12
        • 1970-01-01
        • 2015-08-23
        • 2012-06-22
        相关资源
        最近更新 更多