Ruby on rails 在rails 3.1应用程序中实例化图像,然后用回形针上传到S3
我正在使用以下命令在回调中创建barcode.png: Isbn.rb:Ruby on rails 在rails 3.1应用程序中实例化图像,然后用回形针上传到S3,ruby-on-rails,amazon-s3,paperclip,Ruby On Rails,Amazon S3,Paperclip,我正在使用以下命令在回调中创建barcode.png: Isbn.rb: before_save :barcode def barcode barcode = Barby::Bookland.new(self.productidentifier_idvalue) my_bc = Barcodeimg.new(:isbn_id => self.id) my_bc.image = File.open("#{self.productidentifier_idvalue}-ba
before_save :barcode
def barcode
barcode = Barby::Bookland.new(self.productidentifier_idvalue)
my_bc = Barcodeimg.new(:isbn_id => self.id)
my_bc.image = File.open("#{self.productidentifier_idvalue}-barcode.png", 'w') do |f|
f.write barcode.to_png
end
my_bc.save!
end
然后我希望通过回形针将其直接上传到S3:这是Barcodeimg.rb模型:
require 'open-uri'
require "aws/s3"
class Barcodeimg < ActiveRecord::Base
has_attached_file :image,
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:s3_protocol => "https",
:path => ":class/:id/:basename_:style.:extension",
:bucket => "bucketname",
:url => ":s3_eu_url"
end
将返回块的结果。这意味着您正在将文件的长度写入附件
我将用以下内容替换该块:
f = File.open("#{self.productidentifier_idvalue}-barcode.png", 'w+')
f.write barcode.to_png
my_bc.image = f
另一个问题可能是当前目录的权限。你有权在那里写作吗?Hiya-谢谢你的想法。返回错误:ActionView::Template::error(未打开进行读取)。也许这就是你建议的权限问题。不,那是我应该睡觉的时候写代码;-)。
文件
应该用w+
打开。先生,你是上帝。非常感谢你!如果不先写入磁盘,您将如何执行此操作?Heroku根本不允许你写“本地”字-/@马修克拉克,但你必须在最新的堆栈(雪松)。
f = File.open("#{self.productidentifier_idvalue}-barcode.png", 'w+')
f.write barcode.to_png
my_bc.image = f