如何通过 Rails 在 s3 中上传 html 文件

How to upload a html file in s3 via Rails

所以,我正在尝试将 HTML 文件上传到我的 Aws s3,该文件正在上传,但它不会在浏览器中呈现为 HTML 文件。

  def upload_coverage_s3
   path_to_file = Rails.root.to_s+'/public/coverage/index.html'
   file = File.open(path_to_file)
   aws_path = "test_coverage/#{Time.now.to_i}/index.html"
   uploadObj = AwsHelper.upload_to_s3_html(aws_path,file)
   uploadObj[:url]
  end



   def self.upload_to_s3_html(path,file)
     if path.nil? || path.blank?
      puts 'Cannot upload. Path is empty.'
      return
     end

     obj = S3_BUCKET.objects[path]


     obj.write(
        file: file,
        content_type: "text/html",
        acl: :public_read
      )

     upload = {
       url: obj.public_url.to_s,
       name: obj.key
      }

    upload
  end

所有我得到一个带有加载 gif 的白屏

我关注了这个link

Upload HTML file to AWS S3 and then serving it instead of downloading

因为我想要类似的功能uploading HTML file and then serving as an HTML file instead of downloading

PS: 我也在我的 s3 存储桶中手动上传了那个 HTML 文件,问题是一样的。 如何解决。 s3 不支持 HTML 文件上传吗?

您只上传了一个 HTML 文件,没有其他依赖项。

您似乎正在上传测试覆盖率结果。通常 index.html 只是入口点,您的测试覆盖率工具会生成更多文件。

您需要上传所有其他资源,并且取决于它们的加载方式,它可能有效也可能无效。