【问题标题】:How to upload an image to S3 using paperclip gem如何使用回形针 gem 将图像上传到 S3
【发布时间】:2012-03-29 20:44:49
【问题描述】:

对于我的生活,我无法理解基本的paperclip example 是如何工作的。控制器中只有一条线,那就是

@user = User.create( params[:user] )

我只是不明白如何将图像上传到 s3。我已经对示例进行了相当大的更改,因为我想使用 jquery file uploader 而不是默认的 rails 表单助手,所以我正处于将图像发布到控制器的位置,但我不知道如何我应该从参数中获取图像并将其分配为附件。这是我看到的日志:

Parameters: {"files"=>[#<ActionDispatch::Http::UploadedFile:0x132263b98 @tempfile=#<File:/var/folders/5d/6r3qnvmx0754lr5t13_y1vd80000gn/T/RackMultipart20120329-71039-1b1ewde-0>, @headers="Content-Disposition: form-data; name=\"files[]\"; filename=\"background.png\"\r\nContent-Type: image/png\r\n", @content_type="image/png", @original_filename="background.png">], "id"=>"385"}

我的JS很简单:

 ` $('#fileupload').fileupload({
    dataType: 'json',
    url: '/my_url',
    done: function (e, data) {
        console.log('done');
    }
});`

对我有帮助的是如何从上面给出的 POSTed 参数中剥离文件数据并将其传递给回形针。我确定我必须为附件属性分配 File.open(...) 的值,但我不知道我的文件的来源是什么。

我花了很多时间试图弄清楚这一点,但我似乎无法理解。我试过直接上传到 s3,但事件链非常混乱,所以我想先完成这个简单的传递示例。非常感谢您提供的任何帮助!

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 paperclip


    【解决方案1】:

    你需要更多的部分,如果你能显示你正在使用的确切代码会有所帮助。

    Paperclip 可以通过以下方式发布到 S3:

    http://rubydoc.info/gems/paperclip/Paperclip/Storage/S3

    当您的控制器创建用户模型时,它会发送所有参数。这称为“批量分配”(请务必阅读 attr_accessible)。

    当您的模型收到参数时,它会使用 Paperclip AWS 处理器来上传参数。

    您需要 AWS gem、S3 上的有效存储桶和配置文件。

    试试这篇博文,如果它对您有帮助,请告诉我们:

    http://blog.trydionel.com/2009/11/08/using-paperclip-with-amazon-s3/

    2013-04-03 更新:请参阅下面 Chloe 的评论 - 您可能需要一个额外的参数,并且博客文章可能已过时。

    【讨论】:

    • 我明白这一点,但我不明白为什么我们可以期望 params 包含 Paperclip 期望的所有属性。除非表单助手正在处理这个问题,在这种情况下我理解,但我需要找到一种解决方法,因为不会使用表单助手。用一些 JS 编辑了 OP。谢谢
    • 我的建议是让你在没有 jQuery 的情况下让它像演示所示的那样工作。当它成功运行时,然后添加 jQuery。这种方法可以让您更轻松地查看逐步进行的工作。
    • 博文不好。可能已经过时了。按照步骤报错。 Paperclip RubyDoc 中没有提到的一件事是将:storage => :s3 添加到 has_attached_file。他们听起来好像:s3_credentials 就足够了,但事实并非如此。
    • 谢谢克洛伊,谢谢你! (随意编辑整个答案)
    【解决方案2】:

    如果你想手动操作,可以这样处理:

    # In order to get contents of the POST request with the photo,
    # you need to read contents of request
    upload = params[:file].is_a(String)
    file_name = upload ? params[:file] : params[:file].original_filename
    extension = file_name.split('.').last
    
    # We have to create a temp file which is going to be used by Paperclip for
    # its upload
    tmp_file = "#{Rails.root}/tmp/file.#{extension}"
    file_id = 0
    
    # Check if file with the name exists and generate unique path
    while File.exists?(tmp_file) do
      tmp_file_path = "#{Rails.root}/tmp/file#{file_id}.#{extension}"
      id += 1
    end
    
    # Let's write the file from post request to unique location
    File.open(tmp_file_path, 'wb') do |f|
      if upload
        f.write request.body.read
      else
        f.write params[:file].read
      end
    end
    
    # Now that file is saved in temp location, we can use Paperclip to mimic one file
    # upload
    @photo = Photo.new :photo => File.open(tmp_file_path)
    
    # We'll return javascript to say that the file is uploaded and put its thumbnail in
    # HTML or whatever else you wanted to do with it
    respond_to do |format|
      if @photo.save
        render :text => "Success"
      else
        render :text => @photo.errors
      end
    end
    

    您可以重写您的create 或任何您用作发布表单的网址的网址。

    【讨论】:

      【解决方案3】:

      这一点:

      "files"=>[#<ActionDispatch::Http::UploadedFile:0x132263b98 @tempfile=#     <File:/var/folders/5d/6r3qnvmx0754lr5t13_y1vd80000gn/T/RackMultipart20120329-71039-1b1ewde-0>
      

      是(我认为)保存表单中发布的文件内容的部分。

      在 Rails 中,User 模型将有一个帮助器:has_attached_file

      将 [:params] 传递给 User.create 方法允许助手获取文件内容,对它们进行任何处理(例如,根据提供给助手的属性调整大小等),然后推送图像到您的存储(例如 S3 或其他 - S3 凭据被传递给助手)。

      希望这能解释“它是如何做到的?”问题

      re jQuery bit.. 不确定代码应该在那里,但为什么不使用带有 :remote => true 的 Rails 表单并在 jquery 中处理响应?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-09
        • 1970-01-01
        • 1970-01-01
        • 2012-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多