【问题标题】:How to download and open a file form S3 via rails如何通过 Rails 从 S3 下载和打开文件
【发布时间】:2020-01-06 16:49:22
【问题描述】:

我有用于保存、打开、下载文件(例如:pdf、docx 等)的控制器。我将它们保存在 S3 上。但我不知道,我怎样才能下载或打开文件。 我的控制器

class PdfsController < ApplicationController
  before_action :set_pdf, only: %i[show edit update destroy download]
  before_action :set_url_file, only: %i[download show]

 def show
 send_file(@pdf_filename, filename: @pdf.path.to_s,
                          type: 'application/pdf',
                          x_sendfile: true,
                          disposition: 'inline')

 end

 def download
  send_file(@pdf_filename, filename: @pdf.path.to_s,
                          type: 'application/pdf',
                          x_sendfile: true)

 end

 private

 def set_url_file
  if Rails.env.production?
    @pdf_filename = File.join(@pdf.path.to_s)
  else
    @pdf_filename = File.join(Rails.root, "public#{@pdf.path}")
  end
 end
end

action show 在浏览器中显示 pdf 文件(仅 pdf),action download 在本地保存任何文件。

当我在本地保存文件时它起作用了。但是如果我在 heroku 上部署项目,我会收到错误 ActionController::MissingFile。在我知道我可以下载文件后,我必须使用aws-sdk。但我不明白它是如何用于打开和下载的。我该怎么做?

【问题讨论】:

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


    【解决方案1】:

    当您使用 Rails 5.2 或更高版本时,您不需要aws-sdk。只需使用 Active Storagehttps://guides.rubyonrails.org/v5.2/active_storage_overview.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-29
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多