I would like users to be able to upload videos to my rails site using carrierwave, fog and Amazon S3. (I haven't tried to implement S3 for storage yet and is not the subject of this questions.)
What database columns do I need for my video model and model save code to successfully save a video file using Carrierwave?
At the moment I have the following for my Video table but im confused as to what i really need.
class CreateVideoTable < ActiveRecord::Migration
def up
create_table :videos do |t|
t.string :video
t.integer :user_id
t.integer :question_id
t.string :youtube_url
t.string :type
t.string :filename
t.string :checksum
t.string :path
t.integer :filesize
t.integer :width
t.integer :height
t.integer :duration
t.integer :bit_rate
t.timestamps
end
add_index :videos, [:user_id, :question_id]
end
def down
remove_index :videos, [:user_id, :question_id]
drop_table :videos
end
end
This is how my Video model looks:
class Video < ActiveRecord::Base
attr_accessor :user_id, :question_id, :video, :youtube_url, :type, :filename, :checksum, :path, :filesize, :width, :height, :duration, :bit_rate
belongs_to :question
belongs_to :user
mount_uploader :video, VideoUploader
end
and finally my carrierwave uploader.
class VideoUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w(ogg ogv 3gp mp4 m4v webm mov m2v 3g2)
# %w(ogg ogv 3gp mp4 m4v webm mov)
end
end
Don't forget to put storage :fog
Otherwise, in the model, you only need a string column video
for storing the S3 URL. Carrierwave/fog will take care of the rest. You can of course add more columns relevant to your application if needed.
In other words, you are good to go :)
Also, the initializer is missing, don't know if you forgot it:
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => 'xxx', # required
:aws_secret_access_key => 'yyy', # required
:region => 'eu-west-1', # optional, defaults to 'us-east-1'
:host => 's3.example.com', # optional, defaults to nil
:endpoint => 'https://s3.example.com:8080' # optional, defaults to nil
}
config.fog_directory = 'name_of_directory' # required
config.fog_public = false # optional, defaults to true
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {}
end