繁体   English   中英

使用Paperclip将URL提取的图像保存到Amazon S3

[英]Save fetched image from URL using Paperclip to Amazon S3

我有一个要抓取以将帖子保存到Post模型的JSON代码段

看起来像这样:

{
 "provider_url": "http://twitter.com", 
 "description": "Four more years. pic.twitter.com/bAJE6Vom", 
 "title": "Twitter / BarackObama: Four more years. http://t.co/bAJE6Vom", 
 "author_name": "BarackObama", "height": 532, "thumbnail_width": 150, "width": 800, 
"thumbnail_url": "https://pbs.twimg.com/media/A7EiDWcCYAAZT1D.jpg:thumb", 
 "author_url": "http://twitter.com/BarackObama", "version": "1.0", 
"url": "https://pbs.twimg.com/media/A7EiDWcCYAAZT1D.jpg:large", 
"provider_name": "Twitter", "type": "photo", "thumbnail_height": 150
 }

要保存的图像是提供者提供的原始照片URL。

例如: https//pbs.twimg.com/media/A7EiDWcCYAAZT1D.jpg

如果可能的话,我想使用Paperclip将图像归档到我自己的Amazon S3存储中。

我还有一个名为backup_s3_urlPost列,我想在其中存储由S3托管的上传/复制的图像URL。

我仍然尝试使用open-uri ...但它仍在获取原始图像URL。 我想使用回形针直接从URL保存图像。

def picture_from_url(url)
  self.photo = URI.parse(url)
end

但是上面的代码不起作用,仅供参考。

任何解决方法将不胜感激。

将json图片作为base64编码的字符串发送。
在Rails上对此进行解码,并使用回形针保存到S3。

对于我的iOS应用程序,我将base64字符串发送到Rails模型上的虚拟属性,并使用“ before_save”将真正的S3属性值设置为解码后的字符串。

class Media1 < ActiveRecord::Base
  before_save :set_custom_photo

  attr_accessor :phototmp


  has_attached_file :photo, 
    :styles => 
      { :thumb => '150x150#',
        :small => '100x100>',
        :large => '600x600>'},
     :storage => :s3,
     :s3_credentials => "#{Rails.root}/config/s3.yml",
     :s3_protocol => "https",
     :path => ":class/:id/:basename_:style.:extension",
     :url  => ":s3_eu_url"  


  validates_attachment_size :photo, :less_than => 5.megabytes
  validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']


   def set_custom_photo
      data = StringIO.new(Base64.decode64(self.phototmp))
      data.class.class_eval { attr_accessor :original_filename, :content_type }
      data.original_filename = "mapimage.png"
      data.content_type = "image/png"

      self.photo = data
   end


end

这些资源可能会有所帮助:

使用回形针从网址上传: https : //stackoverflow.com/a/4050758/2463468

使用回形针保存到s3: https : //github.com/thoughtbot/paperclip/wiki/Paperclip-with-Amazon-S3

暂无
暂无

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

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