Uploading videos Rails/S3/Paperclip - ruby-on-rails

I have already gone through all the answers to make the upload of my video to S3 work but they do not work. I keep on getting the error uninitialized constant Paperclip::Storage::S3::Aws.
I hope you could help me out on this one.
Gemfile
gem 'simple_form', '~> 3.4'
gem 'haml', '~> 4.0', '>= 4.0.7'
gem 'coffee-script-source', '1.8.0'
gem 'bootstrap-sass', '~> 3.3', '>= 3.3.7'
gem 'paperclip'
gem 'carrierwave'
gem "paperclip-ffmpeg", "~> 1.0.1"
gem 'paperclip-av-transcoder'
gem 'aws-sdk', '~> 1.6'
gem "figaro"
config\s3.yml
AWS_ACCESS_KEY_ID: xxx
AWS_SECRET_ACCESS_KEY: xxx
S3_BUCKET_NAME: xxx
models\video.rb
class Video < ApplicationRecord
has_attached_file :clip, styles: {
medium: {
:geometry => "640x480",
:format => 'mp4'
},
thumb: { :geometry => "160x120",
:format => 'jpeg',
:time => 10}
},
processors: [:transcoder],
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml"
validates_attachment :clip,
content_type: { content_type: ['application/x-shockwave-flash', 'application/x-shockwave-flash', 'application/flv', 'video/x-flv']}
before_post_process :skip_for_audio
def skip_for_audio
! %w(audio/ogg application/ogg).include?(asset_content_type)
end
before_post_process :image?
def image?
!(data_content_type =~ /^image.*/).nil?
end
end
Videos_controller.rb
class VideosController < ApplicationController
def index
#videos = Video.all
#video = Video.order('created_at')
end
def new
#video = Video.new
end
def create
#video = Video.new(videos_params)
if #video.save
flash[:success] = "The step was added!"
redirect_to root_path
else
render 'New'
end
end
def destroy
#video = Video.find(params[:id])
#video.destroy
flash[:success] = "The step was destroyed."
redirect_to root_path
end
private
def videos_params
params.require(:video).permit(:title, :description, :clip)
end
end
initilizers\paperclip.rb
paperclip_defaults = Rails.application.config_for :paperclip
paperclip_defaults.symbolize_keys!
Paperclip::Attachment.default_options.merge! paperclip_defaults
initilizers\aws.rb
AWS.config(
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
)
S3_BUCKET = AWS::S3.new.buckets[ENV['S3_BUCKET']]

Have your tried this configuration?
# config/environments/production.rb
config.paperclip_defaults = {
storage: :s3,
s3_credentials: {
bucket: ENV.fetch('S3_BUCKET_NAME'),
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
s3_region: ENV.fetch('AWS_REGION'),
}
}
.
# config/initializers/paperclip.rb
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'
Paperclip::Attachment.default_options[:s3_host_name] = 's3-us-west-2.amazonaws.com'
.

Related

Rails paperclip watermark "uninitialized constant Paperclip"

