2

我用回形針什麼樣的環境文件存儲在Amazon S3上,而這個代碼,與aws-s3寶石一起,讓我來存儲文件上傳與Amazon S3:如何有條件地根據你在

has_attached_file :photo, 
    :styles => { 
    :tiny => "25x25#", 
    :shown => "40x40#", 
    :thumbnail => "50x50#", 
    :small => "150x150>", 
    :medium => "300x300>" }, 
    :default_url => "/images/default_:style.jpg", 
    :storage => :s3, 
    :s3_credentials => "#{RAILS_ROOT}/config/s3.yml", 
    :path => "profile/:attachment/:style/:id.:extension" 

但是,我不想在我的開發環境中將文件存儲在Amazon S3上。我如何在我的應用程序中設置?

回答

1

你很可能這樣做

:storage => Rails.env.production? ? :s3 : :whatever 
+0

什麼應該':無論是什麼?因爲通常我會忽略':storage',如果我不想在s3上存儲文件... – 2011-06-13 04:16:03

+0

nil應該做的竅門 – 2011-06-13 04:23:08

+1

可能是:文件系統,它應該將其保存爲本地文件 – PerfectlyNormal 2011-06-13 04:23:11

1

在environment.rb文件的末尾:

APP_CONFIG = YAML.load_file("#{Rails.root.to_s}/config/config.yml")[Rails.env] 

在配置/ config.yml:

development: 
    use_amazon: false 

test: 
    use_amazon: false 

production: 
    use_amazon: true 

而在你的控制器:

if APP_CONFIG['use_amazon'] 
    #USING AMAZON S3 
else 
    #USING SOMETHING ELSE 
end 

這應該工作。 祝你好運!