【发布时间】:2012-02-20 21:18:34
【问题描述】:
我有一个用于生产和开发的 S3 存储桶。我已经完成了我的研究和came across this post,但我当前的配置没有按预期工作。我在本地收到以下异常(如下),并且没有从我的 heroku 应用程序将文件上传到我的 S3 存储桶:
is not a recognized storage provider
Extracted source (around line #3):
1:
2: <p><%= user.name %></p>
3: <%= image_tag user.avatar.url %>
4: <%= link_to 'Show', user %>
5: <%= link_to 'Edit', edit_user_path(user) %>
6: <%= link_to 'Destroy', user, confirm: 'Are you sure?', method: :delete %>
但是当我在 *_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
【问题讨论】:
-
要检查两件事:1. 'fog' gem 是否列在您的 Gemfile 中? 2. 如果是,请尝试在初始化程序中指定 config.storage = :fog。
-
@CanBerkGüder 我更新了初始化程序,但仍然没有成功,请参阅更新部分了解更新后发生的情况。
-
目录结构并不重要,正如文档所说,仅仅是因为 S3 上没有目录(它只是一个名为 uploads/user/avatar/1/2_48x48.png 的文件)。不幸的是,我想不出第三个可能导致您看到的错误的原因。
标签: ruby-on-rails carrierwave fog