我正在尝试将来自S3存储桶的图像上传并显示到heroku上的rails 4应用程序。我想在创建'Designer'对象
时添加图像我正在使用回形针。在config/environments/production.rb
:
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
我设置了配置变量
$ heroku config
AWS_ACCESS_KEY_ID: AKIAI...HVWD2YQ
AWS_BUCKET: myafricastyle
AWS_SECRET_ACCESS_KEY: VFgFVF4.....gKnT5AMFtP4
这是我要将图像添加到的模型:
class Designer < ActiveRecord::Base
has_many :items, dependent: :destroy
has_attached_file :avatar, styles: {
thumb: '100x100>',
square: '200x200#',
medium: '300x300>'
}
end
我有一个迁移将图像添加到Designer表中。
class AddAttachmentAvatarToDesigners < ActiveRecord::Migration
def self.up
change_table :designers do |t|
t.attachment :avatar
end
end
def self.down
drop_attached_file :designers, :avatar
end
end
当我尝试使用上传的照片添加Designer时,我得到:
$ heroku logs
[paperclip] saving /designers/avatars/000/000/016/original/moto_ninjas87.jpg
[paperclip] saving /designers/avatars/000/000/016/original/moto_ninjas87.jpg
Completed 500 Internal Server Error in 2622ms
Completed 500 Internal Server Error in 2622ms
app/controllers/designers_controller.rb:33:in `block in create'
app/controllers/designers_controller.rb:33:in `block in create'
app/controllers/designers_controller.rb:32:in `create'
ArgumentError (missing required :bucket option):
ArgumentError (missing required :bucket option):
app/controllers/designers_controller.rb:32:in `create'
我的存储分区选项配置有什么问题?
此外:
我的controllers/designers_controller.rb
显示:
def create
@designer = Designer.new(designer_params)
respond_to do |format|
if @designer.save
format.html { redirect_to @designer, notice: 'Designer was successfully created.' }
format.json { render action: 'show', status: :created, location: @designer }
else
format.html { render action: 'new' }
format.json { render json: @designer.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_designer
@designer = Designer.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def designer_params
params.require(:designer).permit(:name, :country, :about, :avatar)
end
我安装了aws-sdk
和paperclip
宝石
答案 0 :(得分:1)
您在传递给s3_credentials的哈希中缺少存储桶。您还应该考虑将配置文件移动到yml文件并从那里加载。
:storage => :s3,
:path => "/archive/:style/:id/:filename",
:s3_credentials => File.join(Rails.root, 'config', 's3.yml')
s3.yml文件
production:
bucket: <%= ENV['S3_BUCKET'] %>
access_key_id: <%= ENV['S3_ACCESS_KEY_ID'] %>
secret_access_key: <%= ENV['S3_SECRET_ACCESS_KEY'] %>
答案 1 :(得分:0)
发生以下情况是因为您在create方法中调用了@ designer.save,而paperclip为您的设计器类添加了一个后创建钩子。
ArgumentError (missing required :bucket option):
app/controllers/designers_controller.rb:32:in `create'
为了解决这个问题,我认为
:bucket => ENV['AWS_BUCKET']
在哈希中属于一个级别。所以:
config.paperclip_defaults = {
:storage => :s3,
:bucket => ENV['AWS_BUCKET'],
:s3_credentials => {
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
有关详细信息,请参阅: http://rubydoc.info/gems/paperclip/Paperclip/Storage/S3