Paperclip S3 download remote images - ruby-on-rails

How I can download a remote image (http protocol, the url is in the image_remote_url attribute) and save it as an attachment to S3 via Paperclip ?
class Product < ActiveRecord::Base
require 'open-uri'
attr_accessor :image_remote_url
has_attached_file :photo,
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":class/:id/:style.:extension",
:bucket => "my_bucket",
:styles => {
:icon => "32x32#",
}
def fetch_image
# how should this method look ?
end
end
How should the method "fetch_image" look ?

Here's a link to a page that explains exactly what you need.
http://trevorturk.wordpress.com/2008/12/11/easy-upload-via-url-with-paperclip/
I've implemented it successfully on my own site.

I'm not sure this is still useful for you or not, but in a pull request to paperclip just a few hours ago, I've managed to make this super easy.
def set_photo
self.photo = URI.parse(self.image_remote_url)
end
This should do the job now on paperclip (version > 3.1.3) (not 3.1.3 but whatever comes after).

Related

Why doesn't work Paperclip interpolations?

I am trying to make work Paperclip interpolations the whole afternoon, but still not success.
Here is how I have set up the Image model:
has_attached_file :image,
:styles => { :thumb => '300x300#',
:medium => "300x300>",
:original => "900x900>" },
:path => ":rails_root/public/images/:user_id/:style/:basename.:extension",
:url => "/images/:user_id/:style/:basename.:extension"
In /config/initializers/paperclip.rb is following:
Paperclip.options[:command_path] = "/usr/local/bin"
module Paperclip
module Interpolations
def user_id attachment, style_name
attachment.instance.user_id.to_s
end
end
end
But every time I save a file, the files is saved as
/images//original/file-name.jpg
The user's ID is missing.
What is wrong in this sample? I still cannot find the right config of Paperclip setup. I would be very grateful for every help.
Thank you
Try to add this in your Image model
Paperclip.interpolates :user_id do |attachment, style|
attachment.instance.user_id.to_s
end

How to fix content-type for Aurigma uploads to S3?

Our users have two ways of uploading images. One is through a simple HTML form and the other is through an iPhone app called Aurigma. We use Paperclip to process the images and store them on S3. Images that are uploaded with Aurigma end up having the wrong content-type, which causes them to open as an application.
I tried two solutions:
before_save :set_content_type
def set_content_type
self.image.instance_write(:content_type,"image/png")
end
And:
before_post_process :set_content_type
def set_content_type
self.image.instance_write(:content_type, MIME::Types.type_for(self.image_file_name).to_s)
end
It seems as if both solutions are ignored.
Using paperclip version 3.0.2, Aurigma version 1.3 and I'm uploading a screenshot from my iPhone. This is my paperclip configuration:
has_attached_file :image, {
:convert_options => { :all => '-auto-orient' },
:styles => {
:iphone3 => "150x150",
:web => "300x300"
},
:storage => :s3,
:bucket => ENV['S3_BUCKET'],
:s3_credentials => {
:access_key_id => ENV['S3_KEY'],
:secret_access_key => ENV['S3_SECRET']
},
:path => "/pictures/:id/:style.:extension",
:url => "/pictures/:id/:style.:extension"}
}
I just answered a similar question.
You need to do a copy to itself or use a pre-signed url with the content-type specified in the querystring.
Using the AWS SDK for Ruby and url_for:
object = bucket.objects.myobject
url = object.url_for(:read, :response_content_type => "image/png")
As far as I understand first you upload all the files from client devices to your own server (through Aurigma Up) and then these files are uploaded to Amazon S3. I have similar problem trying to change content-type on client device. This is not possible. You should send files on your server and then change content type before uploading files to S3.

RAILS S3 displaying pdf file stored in amazon s3

