将PDF保存到文件

时间:2018-04-01 03:20:41

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

我有代码来编写文件来保存PDF然后将其上传到s3。我在dev中完美地工作了。但不是在制作中。

def upload_to_s3(pdf)
  save_path = "#{Rails.root}/tmp/pdfs/invoice_#{@invoice.id}.pdf"
  f = File.new(save_path, 'w:ASCII-8BIT')
  f.write(pdf)
  uploader = InvoiceUploader.new
  File.open(save_path) { |file| uploader.store!(file) }
  @invoice.update(pdf: uploader.url)
  File.delete(save_path)
  uploader.url
end

堆栈追踪:

Rendered invoices/pdf.html.erb (6.5ms)
Completed 500 Internal Server Error in 1420ms (ActiveRecord: 34.6ms)
Errno::ENOENT (No such file or directory @ rb_sysopen - /home/deploy/reputation/releases/20180401031049/tmp/pdfs/invoice_2.pdf):
app/controllers/api/v1/invoices_controller.rb:140:in `initialize'
[5e443877-e7fe-4848-847b-f5f6159e7db9] 
app/controllers/api/v1/invoices_controller.rb:140:in `new'
[5e443877-e7fe-4848-847b-f5f6159e7db9] 
app/controllers/api/v1/invoices_controller.rb:140:in `upload_to_s3'
[5e443877-e7fe-4848-847b-f5f6159e7db9] 
app/controllers/api/v1/invoices_controller.rb:65:in `pdf'

1 个答案:

答案 0 :(得分:2)

错误消息明确指出:

  

Errno :: ENOENT(没有这样的文件或目录@ rb_sysopen - /home/deploy/reputation/releases/20180401031049/tmp/pdfs/invoice_2.pdf):...

预先在生产中创建目录/home/deploy/reputation/releases/20180401031049/tmp/pdfs/。由于它是动态的(取决于发布日期时间),最好在ruby代码中创建它:

save_dir = "#{Rails.root}/tmp/pdfs/"
Dir.mkdir(save_dir)

save_path = ...