require 'httparty' require 'tempfile' class AmazonCopy def download_video videolisting if not videolisting.on_s3 download_file_and_upload_to_s3 videolisting.get_video_url, videolisting.videocode, "mp4", "bvideos" download_file_and_upload_to_s3 videolisting.get_preview_url, videolisting.videocode, "mp4", "bvideos_preview" videolisting.on_s3 = true videolisting.save end end handle_asynchronously :download_video def download_file_and_upload_to_s3 url, videocode, extension, prefix file = Tempfile.new ["video_", ".#{extension}"], "#{Rails.root}/tmp" begin file.write HTTParty.get(url).parsed_response.force_encoding("utf-8") file.flush s3 = AWS::S3.new obj = s3.buckets[ENV['VIDEO_BUCKET']].objects["#{prefix}/#{videocode}.mp4"] obj.write(file) ensure file.close file.unlink end end end