I try apply watermark to my paperclip, it keep show error message and unable update/upload image. It keep show **
uninitialized constant Paperclip::Watermark::PaperclipCommandLineError
**
gem file
gem "paperclip", '4.2' gem 'rails', '4.2.6' gem
'paperclip-compression'
paperclip_processors/watermark.rb
module Paperclip
class Watermark < Thumbnail
def initialize(file, options = {}, attachment = nil)
super
#watermark_path = options[:watermark_path]
#position = options[:position].nil? ? "SouthEast" : options[:position]
end
def make
src = #file
dst = Tempfile.new([#basename].compact.join("."))
dst.binmode
return super unless #watermark_path
params = "-gravity #{#position} #{transformation_command.join(" ")} #{#watermark_path} :source :dest"
begin
success = Paperclip.run("composite", params, :source => "#{File.expand_path(src.path)}[0]", :dest => File.expand_path(dst.path))
rescue PaperclipCommandLineError
raise PaperclipError, "There was an error processing the watermark for #{#basename}" if #whiny
end
dst
end
end
end
lisitng.rb
class Listing < ActiveRecord::Base
require 'paperclip_processors/watermark'
has_attached_file :image,
:processors => [:watermark],
:styles => {
:medium => {
:geometry => "300x300>",
:watermark_path => "#{Rails.root}/public/images/watermark.png"
},
:thumb => "100x100>",
}
I think you're subclassing wrong. Don't subclass Thumbnail, subclass Paperclip::Processor
module Paperclip
class Watermark < Processor
...
https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/processor.rb

Paperclip::Error, There was an error processing the thumbnail

I'm using paperclip, imagemagick, rmagick to upload the images to s3. Previously the image upload functionality is working well on ruby 1.9.3,'rails', '3.2.11'. After upgrading ruby 2.0.0 and 'rails', '4.0.3' it throughing error.
In store.rb
#paperclip
has_attached_file :logo,
styles: {
thumb: ["40x40#", :png],
small: ["400x400>", :png],
masked_with_overlay: ["288x64", :png],
masked_with_nearby_overlay: ["238x47#", :png],
masked_with_rewards_overlay: ["288x64", :png],
masked_with_overlay_for_old: ["288x64", :png],
masked_with_nearby_overlay_for_old: ["238x47#", :png],
masked_with_rewards_overlay_for_old: ["288x64", :png],
masked_full_card_image: ["576x128", :png],
masked_logo_only: ["576x128", :png]
},
convert_options: { masked_with_overlay: Proc.new{self.apply_mask_and_overlay},
masked_with_nearby_overlay: Proc.new{self.apply_mask_and_nearby_overlay},
masked_with_rewards_overlay: Proc.new{self.apply_mask_and_rewards_overlay},
masked_with_overlay_for_old: Proc.new{self.apply_mask_and_overlay_for_old},
masked_with_rewards_overlay_for_old: Proc.new{self.apply_mask_and_rewards_overlay_for_old},
masked_full_card_image: Proc.new{self.apply_mask_full_card_image},
masked_logo_only: Proc.new{self.apply_mask_logo_only}
},
storage: :s3,
s3_credentials: "#{Rails.root}/config/s3.yml",
path: "/store_logos/:style/:id/:filename"
validates_attachment :logo, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/JPG", "image/JPEG", "image/PNG"] }
Here the Gemfile
ruby '2.0.0'
gem 'rails', '4.0.3'
gem 'aws-s3'
gem 'aws-sdk', '~> 1.5.7'
gem 'paperclip', "~> 4.2"
gem 'rmagick', '2.13.2'
stores_controller.rb
class StoresController < ApplicationController
def update
if !params[:store].nil? && !params[:store][:store_owner].nil?
params[:store][:store_owner][:email] = params[:store][:store_owner][:email].downcase
end
if params[:demo_store_changes]
params[:store][:active] = false
else
params[:store][:store_hours] = params[:store][:store_hours].to_json
params[:store][:minimum_seconds_per_scan] = (params[:store][:minimum_seconds_per_scan]).to_i * 3600
end
#store.store_owner.store_owner!
if params[:store][:scanning_distance].blank?
params[:store].delete :ms
else
if params[:store][:location_check]
if params[:store][:ms] == "mi"
params[:store][:scanning_distance] = ( params[:store][:scanning_distance].to_f * 5280 )
params[:store].delete :ms
else
params[:store][:scanning_distance] = params[:store][:scanning_distance].to_f
params[:store].delete :ms
end
end
end
if #store.update_attributes(store_params) and #store.build_categories(params[:category_ids].to_a)
if params[:demo_store_changes]
demo_message = #store.demo ? " is now demo store" : " is not a demo store anymore"
flash[:notice] = #store.name + demo_message
redirect_to stores_path('store[search]'=> params[:store_search], search_value: params[:search_value])
else
#password_chk=params[:store][:store_owner_attributes][:password]
redirect_to "#{store_path()}?myid=#{session[:bread_crum]}", notice: (I18n.t :update_store_success)
end
else
#store.store_hours = ActiveSupport::JSON.decode(#store.store_hours)
if #store.minimum_seconds_per_scan?
#store.minimum_seconds_per_scan = (#store.minimum_seconds_per_scan / 3600).to_i
end
#categories = Category.all.sort
#states = StoreConstants::STATES.sort
#main_location_stores = Store.with_no_branch(#store.id)
flash.now[:error] = (I18n.t :update_store_failed)
render action: "edit"
end
private
def store_params
params.require(:store).permit!
end
end
Here is log
Any help?, Thanks in advance
Look into the log
(:bucket_name=>"punchme-co-dev",:key=>"store_logos/masked_with_rewards_overlay/96/Casablanca.png") AWS::S3::Errors::NoSuchKey No Such Key)
AWS access key is taking your image file path.
I finally found the solution for this.
convert: no decode delegate for this image format `/home/likewise-open/ANNETSITE/1820/RailsWorkspace/Punchme_web_Test' # error/constitute.c/ReadImage/532.
convert: unable to open image `Builds/app/assets/images/card_overLay.png': # error/blob.c/OpenBlob/2587.
By renaming project folder Project_web_Test Builds to Project_web_Test_Builds, When you process image space is not allowed.

From ActiveRecord to S3

I am new to S3 and Rails so got stuck. I am wondering if I can directly pass object to S3.
My controller:
def start_upload
#forecasts = Forecast.all
Forecast.export_to_s3(#forecasts)
end
Model:
def self.export_to_s3(data)
---AWS configs ---
Aws.config = { :access_key_id => aws_access_key, :secret_access_key => aws_secret_access_key, :region => aws_region }
s3 = Aws::S3::Client.new(region:aws_region)
resp = s3.put_object(
:bucket => aws_bucket,
:key => aws_bucket_key_forcast,
:body => IO.read(data)
)
end
What are you trying to do? What is you goal?
You should try to serialize your object before you push it to your bucket.
def start_upload
#forecasts = Forecast.all
Forecast.export_to_s3(#forecasts.to_json)
end
def self.export_to_s3(data)
# ---AWS configs ---
Aws.config = { :access_key_id => aws_access_key, :secret_access_key => aws_secret_access_key, :region => aws_region }
tmpfile = Tempfile.new('forecast')
tmpfile.write(data)
tmpfile.close
s3 = Aws::S3::Client.new(region:aws_region)
resp = s3.put_object(
:bucket => aws_bucket,
:key => aws_bucket_key_forcast,
:body => IO.read(tmpfile)
)
tmpfile.unlink
end

Paperclip processor timeout on S3

I'm inserting a digital signature into a pdf file inside my processor, but keep getting an AWS::S3::ERRORS::Requestimeout error. What is this timeout? Is there any way i could keep the connection open until the file gets uploaded ?
Your socket connection to the server was not read from or written to
within the timeout period. Idle connections will be closed.
Here is my code :
model:
...
has_attached_file :receipt_file,
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "/:style/:id/:filename",
:s3_protocol => "https",
:styles => {dummy:""},
processors: [:SignPdf]
#process_in_background :receipt_file
...
Processor
module Paperclip
class SignPdf < Processor
attr_accessor :receipt_id,:style
S3_CONFIG = YAML.load_file("#{::Rails.root}/config/s3.yml")[Rails.env]
ORIGAMIDIR = "/ruby/1.9.1/gems/origami-1.2.4/lib"
def initialize(file, options = {}, attachment = nil)
#file = file
#current_format = File.extname(#file.path)
#basename = File.basename(#file.path, #current_format)
#attachment = attachment
end
def make
signPdf(#file)
#file
end
end
end
begin
require 'origami'
rescue LoadError
$: << ORIGAMIDIR
require 'origami'
end
include Origami
def signPdf(file)
certFile = "#{::Rails.root}/lib/assets/Cert.pem"
rsakeyFile = "#{::Rails.root}/lib/assets/pk.pem"
passphrase = "o2Receipts"
key4pem=File.read rsakeyFile
key = OpenSSL::PKey::RSA.new key4pem, passphrase
cert = OpenSSL::X509::Certificate.new(File.read certFile)
pdf = PDF.read(file)
page = pdf.get_page(1)
# Add signature annotation (so it becomes visibles in pdf document)
sigannot = Annotation::Widget::Signature.new
sigannot.Rect = Rectangle[:llx => 89.0, :lly => 386.0, :urx => 190.0, :ury => 353.0]
page.add_annot(sigannot)
# Sign the PDF with the specified keys
pdf.sign(cert, key,
:method => 'adbe.pkcs7.sha1',
:annotation => sigannot,
:location => "Portugal",
:contact => "email#email.pt",
:reason => "Proof of Concept"
)
# Save the resulting file
pdf.save(file.path)
file
end
I have worked around this, by using the after save. See my answer related to this subject here
What you're looking for isn't in the documentation as of today. You'll need to create an AWS::S3::Client
I'm referencing: https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/storage/s3.rb#L263
config.paperclip_defaults = {
storage: :s3,
s3_credentials: "#{Rails.root}/config/s3.yml",
s3_region: ENV['AWS_REGION'],
s3_protocol: :https,
s3_options: {
client: Aws::S3::Client.new(
access_key_id: ENV['S3_KEY'],
secret_access_key: ENV['S3_SECRET'],
http_open_timeout: 10,
http_read_timeout: 5,
http_idle_timeout: 20
)
}
}

rails - Model Methods, (Getting a PaperClip image URL off S3)

I have a rails 3 app with paperclip. The model looks a little like:
class Attachment < ActiveRecord::Base
has_attached_file :attachment,
:styles => {
:large => '1024x758>',
:medium => "200x150#",
:thumb => "100x75#",
:small => "50x50>"
},
:default_style => :original,
:default_url => '/images/:attachment/default_:style.png',
:path => ":instance_id/:attachment/:id/:style/:basename.:extension",
:storage => :s3,
:s3_credentials => File.join(Rails.root, 'config', 's3.yml'),
:s3_protocol => 'https',
:s3_permissions => :private,
:use_timestamp => false
def authenticated_url(style = nil, expires_in = 90.minutes)
AWS::S3::S3Object.url_for(attachment.path(style || attachment.default_style), attachment.bucket_name, :expires_in => expires_in, :use_ssl => attachment.s3_protocol == 'https')
end
This is called from a user_mailer which is called via delayed_job:
In user_mailer.rb mailer it looks a little something like this:
#comment.attachments.each do |a|
attachments[a.attachment_file_name] = open(a.authenticated_url()) {|f| f.read }
end
The issue here is that delayed mailer is erroring with:
{uninitialized constant Attachment::AWS
/Users/bhellman/Sites/cline/app/models/attachment.rb:53:in `authenticated_url'\n/Users/bhellman/Sites/cline/app/mailers/user_mailer.rb:41:in
`conversation_notification'\n/Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/association_collection.rb:430:in
`method_missing'\n/Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/association_proxy.rb:216:in `method_missing'\n/
Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/association_proxy.rb:216:in
`each'\n/Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/association_proxy.rb:216:in
`send'\n/Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/association_proxy.rb:216:in `method_missing'\n/Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/association_collection.rb:430:in `method_missing'\n/Users/bhellman/Sites/cline/app/mailers/user_mailer.rb:39:in `conversation_notification'\n/Library/Ruby/Gems/1.8/gems/actionpack-3.0.0/lib/abstract_controller/base.rb:150:in `send_action'\n/Library/Ruby/Gems/1.8/gems/actionpack-3.0.0/lib/abstract_controller/base.rb:150:in `process_action'\n/Library/Ruby/Gems/1.8/gems/actionpack-3.0.0/lib/abstract_controller/base.rb:119:in `process'\n/Library/Ruby/Gems/1.8/gems/actionpack-3.0.0/lib/abstract_controller/rendering.rb:40:in `process'\n/Library/Ruby/Gems/1.8/gems/actionmailer-3.0.0/lib/action_mailer/old_api.rb:75:in `process'\n/Library/Ruby/Gems/1.8/gems/actionmailer-3.0.0/lib/action_mailer/base.rb:446:in `process'\n/Library/Ruby/Gems/1.8/gems/actionmailer-3.0.0/lib/action_mailer/base.rb:441:in `initialize'\n/Library/Ruby/Gems/1.8/gems/actionmailer-3.0.0/lib/action_mailer/base.rb:425:in `new'\n/Library/Ruby/Gems/1.8/gems/actionmailer-3.0.0/lib/action_mailer/base.rb:425:in `method_missing'\n/Library/Ruby/Gems/1.8/bundler/gems/delayed_job-411719b38c51/lib/delayed/performable_mailer.rb:6:in `send'\n/Library/Ruby/Gems/1.8/bundler/gems/delayed_job-411719b38c51/lib/delayed/performable_mailer.rb:6:in `perform'\n/Library/Ruby/Gems/1.8/bundler/gems/delayed_job-411719b38c51/lib/delayed/backend/base.rb:83:in `invoke_job'\n/Library/Ruby/Gems/1.8/bundler/gems/delayed_job-411719b38c51/lib/delayed/worker.rb:119:in `run'\n/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:62:in `timeout'\n/Library/Ruby/Gems/1.8/bundler/gems/delayed_job-411719b38c51/lib/delayed/worker.rb:119:in `run'\n/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/benchmark.rb:308:in `realtime'\n/Library/Ruby/Gems/1.8/bundler/gems/delayed_job-411719b38c51/lib/delayed/worker.rb:118:in `run'\n/Library/Ruby/Gems/1.8/bundler/gems/delayed_job-411719b38c51/lib/delayed/worker.rb:176:in `reserve_and_run_one_job'\n/Library/Ruby/Gems/1.8/bundler/gems/delayed_job-411719b38c51/lib/delayed/worker.rb:103:in `work_off'\n/Library/Ruby/Gems/1.8/bundler/gems/delayed_job-411719b38c51/lib/delayed/worker.rb:102:in `times'\n/Library/Ruby/Gems/1.8/bundler/gems/delayed_job-411719b38c51/lib/delayed/worker.rb:102:in `work_off'\n/Library/Ruby/Gems/1.8/bundler/gems/delayed_job-411719b38c51/lib/delayed/worker.rb:77:in `start'\n/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/benchmark.rb:308:in `realtime'\n/Library/Ruby/Gems/1.8/bundler/gems/delayed_job-411719b38c51/lib/delayed/worker.rb:76:in `start'\n/Library/Ruby/Gems/1.8/bundler/gems/delayed_job-411719b38c51/lib/delayed/worker.rb:73:in `loop'\n/Library/Ruby/Gems/1.8/bundler/gems/delayed_job-411719b38c51/lib/delayed/worker.rb:73:in `start'\n/Library/Ruby/Gems/1.8/bundler/gems/delayed_job-411719b38c51/lib/delayed/tasks.rb:9\n/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call'\n/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `execute'\n/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `each'\n/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `execute'\n/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain'\n/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'\n/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'\n/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'\n/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'\n/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'\n/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `each'\n/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'\n/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'\n/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'\n/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in `run'\n/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'\n/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in `run'\n/Library/Ruby/Gems/1.8/gems/rake-0.8.7/bin/rake:31\n/usr/bin/rake:19:in `load'\n/usr/bin/rake:19
Any idea what could be going on?
You need to require "aws/s3" in your model.

Resources