我运行一个分期和开发环境,它是生产数据库的镜像。
我使用剪纸宝石存储上传到S3。为了安全起见,我希望让非生产环境从生产桶(因为文件存储在那里)读取,而是写入另一个存储桶。此外,如果上传完成,应用程序应该足够聪明,现在可以从不同的桶中读取。
有办法做到这一点吗?
发布于 2015-01-01 20:28:40
经过进一步研究,我发现has_attached_file的has_attached_file选项可以使用proc。
config/inititalizers/paperclip_defaults.rb
LIVE_BUCKET = 'media'
STAGING_BUCKET = 'media-demo'
Paperclip::Attachment.default_options.merge!(
storage: :s3,
path: 'assets/:class/:id/:attachment/:style.:extension',
s3_credentials: "#{Rails.root}/config/s3.yml",
s3_host_name: 's3-us-west-2.amazonaws.com',
use_timestamp: false,
bucket: proc { |attachment|
(Rails.env.production? || !attachment.dirty?) ? LIVE_BUCKET : STAGING_BUCKET
}
)相关文件:
http://www.rubydoc.info/github/thoughtbot/paperclip/master/Paperclip/Storage/S3 method
发布于 2014-12-31 20:28:14
实现这一点的方法是,我有一个本地配置文件,与服务器上的配置文件不同,我使用Heroku托管我的应用程序。
1)我创建了一个名为config/config.yml的文件
#S3
S3_KEY: 'xxxxxx'
S3_SECRET: 'xxxxxxxx'
S3_REGION: 'us-east-1'
S3_ASSET_URL: 'appname-dev.s3-website-us-east-1.amazonaws.com'
S3_BUCKET_NAME: 'appname-dev'2)我将config/config.ym中的变量读入config/application.rb require File.expand_path(‘./File.expand_path’,文件)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)
module Appname
class Application < Rails::Application
config.before_initialize do
dev = File.join(Rails.root, 'config', 'config.yml')
YAML.load(File.open(dev)).each do |key,value|
ENV[key.to_s] = value
end if File.exists?(dev)
end
end
end然后在Heroku上设置配置变量
我希望这能帮上忙。
https://stackoverflow.com/questions/27725942
复制相似问题