处理S3(rails)上图像的最佳方法

时间:2013-08-15 15:30:34

标签: ruby-on-rails-3 image amazon-s3

我在Amazon S3上有一组图像,我想自动生成缩略图,以便在我的网站上投放。

我考虑过Cloudinary,但似乎我必须首先将所有图像复制到Cloudinary服务器。我想让他们继续使用S3。

我考虑过Dragonfly,但似乎Dragonfly只适用于我安装Dragonfly后上传的文件。我已经上传了所有文件。

对我来说什么是好的解决方案?我在Rails环境中(rails 3.2)。

谢谢!

2 个答案:

答案 0 :(得分:0)

如果它只是一组“图像”,那么它的结构并不是那么好。您最好重新组织存储和管理图像的方式。

尝试Paperclip

class ModelName < ActiveRecord::Base

  attr_accessible :image #more here
  has_attached_file :image, :styles => { :large => "450x450>", :medium => "300x300>", :thumb => "150x150>" },
    :storage => :s3,
    :s3_credentials => "#{Rails.root}/config/s3.yml",
    :path => "people/:style/:id/:filename",
    :s3_protocol => "https"

   def original_image_url
     image.url
   end

   def large_image_url
     image.url(:large)
   end

   def medium_image_url
     image.url(:medium)
   end

   def small_image_url
     image.url(:thumb)
   end

 #etc

 end

然后只需通过控制台将现有图像分配给现有实例:

require 'uri'
@instance.image = open(URI::encode(url))
@instance.save
# s3 will now contain the images in the appropriate sizes in the format specified above.

由于原件也会被保存,我建议你先删除s3上的'图像集',否则你会复制它们。

答案 1 :(得分:0)

我对蜻蜓错了。您可以在已上传的文件上使用Dragonfly。我在我的项目中使用它并且效果很好。