howto:载波的基本设置
我有一个用于生产和开发的S3桶。 我已完成我的研究并遇到了这篇文章,但我当前的配置无法按预期工作。 我在本地获得以下exception(下面),我从我的heroku应用程序中没有上传到我的S3存储桶的文件:
is not a recognized storage provider Extracted source (around line #3): 1: 2: 3: 4: 5: 6:
但是,当我在*_uploader.rb
文件中设置storage :file
,一切都按预期在本地工作。 但仍然注意到有人被送到我的S3桶。
这是我的设置:
user.rb
class User < ActiveRecord::Base attr_accessible :name, :avatar, :avatar_cache, :remote_avatar_url, :remove_avatar mount_uploader :avatar, AvatarUploader end
fog.rb
CarrierWave.configure do |config| if Rails.env.production? config.storage = :fog config.fog_credentials = { :provider => 'AWS', :aws_access_key_id => ENV['S3_K'], :aws_secret_access_key => ENV['S3_SCRT'], :region => ENV['S3_RG'] } config.fog_directory = ENV['S3_BUCKET'] config.fog_host = 'http://www.example.com' config.fog_public = true # optional, defaults to true config.fog_attributes = {'Cache-Control' => 'max-age=315576000'} # optional, defaults to {} else #for development and testing locally config.storage = :file config.enable_processing = false end end
* _uploader.rb
class AvatarUploader < CarrierWave::Uploader::Base storage :fog def store_dir "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" end def extension_white_list %w(jpg jpeg gif png) end end
users_controller.rb
def new @user = User.new @user.avatar = params[:file] respond_to do |format| format.html # new.html.erb format.json { render json: @user } end end def create @user = User.new(params[:user]) @user.avatar = params[:file] respond_to do |format| if @user.save format.html { redirect_to @user, notice: 'User was successfully created.' } format.json { render json: @user, status: :created, location: @user } else format.html { render action: "new" } format.json { render json: @user.errors, status: :unprocessable_entity } end end
结束
更新感谢@CanBerkGüder我可以确认我能够保存记录但不能保存图像文件。 无论何时我尝试创建用户对象,我的heroku日志都会吐出:
2012-02-20T23:19:45+00:00 app[web.1]: app/controllers/users_controller.rb:46:in `block in create' 2012-02-20T23:19:45+00:00 app[web.1]: app/controllers/users_controller.rb:45:in `create' 2012-02-20T23:19:45+00:00 app[web.1]: 2012-02-20T23:19:45+00:00 app[web.1]: cache: [POST /users] invalidate, pass
好的,这是一个想法。 CarrierWave仍然包含一个S3适配器,用于向后兼容,使用下面的雾,我个人使用:fog
而不是:fog
。 从理论上讲,两者之间应该没有区别,但我认为值得一试。 这是我在Heroku上运行的实时应用程序的CarrierWave初始化程序:
CarrierWave.configure do |config| if Rails.env.production? config.root = Rails.root.join('tmp') config.cache_dir = 'carrierwave' config.storage = :s3 config.s3_access_key_id = ENV['S3_KEY'] config.s3_secret_access_key = ENV['S3_SECRET'] config.s3_bucket = ENV['S3_BUCKET'] else config.storage = :file end end
前两行(config.root和config.cache_dir)可以解决Heroku的只读文件系统,它应该与您的问题无关。
一个问题可能是
config.fog_host = 'http://www.example.com'
如果您使用此设置,那么您可能需要一个真正的主机。 据说你可以评论那条线,虽然我已经看到它强烈推荐。
我假设你在Heroku上设置你的ENV变量?
heroku config:add S3_K=[my_s3_key] S3_SCRT=[my_s3_secret] S3_RG=[us-east-1]S3_BUCKET=[my_bucket_name]
我使用了几乎相同的设置。 我没有工作,但我想我还有一两步:
Missing required arguments: aws_access_key_id, aws_secret_access_key