2014-04-24 21 views
6

Chỉ cần thêm khả năng tải trực tiếp lên Amazon S3 bằng đá quý s3_direct_upload.Kẹp giấy: tùy chọn kiểu không hoạt động với s3_direct_upload

Thật không may, bây giờ nó không xử lý hình ảnh bổ sung thay đổi kích thước mà tôi có như thế này.

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

Sau khi xem tài liệu, tôi không thể tìm thấy bất kỳ điều gì liên quan đến vấn đề này. Tôi cũng đã sử dụng tutorial này để thiết lập chính xác mọi thiết bị. Làm thế nào tôi có thể nhận được các phong cách hình ảnh để làm việc với một 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") 

Mẫu

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 

khiển

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 
+0

Vui lòng đặt một số thông tin nhật ký. Thật khó để tìm một giải pháp mà không có điều này. – lucianosousa

+0

Bạn đang sử dụng phiên bản nào của đá quý 's3_direct_upload'? Lỗi này đã được sửa trong phiên bản 7.1.7 phát hành vào ngày 13 tháng 3. https://github.com/waynehoover/s3_direct_upload/pull/149 –

+0

Agh. bạn có hy vọng của tôi lên. Tôi đang sử dụng 's3_direct_upload (0.1.7)' – tMTboss

Trả lời

1

tôi thấy vài vấn đề trong shar mã ed.

Thứ nhất, bạn nói

Thật không may, bây giờ nó không xử lý hình ảnh bổ sung thay đổi kích thước mà tôi có như thế này.

: phong cách => {: thumbnail => "200x200 #"}

Tôi không thấy bất kỳ tài liệu tham khảo để thumbnail kiểu trong mã thực tế của 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" 

Tất cả tôi xem chỉ là một kiểu được xác định profile:styles => {:profile => "550x330#" } Nó phải bao gồm thumbnail trong đó.

Ví dụ: :styles => { :thumbnail => "200x200#", :profile => "550x330#" }

Tiếp theo, một vấn đề tôi thấy là về convert_options mà hiện nay được thiết lập như:

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

Và bạn không có bất kỳ phong cách định nghĩa là :thumb.Về mặt kỹ thuật, nó cần phải có được một trong hai cho :profile hoặc cho :thumbnail hoặc nếu bạn muốn áp dụng nó cho cả hai phong cách sau đó :all

Ví dụ:

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

-HOẶC-

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

-Hoặc -

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

-OR-

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

Dưới đây là một ví dụ về cách has_attached_file nên hình như:

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" 
+0

Thật không may, đây không phải là vấn đề. Trong đoạn ban đầu của tôi, tôi chỉ sử dụng 'thumbnail' làm ví dụ. Tôi thậm chí đã xóa 'convert_options' được chọn và vẫn còn, chỉ hình ảnh gốc mới được tải lên chứ không phải hình ảnh gốc. – tMTboss

1

thử các mã <%= image_tag @update.image_url, size:"100x100" %>

hoặc <%= image_tag @language.image_url, width: 100, height: 100 %>

hoặc
<%= image_tag(@language.image_url), :style=>'width:100px; height:100px;'%> và chỉnh sửa theo phương thức của bạn là

class YourModel < ActiveRecord::Base 

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

end 
Các vấn đề liên quan