0
votes

I tried to upload movie files from my rails application to Amazon S3. First I tried paperclip, but it dosn't worked ...

No I tried carrierwave + fog but same result nothing worked, no files stored in S3 no database entry and no errors ...

My Files look like this:

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

upload form

normal multipart form_tag

    <%= form_for Movie.new, :html => {:multipart => true, :class => "form-horizontal", :role => "form"}, :method => :post, :url => {} do |f| %>

with file field

    <div class="form-group">
      <label><%= f.label :movie %></label>
      <%= f.file_field :movie, :class => "form-control", :placeholder => :movie %>
    </div>

I used this tutorial: https://u.osu.edu/hasnan.1/2014/03/13/rails-4-upload-image-to-s3-using-fog-and-carrierwave/

Whats going wrong?

1

1 Answers

2
votes

I had the same problem. This fixed my problem.

In 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

In 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

I hope this would be helpful.