Note that there are some explanatory texts on larger screens.

plurals
  1. POPaperclip: upload from url with extension
    primarykey
    data
    text
    <p>I would like to upload pictures from URLs by paperclip on S3 storage. I work with :</p> <pre><code>Ruby 1.9.3 Rails 3.2.6 paperclip 3.1.3 aws-sdk 1.3.9 </code></pre> <p>I have my Picture Model:</p> <pre><code>class Asset has_attached_file :asset, :styles =&gt; {:thumb =&gt; "60x60&gt;"}, :storage =&gt; :s3, :s3_credentials =&gt; "#{Rails.root}/config/s3.yml", :path =&gt; "/pictures/:id/:style.:extension" validates_attachment_content_type :asset, :content_type =&gt; ['image/gif', 'image/jpeg', 'image/png', 'image/x-ms-bmp'] end </code></pre> <p>So basically I made this to download my file from an URL:</p> <pre><code>picture = Asset.new(asset: open("http://www.my_url.com/my_picture.jpg")) picture.save </code></pre> <p>But it save my file with a bad file_name and it doesn't set the extension of the file :</p> <pre><code>#&lt;Asset id: 5, asset_file_name: "open-uri20120717-6028-1k3f7xz", asset_content_type: "image/jpeg", asset_update_at: nil, asset_file_size: 91565, created_at: "2012-07-17 12:41:40", updated_at: "2012-07-17 12:41:40"&gt; p.asset.url =&gt; http://s3.amazonaws.com/my_assets_path/pictures/5/original. </code></pre> <p>As you can see there is no extension.</p> <p>I found a method to solve it but I'm sure I can have a better way. This solution is to copy the file on my computer then I send it on S3 like this:</p> <pre><code>filename = "#{Rails.root}/tmp/my_picture.jpg" open(filename, 'wb') do |file| file &lt;&lt; open("http://www.my_url.com/my_picture.jpg").read end picture = Asset::Picture.new(asset: open(filename)) picture.save </code></pre> <p>This works on my computer:</p> <pre><code>#&lt;Asset::Picture id: 10, asset_file_name: "my_picture.jpg", asset_content_type: "image/jpeg", asset_update_at: nil, asset_file_size: 91565, created_at: "2012-07-17 12:45:30", updated_at: "2012-07-17 12:45:30"&gt; p.asset.url =&gt; "http://s3.amazonaws.com/assets.tests/my_assets_path/10/original.jpg" </code></pre> <p>However I don't know if this method will work on Heroku (I host my app on it).</p> <p>There is not a better way without pass by a temporary file? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload