开发者

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

'开发者_C百科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.


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