【问题标题】:Using different S3 buckets for production and development in Carrierwave在 Carrierwave 中使用不同的 S3 存储桶进行生产和开发
【发布时间】:2011-11-14 12:50:00
【问题描述】:

我开始使用Carrierwave,作为Paperclip 的替代品。

我可以从文档中看到,要使用 S3,我应该在初始化程序中配置 Fog:

CarrierWave.configure do |config|
  config.fog_credentials = {
    :provider               => 'AWS',       # required
    :aws_access_key_id      => 'xxx',       # required
    :aws_secret_access_key  => 'yyy',       # required
    :region                 => 'eu-west-1'  # optional, defaults to 'us-east-1'
  }

end

但是,如何为不同的环境设置不同的存储桶?使用回形针,我将在 yml 文件中为开发/生产/等指定不同的凭据和/或存储桶。使用载波的最佳方法是什么?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 amazon-s3 paperclip carrierwave


    【解决方案1】:

    如果你愿意,你可以用几乎完全相同的方式来做,就像这个完全未经测试的想法:

    # config/initializers/carrierwave.rb
    CarrierWave.configure do |config|
      my_config = "#{Rails.root}/config/fog_credentials.yml"
    
      YAML.load_file(my_config)[Rails.env].each do |key, val|
        config.send("#{key}=", val)
      end
    end
    
    # config/fog_credentials.yml
    common: &common
      aws_access_key: 'whatever'
      ...
      fog_credentials:
        provider: 'whoever'
        ...
    production:
      <<: *common
      fog_directory: 'my-production-bucket'
    development:
      <<: *common
      fog_directory: 'my-dev-bucket'
    

    或者,如果您想放弃 YAML,您总是可以简单地在初始化程序中测试环境并使用案例或条件,最简单的是:

    CarrierWave.configure.do |config|
      if Rails.env.development?
        # configure one env
      else
        # configure another
      end
    
      # configure common stuff
    end
    

    【讨论】:

    • 我没有按照您的建议测试加载 YAML 文件,但我将使用选项 2 并使用简单的条件语句。看起来不错。谢谢!
    【解决方案2】:
    class S3ArticleUploader < CarrierWave::Uploader::Base
    
      if Rails.env.test?
        storage :file
      else
        storage :fog
      end
    
      def fog_directory
        ARTICLE_UPLOADER_BUCKET
      end
    
      def store_dir
        "#{ model.parent_id }/#{ model.id }"
      end
    
    end
    
    # config/environments/development.rb
    ARTICLE_UPLOADER_BUCKET = 'development-articles'
    
    # config/environments/production.rb
    ARTICLE_UPLOADER_BUCKET = 'production-articles'
    

    fog_directory 方法在你不在 TestEnvironment 时调用并初始化正确的 桶。

    你也可以这样做:

      def store_dir
        if self._storage == CarrierWave::Storage::File
          "#{Rails.root}/tmp/files/#{ model.parent_id }/#{ model.id }"
        elsif self._storage == CarrierWave::Storage::Fog
          "#{ model.parent_id }/#{ model.id }"
        end
      end
    

    v2

    class S3ArticleUploader < CarrierWave::Uploader::Base
    
      if Rails.env.test?
        storage :file
      else
        storage :fog
      end
    
      def initialize
        self.fog_directory = ARTICLE_UPLOADER_BUCKET
      end
    
      def store_dir
        "#{ model.parent_id }/#{ model.id }"
      end
    
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-21
      • 1970-01-01
      • 1970-01-01
      • 2019-06-28
      • 1970-01-01
      • 2012-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多