大虾如何将PDF文件存储到Amazon S3中

发布于 2024-12-20 09:15:56 字数 1316 浏览 2 评论 0原文

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

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 中的正确目录中...所有帮助将不胜感激!

曼尼,谢谢,九月

I have a problem with the path. In my model I have following setup:

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

and in a controller looks my action this:

   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

And my problem is, that this PDF file is stored to the S3 server, but on the bad place. Instead the directory app/public/pdff/id_of_a_user/file_name_of_pdf_file is the file saved to

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

I am not totally sure, if I use the prawn for saving PDF files right, but I think the problem could be in the controller, where I have set up the place, where the created file have to be saved...

I would like to ask you, what I should change for saving PDF files into the right directory in S3... All helps will be appreciated!

Manny thanks, Sep

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回心转意 2024-12-27 09:15:56

您可以使用类似 CarrierWave ( https://github.com/jnicklas/rierwave ) 的东西 - 它使得使用 FOG 库可以非常轻松地上传到 S3 https://github.com/jnicklas/rierwave

You could use something like CarrierWave ( https://github.com/jnicklas/carrierwave ) -- it makes uploading to S3 extremely easy with the FOG library https://github.com/jnicklas/carrierwave

养猫人 2024-12-27 09:15:56

路径的 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

我希望这会有所帮助。

The Users/my_name/my_ruby_root_directory/name_of_my_project/public portion of the path came from :rails_root/public portion of the path you configured in paperclip. So if you really want the s3 "directory" to be app/public/pdff/id_of_a_user/file_name_of_pdf_file you need to give paperclip the following path: app/public/pdffiles/:user_id/:style/:basename.:extension

Also, according to your model, you should use pdff.invoice_file = pdf_file instead of pdff.pdffile_file = pdf_file

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文