How to display pdf file which is stored in s3 amazon in rails application???
when uploading files to S3 the filename has to have No Spaces or special caracters.
To upload files with spaces use the following
yourmodel.rb
class Video < ActiveRecord::Base
has_attached_file :video,
:path => ":rails_root/public/system/:attachment/:id/:style/:normalized_video_file_name",
:url => "/system/:attachment/:id/:style/:normalized_video_file_name"
Paperclip.interpolates :normalized_video_file_name do |attachment, style|
attachment.instance.normalized_video_file_name
end
def normalized_video_file_name
"#{self.id}-#{self.video_file_name.gsub( /[^a-zA-Z0-9_\.]/, '_')}"
end
end
What are we doing here? Easy, in has_attached_file we edit the way paperclip returns the path and url by default, the most relevant components when saving and loading the file in order to display it. Paperclip default values are:
path default => ":rails_root/public/system/:attachment/:id/:style/:filename"
url default => "/system/:attachment/:id/:style/:filename"
Values preceded by ’:’ are the standard interpolations paperclip has
http://blog.wyeworks.com/2009/7/13/paperclip-file-rename
you need to add an :s3_headers entry to your has_attachment line:
has_attached_file :asset,
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "uploads/:id/:basename.:extension",
:s3_headers => {"Content-Disposition" => "attachment"},
:s3_permissions => 'authenticated-read',
:s3_protocol => "http",
:bucket => "my_bucket_or_something"

Amazon S3 path or to_file wont work

Hey guys ive got the following code in my Model
require 'RMagick'
class Upload < ActiveRecord::Base
belongs_to :card
has_attached_file :photo,
:styles => {
:thumb => ["100x100", :jpg],
:pagesize => ["500x400", :jpg],
},
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => 'your_deck',
:default_style => :pagesize
attr_accessor :x1, :y1, :width, :height
def update_attributes(att)
scaled_img = Magick::ImageList.new(self.photo.path)
orig_img = Magick::ImageList.new(self.photo.path(:original))
scale = orig_img.columns.to_f / scaled_img.columns
args = [ att[:x1], att[:y1], att[:width], att[:height] ]
args = args.collect { |a| a.to_i * scale }
orig_img.crop!(*args)
orig_img.write(self.photo.path(:original))
self.photo.reprocess!
self.save
super(att)
end
end
This code works offline however when on Heroku with Amazon S3 it fails to work, ive tried the code with to_file and it also wont work
I get the following error
can't convert Array into String
I had the same problem and it's due to an update to paperclip. I'm surprised heroku are still using this gem version as it will surely affect all their users; I installed a previous version as a plugin and it's fine. Don't forget to remove the gem from your .gems file or specify the previous version in your gems manifest.

Ruby on Rails / Paperclip / AWS::S3::NoSuchBucket error

I installed the paperclip plugin and was able to use it locally. When I configured it to work with amazon S3 I keep getting the NoSuchBucket (The specified bucket does not exist) error. Paperclip documentation states that the bucket will be created if it doesn't exist but clearly
something is going wrong in my case.
I first insalled aws-s3 gem (v0.6.2)
then also installed right_aws gem (v1.9.0)
both have corresponding
config.gem "aws-s3", :lib => "aws/s3"
config.gem 'right_aws', :version => '1.9.0'
lines in environment.rb file
The code for the image.rb file with paperclip is as follows:
class Image < ActiveRecord::Base
belongs_to :work
has_attached_file :photo, :styles => {:big => "612x1224>", :small => "180X360>", :thumb => "36x36#"},
:storage => 's3',
:s3_credentials => YAML.load_file("#{RAILS_ROOT}/config/s3.yml")[RAILS_ENV],
:path => ":attachment/:id/:style/:basename.:extension",
:bucket => 'my-unique-image-bucket'
attr_protected :photo_file_name, :photo_content_type, :photo_size
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 3.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png', 'image/gif']
end
I'm not entirely sure this is it, but your loading of the s3_credentials is different than what I'm using on my production sites.
My config line is:
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml"
Instead of
:s3_credentials => YAML.load_file("#{RAILS_ROOT}/config/s3.yml")[RAILS_ENV]
it should create but the bucket but this was a bug at one point :
http://groups.google.com/group/paperclip-plugin/browse_thread/thread/42f148cee71a0477
i recently had this problem and it turned out to be the servers time was hugely off and s3 wouldnt allow any updates "that far in the future" or similar but the rails error was NoSuchBucket...confusing
..
I have installed the s3fox plugin for firefox and created the bucket with the plugin. Now Paperclip works fine with S3 as the bucket identified is already created.
But I am still curious about paperclip's inability to create new buckets with the code above.
In case anyone winds up here via google: I saw this same error when I mistakenly switched the order of the 2nd and 3rd parameters I was passing to AWS::S3::S3Object.store.
It's not your case, but AWS doesn't allow upper case letters in bucket name and paperclip doesn't check that, failing in create_bucket.

Resources