【问题标题】:S3 website downloads files instead of displaying pagesS3 网站下载文件而不是显示页面
【发布时间】:2021-06-28 08:06:42
【问题描述】:

我正在尝试使用 Terraform 在 S3 上设置静态页面。当我访问端点时,我的浏览器会尝试下载文件而不是显示页面。

resource "aws_s3_bucket" "b1" {
  bucket = "my-bucket"
  website {
    index_document = "index.html"
    error_document = "error.html"
  }
  tags = {
    Name        = "my bucket"
    Environment = "dev"
  }
}

resource "aws_s3_bucket_policy" "bucket_policy" {
  bucket = aws_s3_bucket.b1.id

  policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "PublicReadGetObject",
      "Effect": "Allow",
      "Principal": "*",
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::my-bucket/*"
    }
  ]
}
EOF

}

resource "aws_s3_bucket_object" "object" {
  bucket = aws_s3_bucket.b1.id
  key    = "index.html"
  source = "myfiles/index.html"
  etag   = filemd5("myfiles/index.html")
}

【问题讨论】:

  • 您可以尝试将content_type 添加/指定到aws_s3_bucket_object

标签: amazon-web-services terraform terraform-provider-aws


【解决方案1】:

默认情况下,S3 对象具有通用内容类型application/octet-stream,浏览器通常只通过下载文件来处理,因为没有足够的信息来选择更具体的处理方式。

您可以使用content_type 参数设置不同的内容类型aws_s3_bucket_object。 HTML 文件的预期内容类型是 text/html,因此您可以像这样配置您的对象:

resource "aws_s3_bucket_object" "object" {
  bucket       = aws_s3_bucket.b1.id
  key          = "index.html"
  source       = "myfiles/index.html"
  etag         = filemd5("myfiles/index.html")
  content_type = "text/html"
}

如果您要上传各种不同类型的对象,您可能会发现使用the shared module hashicorp/dir/template 会更容易,它在其实现中包含一个从文件名后缀到适当content_type 值的默认映射表,因此您可以可能会处理具有常规文件名后缀的文件目录,然后是map the result systematically into S3 objects

【讨论】:

    猜你喜欢
    • 2017-07-11
    • 1970-01-01
    • 2012-11-03
    • 2013-02-22
    • 2012-12-18
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 2013-04-04
    相关资源
    最近更新 更多