Granular 'public' settings on uploaded files with Fog and Carrierwave
Asked Answered
U

2

7

I am creating a rails app that lets an administrator upload photos that are optionally publicly displayed. For the upload / storage process I am using the Carrierwave gem along with the Fog gem and S3. The issue is that in order to get this all working, I have to make every file uploaded to the s3 bucket public. Is there a way to make files public / private on a file-by-file basis? Also, if this file-by-file granularity is possible, can it extend down to versions of images (created by automatic Carrierwave resizing)?

Currently, I have the following line in my carrierwave initializer:

  config.fog_public = true
Usurp answered 18/7, 2011 at 15:6 Comment(2)
This is probably a long-requested missing feature in most Rails uploaders. I'd suggest going with DragonFly as it permits an unprecedented level of customization and is able to do just that.Keewatin
After just a quick look at DragonFly, it does appear to support what I am trying to accomplish, and seems to be much more flexible than CarrierWave as well. Thanks.Usurp
B
7

Actually, it's dead simple in Carrierwave.

You can do this:

class PrivateUploader < StandardUploader  

  @fog_public = false

Or (untested but should work perfectly) this:

class PrivateUploader < StandardUploader  


  def fog_public
    if local_condition
      true
    else
      false
    end
  end

:-)

I haven't tried DragonFly, but now that a couple of issues have been fixed in the last 2 months with Carrierwave, it's far superior to anything else I've seen. Insanely flexible.

//matt

Bugg answered 6/8, 2011 at 8:21 Comment(1)
I had previous setup an after :store callback to achieve this, setting my original version to private the rest to public. This code helped simplify my work immensely. Specifically, to make all versions of an upload public except the original, I now use !self.version_name.nil? as my local_condition in the above code.Hoag
S
5

Just have to make your uploader class override the base class. I tore my hair out today too.. :( This worked for me:

Using Carrierwave 0.8.0 (in May 2013) /app/uploaders/whatever_uploader.rb

class WhateverUploader < CarrierWave::Uploader::Base
  def fog_public
    true # or false
  end
end
Soothe answered 29/5, 2013 at 12:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.