【问题标题】:How to upload a html file in s3 via Rails如何通过 Rails 在 s3 中上传 html 文件
【发布时间】:2020-07-18 18:31:11
【问题描述】:

所以,我正在尝试将 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 的白屏

我点击了这个链接

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文件上传吗?

【问题讨论】:

    标签: ruby-on-rails amazon-web-services amazon-s3 aws-sdk


    【解决方案1】:

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

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

    您需要上传所有其他资源,根据加载方式,它可能会起作用,也可能不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-02
      • 1970-01-01
      • 2019-07-25
      • 2017-04-02
      • 1970-01-01
      • 1970-01-01
      • 2018-01-21
      • 2019-07-30
      相关资源
      最近更新 更多