繁体   English   中英

使用Paperclip将jQuery直接文件上传到S3

[英]Jquery Direct File Upload to S3 using Paperclip

我正在尝试使用此教程使用Paperclip,Jquery和Rails 3.0将书页直接上传到AmazonS3

到目前为止,我已经设置了数据库,我的config / initializers文件和CORS。 我也在使用这些宝石:

gem 'aws-sdk'
gem 'paperclip' 
gem 's3_direct_upload'

出于某种原因,在我上载“图书页面”后,出现以下错误:

错误

NoMethodError (undefined method `[]' for nil:NilClass):

我不确定为什么这种情况持续发生。 让我知道您是否需要我发布其他任何内容。

咖啡

jQuery ->
  $('#new_book_page').fileupload
    dataType: "script"
    add: (e, data) ->
      types = /(\.|\/)(gif|jpe?g|png)$/i
      file = data.files[0]
      if types.test(file.type) || types.test(file.name)
        data.context = $(tmpl("template-upload", file))
        $('#new_book_page').append(data.context)
        data.submit()
      else
        alert("#{file.name} is not a gif, jpeg, or png image file")
    progress: (e, data) ->
      if data.context
        progress = parseInt(data.loaded / data.total * 100, 10)
        data.context.find('.bar').css('width', progress + '%')

$ ->
  $("#s3_uploader").S3Uploader
    remove_completed_progress_bar: false
    progress_bar_target: $("#uploads_container")

  $("#s3_uploader").bind "s3_upload_failed", (e, content) ->
    alert content.filename + " failed to upload. Error: " + content.error_thrown

  return

楷模

class BookPage < ActiveRecord::Base
  attr_accessible :caption, :book_id, :page, :direct_upload_url, :page_file_path,
              :page_content_type, :page_file_size, :page_file_name, :page_updated_at

  DIRECT_UPLOAD_URL_FORMAT = %r{\Ahttps:\/\/s3\.amazonaws\.com\/bucketname#{!Rails.env.production? ? "\\-#{Rails.env}" : ''}\/(?<path>pages\/.+\/(?<filename>.+))\z}.freeze

  belongs_to :books

  has_attached_file :page, 
                :styles => { :small => "200x200>" },
                :storage => :s3,
                :bucket => 'bucketname',
                :s3_credentials => {
                  :access_key_id => 'XXXXXX',
                  :secret_access_key => 'XXXXXX',
                }

  before_create :set_page_attributes

  def direct_upload_url=(escaped_url)
    write_attribute(:direct_upload_url, (CGI.unescape(escaped_url) rescue nil))
  end

  def post_process_required?
    %r{^(image|(x-)?application)/(bmp|gif|jpeg|jpg|pjpeg|png|x-png)$}.match(page_content_type).present?
  end

  protected

  def set_page_attributes
    tries ||= 5
    direct_upload_url_data = DIRECT_UPLOAD_URL_FORMAT.match(direct_upload_url)
    s3 = AWS::S3.new

    ###THE ERROR IS GENERATED HERE
    direct_upload_head = s3.buckets[Rails.configuration.aws[:bucket]].objects[direct_upload_url_data[:path]].head

    self.page_file_name     = direct_upload_url_data[:filename]
    self.page_file_size     = direct_upload_head.content_length
    self.page_content_type  = direct_upload_head.content_type
    self.page_updated_at    = direct_upload_head.last_modified
  rescue AWS::S3::Errors::NoSuchKey => e
    tries -= 1
    if tries > 0
      sleep(3)
      retry
    else
      false
    end
  end

ends

意见

###When I submit this I get my COFFEE error
<%= s3_uploader_form callback_url:([book_book_pages_url]),
  id: "s3_uploader",
  acl: "private",
  callback_param: " book_page[direct_upload_url]" do %>
  <%= file_field_tag :page, multiple: false %>
<% end %>

###If I submit here, I can see my LOGS which state the error above
<%= form_for ([@book, BookPage.new]) do |f| %>

    <%= render 'shared/error_messages', object: f.object %>

    <%= f.file_field :page, multiple: true %>

    <%= f.hidden_field :direct_upload_url %>

    <%= f.hidden_field :page_file_name %>
    <%= f.hidden_field :page_file_size %>
    <%= f.hidden_field :page_content_type %>

    <%= f.hidden_field :page_file_path %>

    <%= f.submit "Upload" %>
<% end %>

<div id="uploads_container"></div>

<script id="template-upload" type="text/x-tmpl">
<div id="page_{%=o.unique_id%}" class="upload">
  <h5>{%=o.name%}</h5>
   <div class="progress progress-striped active"><div class="bar" style="width: 0%"></div></div>
</div>
</script>

路线

resources :books do
  resources :book_pages
end

日志

Started POST "/books/1732/book_pages" for 127.0.0.1 at 2014-06-20 19:38:31 -0700

Processing by BookPagesController#create as JS

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"2pzDw3HYOg+pkc6Q8vPHwnAzVsX1dcNNf9COD6x0aI8=", 
    "book_page"=>{"direct_upload_url"=>"", "page_file_name"=>"", "page_file_size"=>"", 
    "page_content_type"=>"", "page_file_path"=>"", 
    "page"=>[#<ActionDispatch::Http::UploadedFile:0x007ffa70e77490 
    @original_filename="Ultimate.jpg", @content_type="image/jpeg", 
    @headers="Content-Disposition: form-data; name=\"book_page[page][]\";
    filename=\"Ultimate.jpg\"\r\nContent-Type: image/jpeg\r\n", 
    @tempfile=#<File:/var/folders/11/4mfc2tgd32bdq1t3y_sq_q580000gq/T/
    RackMultipart20140620-67389-dcy9nj>>]}, "book_id"=>"1732"}

Completed 500 Internal Server Error in 19ms

我知道这有点过时了,我确定您已经解决了这个问题,但是马上就可以看到您正在使用write_attribute(:direct_upload_url, (CGI.unescape(escaped_url) rescue nil))设置一个attr_accessor :direct_upload_url

您应该这样做: @direct_upload_url = (CGI.unescape(escaped_url) rescue nil)

我的猜测是缺少数组是因为write_attribute等同于self["attribute"] = "some value" ,并且没有direct_upload_url列映射。

但是,您可能应该将direct_upload_url设置为该表上的实际列,因为它不会在多个请求中持续存在。 尽管可能没有必要,但它也是视图内部的一个很好的指示,表明该值(如果存在)尚未被上传和处理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM