Rails : Upload secret files using Capistrano before booting

This answer is tested with Capistrano 3.6.1

Well, in that case, you would have to make capistrano upload your application.yml(database.yml if its ignored from git).

# in config/deploy.rb

# Default value for :linked_files is []
append :linked_files, 'config/database.yml', 'config/application.yml'

Now write some code to upload the files to the server

namespace :upload do
  desc 'Upload shared files to the server'
  task :yml_files do
    on roles(:web) do |host|
      fetch(:linked_files).each do |common_file|
        upload! common_file, "#{fetch(:deploy_to)}/shared/#{common_file}"

Now you need to trigger the above mentioned capistrano task just before a very specific event.

# config/deploy.rb

before 'deploy:check:linked_files', 'upload:yml_files'

This will make sure the necessary environment variables are set before your deployed tries to boot your application in the production server.

Rails : File upload without Carrierwave && Paperclip

The most important thing to remember with file uploads is that the rendered form’s encoding MUST be set to multipart/form-data. I guess you have that maintained.

<%= form_tag({action: :upload}, multipart: true) do %>
  <%= file_field_tag 'file' %>
<% end %>

Now in controller you will have access to IO object via params. The object in the params hash is an instance of a subclass of IO.

The object will have an original_filename attribute containing the name the file had on the user’s computer and a content_type attribute containing the MIME type of the uploaded file.

You can either move the temporary file to some dir in your public directory or simple use and throw away.

def upload
  uploaded_io = params[:file]

  # Moving the file to some safe place; as tmp files will be flushed timely
  File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file|

for you, you can leverage the tempfile attribute in the IO object. The actual file is accessible via the tempfile accessor, though some of its interface is available directly for convenience.

So, this should work

csv_text = File.read(file.tempfile)

> uploaded_io = params[:file]
=> #<ActionDispatch::Http::UploadedFile:0x007f4154fe9a38
 @headers="Content-Disposition: form-data; name=\"file\"; filename=\"3423loadshedding.pdf\"\r\nContent-Type: application/pdf\r\n",

[2] pry(#<PostsController>)> uploaded_io.tempfile
=> #<File:/tmp/RackMultipart20160421-14883-1lv0qtq.pdf>

> uploaded_io.tempfile.class
=> Tempfile

For Error

ProductsController#upload No such file or directory @ rb_sysopen - /<path to my app>/public/uploads/X.csv 

You need to first create the folder public/uploads

$ mkdir public/uploads

Now try; the file should be moved. This is how Paperclip and Carrierwave manage uploaded files.

Amazon S3 : Carrier Wave : Error : The difference between the request time and the current time is too large.

Error:  Excon::Errors::Forbidden

You have sometime faced this error which you can see in the server log in development or any environment. This mainly concerns with Amazon S3 server or any other servers you rely upon for APIs.

Main Reason:

  • You have changed your system Date – Time by any means
  • You use Virtual Machine to run your operating system in which your development environment run and you have resumed the system from suspension after a while.
  • Your server Date – Time is not configured properly if you have your own server locally (Physically)

Continue reading

Ruby On Rails :: Heroku file upload issue : Timeout

Uploading large files in Heroku

If you have forms uploading files to the server in Heroku, you may experience problems when file size is larger and internet bandwidth/speed of yours is normal. Heroku has a very small timeout set for operations/(HTTP requests). For heavy operation such as below, Heroku recommends to use background jobs.

  • Sending an email
  • Accessing a remote API (posting to Facebook, Twitter, querying Flickr, etc.)
  • Web scraping / crawling / Web spider operations
  • Rendering an image or PDF
  • Heavy computation (computing a fibonacci sequence, etc.)
  • Heavy database usage (slow or numerous queries, N+1 queries)

Source: https://devcenter.heroku.com/articles/request-timeout#uploading-large-files