简体   繁体   中英

How to save a file on Heroku using a rake task?

I have a rake task to update some fields in the mongo db using mongoid. I use CSV.open to create a file to store the error for anomalies which can't be updated programatically. When I run this locally it simply creates the CSV file in the root directory of the app. But when I run rake on Heroku it doesn't seem to save the file anywhere I can access it. Here's my code:

require 'csv'
CSV.open("errors.csv", "w") do |csv|
  csv << ["Email", "Jacket Size"]
  errors.each do |e|
    csv << e
  end
end

Heroku filesystem is read-only so your rake task cannot save this file in app root directory. With Heroku you can save only to /tmp and /log directories. You can create your CSV file in /tmp directory and after it's processed you have to save it at some external storage (like Amazon S3) or send via mail, whatever you think is better in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM