使用图片 url 用回形针播种
Seed with paperclip using image url
我正在使用 Paperclip,我想将图像上传到 S3 上作为用户头像。
我的 CSV
username avatar_url
foo https://s3.amazonaws.com/foo/seed/10215716104_e09765dabd_z.jpg
我正要去 follow this tutorial to create a CSV, along with this 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 上使用图片做种?
以下是我的使用方法:
class User < ActiveRecord::Base
has_attached_file :avatar, ...
end
user = User.new
user.avatar = URI.parse(url_goes_here)
user.save
我正在使用 Paperclip,我想将图像上传到 S3 上作为用户头像。
我的 CSV
username avatar_url
foo https://s3.amazonaws.com/foo/seed/10215716104_e09765dabd_z.jpg
我正要去 follow this tutorial to create a CSV, along with this 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 上使用图片做种?
以下是我的使用方法:
class User < ActiveRecord::Base
has_attached_file :avatar, ...
end
user = User.new
user.avatar = URI.parse(url_goes_here)
user.save