One of the most popular plugins to use for file uploading and processing is carrierwave. I have used it extensively in all my rails projects that require some form of file upload and it has never let me know - it always works out of the box and as expected.
One of the issues that has always interested me but I never took the time to investigate is - how do you define per user settings for each uploader instance? Normally the carrierwave settings are defined in advance in a rails initializer file and that pre determines the type of storage, where the file is uploaded to etc. In a recent project using carrierwave I had to upload user files to Amazon S3 using fog but each file had to go to the end user’s bucket.
What I did was to remove the initializer file from the rails config directory as that does not apply anymore. Within the uploader you would have to define the settings as instance methods. Thats the glue to the trick: define each setting as an instance method and then grab the user’s setting within the method
For instance to set the s3 bucket directory, we can do something like this:
In the example above, we have set the fog_directory to be based on the settings attribute of the user model on which this uploader is mounted on. The same can be applied to all the other setting such as ‘fog_host’, “fog_public’
You may be wondering thats great but where do I define each user’s s3 settings? The answer is you should not have to. When you are uploading into another user’s s3 bucket you would first need to add your settings as an ACL entry into their s3 account first. The example above assumes you have already done so and all you need is just your own s3 settings to upload to their s3 bucket.
Hope this helps someone.