Read more

Caution: Carrierwave has your file three times (temporarily)

Dominik Schöler
June 28, 2017Software engineer at makandra GmbH

When storing a file with Carrierwave, it is always cached prior to actually storing it (to support form roundtrips Show archive.org snapshot ).

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

Carrierwave by default 1) copies the file to the cache and then 2) copies it again to its final destination (deleting the cached file immediately after storing). This means there are 3 (three) instances of a file on the disk at some point in time, and still 2 instances after Carrierwave is done if you do not remove the original file manually.

Usually this is not an issue, because a file upload from a browser will immediately write to the cache, and most files are of a size that is still ok when tripled. However, this can become an issue with large files, regarding both time and disk usage.

Suggested solution for large files

You can easily configure Carrierwave to move the files instead of copying them. Simply define two methods in your uploader class:

def move_to_cache
  true
end

def move_to_store
  true
end

Profit!

Posted by Dominik Schöler to makandra dev (2017-06-28 14:45)