【问题标题】:Jquery Direct File Upload to S3 using PaperclipJquery 使用 Paperclip 将文件直接上传到 S3
【发布时间】:2014-08-11 20:09:09
【问题描述】:

我正在尝试使用此 Tutorial. 使用 Paperclip、Jquery 和 Rails 3.0 将书籍页面直接上传到 AmazonS3

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

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

由于某种原因,在我上传图书页面后,我收到此错误:

错误

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

我不确定为什么会一直这样。如果您需要我发布其他内容,请告诉我。

JS.COFFEE

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

【问题讨论】:

    标签: ruby-on-rails-3 amazon-s3 coffeescript paperclip jquery-file-upload


    【解决方案1】:

    我知道这有点过时了,我相信你已经解决了这个问题,但马上,我看到你正在使用 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 设为该表上的实际列,因为它不会在多个请求中持续存在。虽然这可能不是必需的,但它也是视图内部的一个很好的指标,如果该值存在,它还没有被上传和处理。

    【讨论】:

      猜你喜欢
      • 2011-06-29
      • 2014-10-23
      • 1970-01-01
      • 2012-02-22
      • 1970-01-01
      • 2012-09-11
      • 1970-01-01
      • 1970-01-01
      • 2013-06-03
      相关资源
      最近更新 更多