user984621
user984621

Reputation: 48443

How to store prawn PDF files into Amazon 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

Upvotes: 5

Views: 2573

Answers (2)

brettish
brettish

Reputation: 2638

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.

Upvotes: 1

Josh
Josh

Reputation: 945

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

Upvotes: 2

Related Questions