Search code examples
ruby-on-rails-3herokuamazon-s3dragonfly-gem

Define Dragonfly specific path for S3 Datastore


I need to define an specific structure path for storing my files in S3.

Example:

Instead of

'bucket_name/2010/12/23/127/43/2345/File.jpg'

I need

'bucket_name/artists/elvis_presley/faceshot/100x100.jpg'

'bucket_name/books/the_black_cat/cover/180x280.jpg'

etc.

I read a similar question but don't catched much of it.

Thanks.


Solution

  • UPDATE ---

    Just do something like this, you can override like below if you really need something special. Easier way is built in:

    some_image.store({:path => "images/some_identifier/the_name.jpg"}) 
    

    That's what we will get stored on your bucket.

    Original Post


    Stick this in a file, say dragonfly.rb, in config/initializers

    # Monkey patch for Dragonfly's S3 implementation
    module Dragonfly
      module DataStorage
        class S3DataStore
    
          def generate_uid(name)
            # Replace this sucker for a better name
            "#{Time.now.strftime '%Y/%m/%d/%H/%M/%S'}/#{rand(1000)}/#{name.gsub(/[^\w.]+/, '_')}"
          end
    
        end
      end
    end