Paperclip 用于将图像上传到 Rails 中的 S3。文件上传速度非常慢。解决方法?

发布于 2024-09-18 04:31:02 字数 337 浏览 3 评论 0原文

我正在开发一个 Rails 应用程序,用户将在其中上传大量图像。

我当前的设置:使用带有 S3 存储的 Paperclip 插件使用 SWFUpload 一次上传多个文件。原始图像上传到S3后,Delayed_Job用于后期处理(缩略图等)。

我遇到的问题是图像上传速度非常慢。我假设默认的回形针设置是图像将从用户转到 ->我的服务器到-> s3。

我想我可以将图像直接上传到 s3,但我不确定如何使用 Paperclip 和后期处理来实现这一点。我找不到任何涉及此问题的插件或示例。

有人有建议吗?如果没有,你能指出我正确的方向吗?

提前致谢!

蒂姆

I'm working on a rails app where the user will be uploading large quantities of images.

My current setup: Using SWFUpload to upload multiple files at once using the Paperclip plugin with S3 storage. After the original image is uploaded to S3, Delayed_Job is used for the post processing (thumbnails, etc).

The problem I have is that the images upload at a very slow rate. I'm assuming the default Paperclip setup is that the image will go from the user to -> my server to -> s3.

I was thinking that I can have the images upload directly to s3 but I'm not sure how to implement that with Paperclip and post processing. I couldn't find any plugins or examples dealing with this.

Does anyone have suggestions? If not, can you point me in the right direction?

Thanks in advance!

Tim

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云巢 2024-09-25 04:31:03

我已经遇到过同样的问题好几次了。我解决这个问题的方法是创建 2 个模型,一个 Image 模型和一个 TempImage 模型,它继承自 Image 模型。这要求您的 Image 表上有一个 type 列。 TempImage 模型将图像保存在本地,然后当您直接从 Image 模型访问它并重新保存它时,它将遵循 Image 中定义的内容代码>模型,是Amazon S3。

示例:

# Will save in the database as a TempImage inside the Image table
temp = TempImage.create(:asset => File.new('some_path', 'r'))

# When you find it again through the Image model, it bypasses the type column
# so next time you save it, it is saved as an Image.
amazon = Image.find(temp.id)
amazon.save!

这是我延迟的工作:

class MoveToS3Job < Struct.new(:temp_revision_id)
  def perform
    upload = Image.find(temp_revision_id)
    temp_path = File.expand_path("tmp/uploads/#{upload.asset_file_name}", Rails.root)
    upload.asset = File.new(temp_path, 'r')
    upload.save!

    if File.exists?(temp_path) && !File.directory?(temp_path)
      File.delete(temp_path)
    end
  rescue ActiveRecord::RecordNotFound
    # If the record wasn't found, do some sort of
    # error report, but don't keep it in the queue.
  end
end

这是 TempImage 模型:

class TempImage < Image
  has_attached_file :asset, {
    :path => ":rails_root/tmp/uploads/:basename_:updated_at.:extension"
  }
end

然后是原始 Image 模型:

class Image < ActiveRecord::Base
  # Validations
  validates :asset, :presence => true

  # Paperclip
  has_attached_file :asset, :styles => {
    :preview => ['100x100#', :png],
    :thumb => ['50x50#', :png]
  },
  :default_style => :thumb,
  :storage => :s3,
  :bucket => 'bucket-name',
  :s3_credentials => File.expand_path('config/s3.yml', Rails.root),
  :path => "photos/:id_partition/:style.:extension"
end

您的原始 Image 模型应始终包含您的帖子处理,因为这将在后台完成。

您始终可以覆盖一些方法以使其更简洁,但这可以让您更好地了解它是如何工作的以及您需要做什么,以便您可以让它像您希望的那样工作。

I've ran into this same problem a few times. The way I solved it was by creating 2 models, a Image model and a TempImage model, which inherits from the Image model. This requires you to have a type column on your Image table. The TempImage model saves the image locally, then when you access it from the Image model directly and resave it, it will follow whatever is defined in the Image model, being Amazon S3.

Example:

# Will save in the database as a TempImage inside the Image table
temp = TempImage.create(:asset => File.new('some_path', 'r'))

# When you find it again through the Image model, it bypasses the type column
# so next time you save it, it is saved as an Image.
amazon = Image.find(temp.id)
amazon.save!

Here is my delayed job:

class MoveToS3Job < Struct.new(:temp_revision_id)
  def perform
    upload = Image.find(temp_revision_id)
    temp_path = File.expand_path("tmp/uploads/#{upload.asset_file_name}", Rails.root)
    upload.asset = File.new(temp_path, 'r')
    upload.save!

    if File.exists?(temp_path) && !File.directory?(temp_path)
      File.delete(temp_path)
    end
  rescue ActiveRecord::RecordNotFound
    # If the record wasn't found, do some sort of
    # error report, but don't keep it in the queue.
  end
end

Here is the TempImage model:

class TempImage < Image
  has_attached_file :asset, {
    :path => ":rails_root/tmp/uploads/:basename_:updated_at.:extension"
  }
end

Then the original Image model:

class Image < ActiveRecord::Base
  # Validations
  validates :asset, :presence => true

  # Paperclip
  has_attached_file :asset, :styles => {
    :preview => ['100x100#', :png],
    :thumb => ['50x50#', :png]
  },
  :default_style => :thumb,
  :storage => :s3,
  :bucket => 'bucket-name',
  :s3_credentials => File.expand_path('config/s3.yml', Rails.root),
  :path => "photos/:id_partition/:style.:extension"
end

Your original Image model should always contain your post processing, as that will be done in the background.

You can always overwrite some methods to make it a little cleaner, but this gives you a better idea of how it works and what you need to do to so you can have it work like you want it to.

凉薄对峙 2024-09-25 04:31:03

如果您最终选择直接上传到 S3,这会减轻 Rails 服务器上的工作负担,请查看我的示例项目:

使用 Rails 3、Flash 和基于 MooTools 的 FancyUploader 直接上传到 S3 的示例项目:https://github.com/iwasrobbed/Rails3-S3-Uploader-FancyUploader

使用 Rails 的示例项目3、Flash/Silverlight/GoogleGears/BrowserPlus和基于jQuery的Plupload直接上传到S3:https ://github.com/iwasrobbed/Rails3-S3-Uploader-Plupload

顺便说一句,您可以使用 Paperclip 进行后期处理,使用如下博客文章所述:

http://www.railstoolkit.com/posts/fancyupload-amazon-s3-uploader-with-paperclip

If you end up going the route of uploading directly to S3 which offloads the work from your Rails server, please check out my sample projects:

Sample project using Rails 3, Flash and MooTools-based FancyUploader to upload directly to S3: https://github.com/iwasrobbed/Rails3-S3-Uploader-FancyUploader

Sample project using Rails 3, Flash/Silverlight/GoogleGears/BrowserPlus and jQuery-based Plupload to upload directly to S3: https://github.com/iwasrobbed/Rails3-S3-Uploader-Plupload

By the way, you can do post-processing with Paperclip using something like this blog post describes:

http://www.railstoolkit.com/posts/fancyupload-amazon-s3-uploader-with-paperclip

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文