我正在制作一款能够让用户上传图片的rails应用程序。现在它只使用paperclip,它在我的系统/用户本地保存图像。
现在,我希望能够将图像上传到S3。我配置了我的水桶,并安装了aws-sdk
宝石,所以亚马逊方面也下降了。
我在线阅读了多个来源,但迄今为止都没有。这是我目前拥有的
user.rb
has_attached_file :image_1,
:storage => :s3,
:s3_credentials => Proc.new{ |amazon| amazon.instance.s3_credentials }
validates_attachment_content_type :image_1, :content_type => { :content_type => ["image/jpeg", "image/gif", "image/png", "application/pdf"] }
has_attached_file :image_2,
:storage => :s3,
:s3_credentials => Proc.new{ |amazon| amazon.instance.s3_credentials }
validates_attachment_content_type :image_2, :content_type => { :content_type => ["image/jpeg", "image/gif", "image/png", "application/pdf"] }
def s3_credentials
{:bucket => "bucketname",
:access_key_id => "1234",
:secret_access_key => "1234"}
end
aws.yml
test:
access_key_id: key
secret_access_key: secret
development:
access_key_id: key
secret_access_key: secret
production:
access_key_id: key
secret_access_key: secret
development.rb,production.rb,test.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_host_name => 's3-website-us-west-2.amazonaws.com'
}
users_controller.rb
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
redirect_to '/'
else
render 'new'
end
end
def user_params
params.require(:user).permit(:image_1, :image_2)
end
**这是我提交表格时的堆栈跟踪**
Started POST "/users" for ::1 at 2015-08-17 19:20:14 -0700
Processing by UsersController#create as HTML
Parameters{"utf8"=>"✓","user"=>{"recommendation_image"=>#<ActionDispatch::Http::UploadedFile:0x007f8e4c9c8380 @tempfile=#<Tempfile:/var/folders/9c/1_0mk00n297f560_fpv9jzl40000gn/T/RackMultipart20150817-1642-1ituzt0.JPG>, @original_filename="20150520_DBC_519_L.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[recommendation_image]\"; filename=\"20150520_DBC_519_L.JPG\"\r\nContent-Type: image/jpeg\r\n">, "identification_image"=>#<ActionDispatch::Http::UploadedFile:0x007f8e4c9c82e0 @tempfile=#<Tempfile:/var/folders/9c/1_0mk00n297f560_fpv9jzl40000gn/T/RackMultipart20150817-1642-1mh7arc.JPG>, @original_filename="20150520_DBC_519_L.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[identification_image]\"; filename=\"20150520_DBC_519_L.JPG\"\r\nContent-Type: image/jpeg\r\n">}, "commit"=>"Submit"}
Command :: file -b --mime '/var/folders/9c/1_0mk00n297f560_fpv9jzl40000gn/T/594bb37e5dccc1de38e08bc3a4fe8b6e20150817-1642-g89xku.JPG'
Command :: file -b --mime '/var/folders/9c/1_0mk00n297f560_fpv9jzl40000gn/T/594bb37e5dccc1de38e08bc3a4fe8b6e20150817-1642-qx6l87.JPG'
我还阅读了一些资源,说我可能需要一个初始化文件,但这似乎也不起作用。如果有人有这方面的经验,我将不胜感激听到你的建议。