【问题标题】:How to store prawn PDF files into Amazon S3如何将虾 PDF 文件存储到 Amazon S3 中
【发布时间】:2012-01-17 16:42:43
【问题描述】:

我的路径有问题。在我的模型中,我有以下设置:

class Pdffiles < ActiveRecord::Base
  belongs_to :user

  has_attached_file :invoice_file,
                    :path => ":rails_root/public/pdffiles/:user_id/:style/:basename.:extension",
                    :url => "/pdffiles/:user_id/:style/:basename.:extension",

                    :storage => :s3,
                        :bucket => '...',
                        :s3_credentials => {
                          :access_key_id => '...',
                          :secret_access_key => '...'
                        }  
end

在控制器中查看我的操作:

   pdf = Prawn::Document.new
    pdf.move_down 70

    pdf.text("Prawn Rocks")
    pdf.render_file('prawn.pdf')
    pdf_file = File.open('prawn.pdf')

    pdff = Pdffile.new()
    pdff.pdffile_file = pdf_file
    pdff.user_id = todays_user.id
    pdff.save

我的问题是,这个 PDF 文件存储在 S3 服务器上,但位置不好。相反,目录app/public/pdff/id_of_a_user/file_name_of_pdf_file 是保存到的文件

Users/my_name/my_ruby_root_directory/name_of_my_project/public/pdffiles/id_of_a_user/file_name_of_pdf_file.

我不完全确定,如果我使用虾来保存 PDF 文件是否正确,但我认为问题可能出在控制器中,我已经设置了位置,必须保存创建的文件......

我想问你,为了将 PDF 文件保存到 S3 中的正确目录中,我应该进行哪些更改...所有帮助将不胜感激!

谢谢,九月

【问题讨论】:

    标签: ruby-on-rails-3 pdf amazon-s3 paperclip prawn


    【解决方案1】:

    您可以使用像 CarrierWave (https://github.com/jnicklas/carrierwave) 之类的东西——它使用 FOG 库 https://github.com/jnicklas/carrierwave 使得上传到 S3 变得非常容易

    【讨论】:

      【解决方案2】:

      路径的Users/my_name/my_ruby_root_directory/name_of_my_project/public 部分来自您在回形针中配置的路径的:rails_root/public 部分。因此,如果您真的希望 s3“目录”为 app/public/pdff/id_of_a_user/file_name_of_pdf_file,则需要为回形针提供以下路径:app/public/pdffiles/:user_id/:style/:basename.:extension

      另外,根据您的模型,您应该使用pdff.invoice_file = pdf_file 而不是pdff.pdffile_file = pdf_file

      我希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-05
        • 2015-02-24
        • 1970-01-01
        • 2017-06-14
        • 2018-04-04
        • 2012-11-02
        • 1970-01-01
        相关资源
        最近更新 更多