Paperclip:样式选项不适用于s3_direct_upload

时间:2014-04-24 03:13:50

标签: ruby-on-rails ruby-on-rails-4 amazon-s3 paperclip

刚刚添加了使用gem s3_direct_upload直接上传到Amazon S3的功能。

不幸的是,现在它没有处理我喜欢的额外图像大小调整。

:styles => { :thumbnail => "200x200#" }

查看文档后,我找不到任何涉及此问题的内容。我还使用此tutorial来正确设置每个设置。如何将图像样式与direct_upload一起使用到S3 ??

LOG

Processing by UpdatesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"fmxSfjg53jcouwmF/fdsoqxmDuq84=", "update"=>{"description"=>"", "direct_upload_url"=>"https://my-bucket.s3.amazonaws.com/uploads%2F1398310463606-c68nyl5gvgkqpvi-3708f19268a140853f118214d98924f8%2Fretro.png", "image_file_name"=>"retro.png", "image_file_size"=>"107715", "image_content_type"=>"image/png", "image_file_path"=>"/uploads%2F1398310463606-c68nyl5gvgkqpvi-3708f19268a140853f118214d98924f8%2Fretro.png"}, "commit"=>"Save changes"}
User Load (1.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 29 ORDER BY "users"."id" ASC LIMIT 1

(0.3ms)  BEGIN
Update Exists (0.5ms)  SELECT 1 AS one FROM "updates" WHERE "updates"."id" = 730128 ORDER BY created_at DESC LIMIT 1
SQL (12.2ms)  INSERT INTO "updates" ("created_at", "description", "direct_upload_url", "id", "image_content_type", "image_file_name", "image_file_path", "image_file_size", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id"  [["created_at", Wed, 23 Apr 2014 22:34:29 CDT -05:00], ["description", ""], ["direct_upload_url", "https://my-bucket.s3.amazonaws.com/uploads%2F1398310463606-c68nyl5gvgkqpvi-3708f19268a140853f118214d98924f8%2Fretro.png"], ["id", 730128], ["image_content_type", "image/png"], ["image_file_name", "retro.png"], ["image_file_path", "/uploads%2F1398310463606-c68nyl5gvgkqpvi-3708f19268a140853f118214d98924f8%2Fretro.png"], ["image_file_size", 107715], ["updated_at", Wed, 23 Apr 2014 22:34:29 CDT -05:00], ["user_id", 29]]

Digest::Digest is deprecated; use Digest
[AWS S3 300 .5453 0 retries] copy_object(:bucket_name=>"my-bucket",:copy_source=>"my-bucket/uploads/1398310463606-c68nyl5gvgkqpvi-3708f19268a140853f118214d98924f8/retro.png",:key=>"updates/images/730128/original/retro.png",:metadata_directive=>"COPY",:storage_class=>"STANDARD")  

Digest::Digest is deprecated; use Digest
[AWS S3 543 0.0493 0 retries] delete_object(:bucket_name=>"my-bucket",:key=>"uploads/1398310463606-c68nyl5gvgkqpvi-3708f19268a140853f118214d98924f8/retro.png")

模型

class Update < ActiveRecord::Base
  ...
  ...
  has_attached_file :image, :s3_protocol => :https,
                            :storage => :s3,
                            :styles => { 
                              :profile => "550x330#" },
                            :convert_options => {
                              :thumb => "-quality 75 -strip" },
                            :s3_credentials => "#{Rails.root}/config/aws.yml",
                            :path => ":class/:attachment/:id/:style/:filename",
                            :url => ":s3_domain_url"
  validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/

  def self.copy_and_delete(paperclip_file_path, raw_source)
    s3 = AWS::S3.new #create new s3 object
    destination = s3.buckets['bucket-name'].objects[paperclip_file_path]
    sub_source = CGI.unescape(raw_source)
    sub_source.slice!(0) # the attached_file_file_path ends up adding an extra "/" in the beginning. We've removed this.
    source = s3.buckets['bucket-name'].objects["#{sub_source}"]
    source.copy_to(destination) #copy_to is a method originating from the aws-sdk gem. 
    source.delete #delete temp file.
  end

控制器

def create
  if(params[:url])
    @update = Update.new
    render "new" and return
  end

  if(params[:update][:image_file_path])
    @update = current_user.updates.create(update_params)

    respond_to do |format|
      if @update.save
        #we want a destination(paperclip_file_path) and a source(raw_source)
        paperclip_file_path = "updates/images/#{@update.id}/original/#{params[:update][:image_file_name]}"
        raw_source = params[:update][:image_file_path]

        Update.copy_and_delete paperclip_file_path, raw_source #this is where we call a method to copy from temp location to where paperclip expects it to be.
        format.html { redirect_to update_path(:id => @update.id), :flash => { :success => "<strong>Awesome!</strong> You've successfully created your update.".html_safe }}
        format.json { render action: 'show', status: :created, location: @update }
      else
        format.html { render action: 'new' }
        format.json { render json: @update.errors, status: :unprocessable_entity }
      end
    end
  else
    @update = Update.new
    render action: "new", notice: "No file"
  end
end

2 个答案:

答案 0 :(得分:1)

我在共享代码中看到了几个问题。

首先,你说

  

不幸的是,现在它没有处理我喜欢的额外图像大小调整。

     

:styles =&gt; {:thumbnail =&gt; “200x200#”}

我在thumbnail的实际代码中看不到has_attached_file样式的任何引用

has_attached_file :image, :s3_protocol => :https,
                            :storage => :s3,
                            :styles => { 
                              :profile => "550x330#" },
                            :convert_options => {
                              :thumb => "-quality 75 -strip" },
                            :s3_credentials => "#{Rails.root}/config/aws.yml",
                            :path => ":class/:attachment/:id/:style/:filename",
                            :url => ":s3_domain_url"

我只看到一个定义的样式profile:styles => {:profile => "550x330#" } 它应该包含thumbnail

例如::styles => { :thumbnail => "200x200#", :profile => "550x330#" }

接下来,我看到的另一个问题是convert_options,目前设置为:

:convert_options => { :thumb => "-quality 75 -strip" }

并且您没有将任何样式定义为:thumb。从技术上讲,它应该是:profile:thumbnail,或者如果你想将它应用于这两种风格,那么:all

例如:

:convert_options => { :thumbnail => "-quality 75 -strip" }

-OR -

:convert_options => { :profile => "-quality 75 -strip" } 

-OR -

:convert_options => { :profile => "-quality 85 -strip", :thumbnail => "-quality 75 -strip"  } 

-OR -

:convert_options => { :all => "-quality 75 -strip" } 

以下是has_attached_file应如何显示的示例:

has_attached_file :image, :s3_protocol => :https,
                            :storage => :s3,
                            :styles => { 
                              :thumbnail => "200x200#", 
                              :profile => "550x330#" },
                            :convert_options => {
                              :thumbnail => "-quality 75 -strip", 
                              :profile => "-quality 85 -strip" },
                            :s3_credentials => "#{Rails.root}/config/aws.yml",
                            :path => ":class/:attachment/:id/:style/:filename",
                            :url => ":s3_domain_url"

答案 1 :(得分:1)

试试这些代码                 <%= image_tag @update.image_url, size:"100x100" %>

或                 <%= image_tag @language.image_url, width: 100, height: 100 %>


                 <%= image_tag(@language.image_url), :style=>'width:100px; height:100px;'%> 并在您的模态中编辑为

 class YourModel < ActiveRecord::Base

  has_attached_file :picture, :styles => { :medium => "117x245>", :thumb => "100x100>" }
  validates_attachment_content_type :picture, :content_type => /\Aimage\/.*\Z/

end