Rails通过载波和雾将文件上传到Amazon S3 [英] Rails upload files to Amazon S3 with carrierwave and fog
本文介绍了Rails通过载波和雾将文件上传到Amazon S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图将电影文件从我的Rails应用程序上传到Amazon S3.首先,我尝试使用回形针,但没有用...
I tried to upload movie files from my rails application to Amazon S3. First I tried paperclip, but it dosn't worked ...
没有,我尝试过载波+雾,但是同样的结果没有任何效果,没有文件存储在S3中,没有数据库条目,也没有错误...
No I tried carrierwave + fog but same result nothing worked, no files stored in S3 no database entry and no errors ...
我的文件如下:
app/uploader/movie_uploader.rb
class MovieUploader < CarrierWave::Uploader::Base
storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
config/initializers/carrierwave.rb
CarrierWave.configure do |config|
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: '--',
aws_secret_access_key: '--',
region: 'eu-central-1'
}
config.fog_directory = 'movies'
end
app/models/movie.rb
class Movie < ActiveRecord::Base
mount_uploader :movie, MovieUploader
end
app/controller/movies_controller.rb
class MoviesController < ActionController::Base
layout "application"
# Method to add a new Movie
def addMovie
if request.post?
@movie = Movie.new(movies_params)
if @movie.save
redirect_to :addMovie
end
else
@movie = Movie.new
end
end
private
def movies_params
params.require(:movie).permit(:movietitle, :movieprice, :locked, :moviedescription, :currency, :language, :movie)
end
end
上传表单
正常的多部分表单标签
<%= form_for Movie.new, :html => {:multipart => true, :class => "form-horizontal", :role => "form"}, :method => :post, :url => {} do |f| %>
带有文件字段
<div class="form-group">
<label><%= f.label :movie %></label>
<%= f.file_field :movie, :class => "form-control", :placeholder => :movie %>
</div>
我使用了本教程:出了什么问题?
推荐答案
我遇到了同样的问题.这解决了我的问题.
I had the same problem. This fixed my problem.
在MovieUploader中
class MovieUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
include CarrierWave::MiniMagick
storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
在config/carrierwave.rb
if Rails.env.development? or Rails.env.test?
CarrierWave.configure do |config|
config.storage = :file
config.enable_processing = false
end
else
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => 'ACCESS KEY', # required
:aws_secret_access_key => 'ACCESS SECRET', # required
:region => 'eu-central-1'
}
config.fog_use_ssl_for_aws = false
config.storage = :fog
config.fog_directory = 'movies' # required
end
end
我希望这会有所帮助.
这篇关于Rails通过载波和雾将文件上传到Amazon S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文