Carrierwave,Minimagick图像上传到S3无法正常工作

时间:2016-01-06 05:23:20

标签: amazon-s3 carrierwave image-uploading minimagick

我想保存多个版本的图片。所以我正在关注并自定义uploading-images-with-carrierwave-to-s3-on-rails,但表单不允许我的图像参数,但我不知道为什么。 我的 模型

class Attachment < ActiveRecord::Base
    mount_uploader :img, S3uploaderUploader
end

我的 表单 看起来像这样。

<%= form_tag(img_upload_create_path, { multipart: true, method: "POST"}) do %>
  <div class="fileupload btn btn-default">                  
    <span>file open</span>
    <%= file_field_tag 'user_pic[]', multiple: true, accept:'image/png,image/gif,image/jpeg', class: "pictures btn btn-success" %>
  </div>
  <%= submit_tag "upload", :class => "btn btn-success btn-lg" %>
  </div>  
<% end %>

我的 控制器

class ImgUploadController < ApplicationController
  def create
    params[:user_pic].each do |pic|
      Attachment.create(
            img: pic
      )
    end
    redirect_to :back
  end 
end

我的 上传者,

class S3uploaderUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick      
  storage :fog

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
  def cache_dir
    '/tmp/cache/s3file'
  end
  def extension_white_list
    %w(jpg jpeg png gif)
  end
  #version1
  version :detailVertical do
    process :quality => 50
  end
  #version2
  version :detailHorizontal do
    process :quality => 50
  end
  #version3
  version :mainVertical, :from_version => :detailVertical do
    process :resize_to_fit => [240, 320]
    process :quality => 100
  end
  #version4
  version :mainHorizontal, :from_version => :detailHorizontal do
    process :resize_to_fit => [240, 180]
    process :quality => 100
  end
end

我的 config / initializers / carrierwave.rb

module CarrierWave
  module MiniMagick
    def quality(percentage)
      manipulate! do |img|
        img.quality(percentage.to_s)
        img = yield(img) if block_given?
        img
      end
    end
  end
end

当我提交图片时,我在这里添加了日志。

Processing by ImgUploadController#create as HTML
  Parameters: {"utf8"=>"✓", 

"authenticity_token"=>"c6z+PUcBk59+ig/SUMcRrkxwFbTcA
rKnZCMu0ag0Rmp3JQzvXLWxbQ2SF7f/7VazG8Iwcxy5CWQ+nREPVJ10Jw==",
"user_pic"=>[#<ActionDispatch::Http::UploadedFile:0x007facb8033df8 
@tempfile=#<Tempfile:/home/ubuntu/workspace/RackMultipart20160106-5826-109snyd.jpg>, @original_filename="yhouse7.jpg", 
@content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user_pic[]\"; filename=\"yhouse7.jpg\"\r\nContent-Type: image/jpeg\r\n">,
 #<ActionDispatch::Http::UploadedFile:0x007facb8033dd0 @tempfile=#
<Tempfile:/home/ubuntu/workspace/RackMultipart20160106-5826-d9ni1l.jpg>, @original_filename="yhouse7-2.jpg", @content_type="image/jpeg", 
@headers="Content-Disposition: form-data; name=\"user_pic[]\"; 
filename=\"yhouse7-2.jpg\"\r\nContent-Type: image/jpeg\r\n">], 
"commit"=>"upload"}

1 个答案:

答案 0 :(得分:0)

好的,经过长途旅行找到解决方案后,我做到了。我没有安装imagemagick。 我刚下载了

gem 'carrierwave', github: 'carrierwaveuploader/carrierwave'
gem "fog-aws"
gem 'mini_magick'
那三个宝石。请使用

下载imagemagick
sudo apt-get -y install imagemagick

开始使用Minimagick

之前