2017-06-07 80 views
0

我正在使用回形針,我想上傳用戶的頭像在S3上的圖像。使用圖像url的回形針種子

我的CSV

username avatar_url 
foo   https://s3.amazonaws.com/foo/seed/10215716104_e09765dabd_z.jpg 

我要follow this tutorial創建一個CSV,伴隨着這個SO question(用於回形針播種),所以是這樣的:

require 'csv' 

csv_text = File.read(Rails.root.join('lib', 'seeds', 'user.csv')) 
csv = CSV.parse(csv_text, :headers => true, :encoding => 'ISO-8859-1') 
csv.each do |row| 
    u = User.new 
    u.username = row['username'] 
    u.avatar = File.open(row['avatar_url']) 
    u.save 
end 

然而,當我嘗試這在控制檯,像這樣:

User.create(username: 'foo', avatar: File.new("https://s3.amazonaws.com/foo/seed/10215716104_e09765dab")) 

我得到一個錯誤:

Errno::ENOENT: No such file or directory @ rb_sysopen - https://s3.amazonaws.com/foo/seed/10215716104_e09765dabd_z.jpg 

是否可以在S3上播種圖像?

+0

不應該說是'File.open()'? –

+0

我試過兩種。但都不適合我。 :( – yellowreign

回答

0

這裏是我如何使用它:

class User < ActiveRecord::Base 
    has_attached_file :avatar, ... 
end 

user = User.new 
user.avatar = URI.parse(url_goes_here) 
user.save 

Attachment downloaded from a URL