Question

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.

Was it helpful?

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

OTHER TIPS

Since sometime around Dragonfly 0.9.4, you can do this in the model:

class User < ActiveRecord::Base
  image_accessor :image do
    storage_path{ "users/#{self.user_type}/#{self.login_name" }
  end
  # ...
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top