Ruby on Rails 3.2.11 : Using S3 as Paperclip’s Storage

Actually, this post was intended as a self note for me. So, it will be easier for me to set things up. Well, this story came up when I have a need of S3 for collecting my static files and user uploaded files, then I started my searching. I assume you already have your storage at S3, so I don’t need to explain how to setup a storage at S3 anymore. These are the steps that I did :

  1. Add these 3 gems to your Gemfile
    gem "paperclip", '~> 3.0'
    gem "aws-sdk"
    gem "aws-s3"
  2. Add a file named s3.yml (you can name it whatever you want, but I prefer to use s3) and place it inside config folder. You can see the content of the file below
      access_key_id: YOUR_ACCESS_KEY
      secret_access_key: YOUR_SECRET_ACCESS_KEY
      access_key_id: YOUR_ACCESS_KEY
      secret_access_key: YOUR_SECRET_ACCESS_KEY
      access_key_id: YOUR_ACCESS_KEY
      secret_access_key: YOUR_SECRET_ACCESS_KEY
  3. You’ve finished for S3 configuration part, now let’s start with the paperclip stuff. I assume you’ve made the migration file for a model you wish, let’s say the model is user. For example, this is the configuration that you need to add at your user model file
    has_mongoid_attached_file :avatar,
      :styles => {
        :small => "100x100>",
        :thumb => "200x200#",
        :large  => "500x500>" },
      :storage => :s3,
      :url            => ':s3_alias_url',
      :s3_host_alias  => '', #this value depends on your bucket host alias, which is defined at S3 setup at AWS
      :s3_credentials => File.join(Rails.root, 'config', 's3.yml'), #this value depends on the file name that you made at the config file
      :path => "/avatars/:style/:id_:filename"

That’s it! Now run your web app and try uploading something to your model and see your bucket after that. I believe the file will be in the bucket.

Happy coding and cheers!

Sphinx Setup on Mac OS X 10.7

My previous post was talking about how to use thinking sphinx (ruby gem for sphinx) with MySQL and now I would like to share about how to set sphinx up, especially at Mac OS X 10.7 machine. I assume you guys have installed RDBMS (MySQL or PostgreSQL) at your machine.

First, you need to download sphinx at sphinx download page and download the source tarball. Extract the file and get into the folder. Then, execute these commands to install sphinx to your mac machine.

> ./configure
> make
> sudo make install

Continue reading “Sphinx Setup on Mac OS X 10.7”

Search Engine with Ruby on Rails 3 and Thinking Sphinx + MySQL

My previous research is about creating a search engine and now, I want to share about it to you guys.

If anyone asks you about how to perform a simple search in MySQL, you may suggest add INDEX or FULLTEXT INDEX to the columns and use LIKE or MATCH query. Yes, it’s not wrong, but for some circumstances I suggest you to reconsider using those queries. A couple of days ago, I performed a simple search with MATCH query which involved about a million of records. Do you know how long it took to retrieve the data? Well, it’s about 2 minutes. My boss was mad at me and said that nobody is willing to use the application if it has that kind of performance.

One of the solutions that you can use is using sphinx. It provides you some methods in indexing the data, so the application will never actually hit the database for some query. In a simple explanation, you give some query that you want to index to sphinx, then sphinx will create one or more indexes file (depend on how many query that you want to have), next the application perform the search through the index file. I’ll tell you this, to retrieve about 80000 records in table which is containing 1000000 records, sphinx will give you the results in milli seconds. What a performance, right?
Continue reading “Search Engine with Ruby on Rails 3 and Thinking Sphinx + MySQL”