5

我有一个用于生产和开发的 S3 存储桶。我已经完成了研究并看到了这篇文章,但我当前的配置没有按预期工作。我在本地收到以下异常(如下),并且没有从我的 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 %>

但是,当我storage :file在文件内部设置时,*_uploader.rb一切都按预期在本地工作。但仍然注意到曾经被发送到我的 S3 存储桶。

这是我的设置:

用户.rb

class User < ActiveRecord::Base
 attr_accessible :name, :avatar, :avatar_cache, :remote_avatar_url, :remove_avatar
 mount_uploader :avatar, AvatarUploader
end

雾.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
4

2 回答 2

1

好的,这是一个想法。CarrierWave 仍然包含一个用于向后兼容的 S3 适配器,它在下面使用雾,我个人使用它而不是: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 的只读文件系统,这与的问题无关。

于 2012-02-20T22:35:04.300 回答
0

一个问题可能是

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
于 2012-02-24T15:42:37.770 回答