howto:载波的基本设置[Heroku和S3] [英] howto: Basic setup of carrierwave [Heroku and S3]

查看:79
本文介绍了howto:载波的基本设置[Heroku和S3]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于生产和开发的S3存储桶。我已经完成了研究,看到了这篇文章,但是我当前的配置不能按预期工作。我在本地收到以下异常(如下),并且没有从heroku应用程序将文件上传到S3存储桶:

I have an S3 bucket for production and development. I have done my research and came across this post but my current config does not work as expected. I get the following exception (below) locally and I get no file uploads to my S3 bucket from my heroku app:

 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 文件中,一切正常在本地运行。

However When I set storage :file inside of the *_uploader.rb file everything works as expected locally. But still noting ever gets sent to my S3 bucket.

这是我的设置:

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日志就会吐出:

UPDATE Thanks to @CanBerkGüder I can confirm that I am able to save the record but not the image file. Anytime I attempt to create a user object, my heroku log spits out:

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 。从理论上讲,两者之间应该没有区别,但是我认为值得一试。这是我在Heroku上运行的实时应用程序中的CarrierWave初始化程序:

OK, here's an idea. CarrierWave still includes an S3 adapter for backward compatibility that uses fog underneath, which I personally use instead of :fog. In theory, there should be no difference between the two, but I think it's worth a shot. Here's my CarrierWave initializer from a live application running on Heroku:

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的只读文件系统,该文件系统应该与您的问题无关。

The first two lines (config.root and config.cache_dir) are there to work around Heroku's read-only filesystem, which should have nothing to do with your problem.

这篇关于howto:载波的基本设置[Heroku和S3]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