简体   繁体   English

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

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

I have a JSON snippet that I am fetching to save posts to Post model 我有一个要抓取以将帖子保存到Post模型的JSON代码段

It looks like this: 看起来像这样:

{
 "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
 }

The image being saved is the original photo URL from the provider. 要保存的图像是提供者提供的原始照片URL。

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

I want to archive the image into my own Amazon S3 storage using Paperclip if possible. 如果可能的话,我想使用Paperclip将图像归档到我自己的Amazon S3存储中。

I also have a Post column named backup_s3_url where I want to store my uploaded/duplicated image URL hosted by S3. 我还有一个名为backup_s3_urlPost列,我想在其中存储由S3托管的上传/复制的图像URL。

I tried to use open-uri still... But it's still getting the original image URL. 我仍然尝试使用open-uri ...但它仍在获取原始图像URL。 I want to directly save the image from URL using paperclip. 我想使用回形针直接从URL保存图像。

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

But the code above doesn't work, FYI. 但是上面的代码不起作用,仅供参考。

Any workarounds will be appreciated. 任何解决方法将不胜感激。

Send the json image as a base64 encoded string. 将json图片作为base64编码的字符串发送。
On Rails decode this and save to S3 with paperclip. 在Rails上对此进行解码,并使用回形针保存到S3。

For my iOS app I send the base64 string to a virtual attribute on the Rails model and use "before_save" to set the real S3 attribute value to the decoded string. 对于我的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

Maybe these resources might be helpful: 这些资源可能会有所帮助:

Uploading from a url with paperclip: https://stackoverflow.com/a/4050758/2463468 使用回形针从网址上传: https : //stackoverflow.com/a/4050758/2463468

Save to s3 with paperclip: https://github.com/thoughtbot/paperclip/wiki/Paperclip-with-Amazon-S3 使用回形针保存到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