Felix
Felix

Reputation: 5629

rails uploading file to Amazon S3

I want to upload files directly to AmazonS3 from my Rails application. config looks like this at the moment:

Gemfile

gem "paperclip"

gem 'aws-sdk'

In config/application.rb

# Amazon S3 configuration for paperclip
config.paperclip_defaults = {
    :storage => :s3,
    :s3_host_name => 's3-eu-west-1.amazonaws.com'
}

Model - movie.rb

class Movie < ActiveRecord::Base

   # add File to Movie association on column mo
   has_attached_file :movie,
                :storage => :s3,
                :bucket => '--',
                :s3_credentials => {
                    :bucket => '---',
                    :access_key_id => '---',
                    :secret_access_key => '---'
                }

  # validdates the file type
  validates_attachment_content_type :movie, :content_type => /\Avideo\/.*\Z/

end

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)
      @movie = Movie.new(params[:movie])
      if @movie.save
        flash[:notice] = t("flash.moveuploadstarted")
        redirect_to :addMovie
      end
    else
      @movie = Movie.new
    end
  end


  private

   def movies_params
    params.require(:movie).permit(:movietitle, :movieprice, :locked,    :moviedescription, :currency, :language)
  end

end

Database looks like this: enter image description here

When I fill my form and send the movie the browser starts to upload the movie the persentage is shown on left bottom corner. But when 100% are reached the movie isn't stored to Amazon S3 and DB is empty, but no errors or so....

What could be the problem? .

UPDATE:

upload form has this input file for the video

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

Upvotes: 1

Views: 1573

Answers (3)

GRex
GRex

Reputation: 21

Have you tried printing the params[:movie] to the console?

puts params[:movie]

It's looks like you're getting the movie hash from the parameters rather than the value from a field inside that hash. I would suggest that you use a field name for saving the video file, movie_video for example.

 class Movie < ActiveRecord::Base

       # add File to Movie association on column mo
       has_attached_file :movie_video,
                    :storage => :s3,
                    :bucket => '--',
                    :s3_credentials => {
                        :bucket => '---',
                        :access_key_id => '---',
                        :secret_access_key => '---'
                    }

      # validdates the file type
      validates_attachment_content_type :movie_video, :content_type => /\Avideo\/.*\Z/

end

Upvotes: 1

Arminius
Arminius

Reputation: 606

This is the code that goes if you use carrierwave, but should be the same for paperpclip (by the way, carrierwave is the standard and recommended)

Note that it should be in the config initializers file

config/initializers/carrier_wave.rb

if Rails.env.production?
  CarrierWave.configure do |config|
    config.fog_credentials = {
      # Configuration for Amazon S3
      :provider              => 'AWS',
      :aws_access_key_id     => ENV['S3_ACCESS_KEY'],
      :aws_secret_access_key => ENV['S3_SECRET_KEY']
    }
    config.fog_directory     =  ENV['S3_BUCKET']
  end
end

Upvotes: 1

Dup Step
Dup Step

Reputation: 218

For giggles can you try hard doing the bucket name, access key and secret key in to the application.rb file? I'm wondering if the bucket and other info need to be in the config file

Upvotes: 1

Related Questions