Replace S3 with DigitalOcean as asset server for Spree commerce

In this post I will show steps need to setup DigitalOcean as asset server for Spree projects. So before going into any detail, I want to explain a bit the reason I did it. S3 was great for me. It’s simple to setup and use. Until one day my bill for Amazon S3 go up to ~7k USD/month. I was freak out. And the reason was, one of our dev guys have use aws key for testing purpose and he was not careful enough. And the rest of the story you already know what was going on. Then I decided to setup the asset server on DigitalOcean with only 5$/month and I got 20Gb of SSD storage. Which I think pretty good.

Ok. So here is what you need to make it happen.

First, add the following gem to your Gemfile `paperclip-storage-ftp` at https://github.com/xing/paperclip-storage-ftp. Second, you gonna need to do the config for Spree. You can add a file called `spree_ftp.rb` under `initializers` folder

attachment_config = {
  storage: :ftp,
  path: "/home/your_project_path/files/:attachment/:id/:style/:filename",
  url: "http://X.X.X.X/:attachment/:id/:style/:filename",
  styles: {
    mini: "48x48>",
    small: "100x100>",
    product: "240x240>",
    large: "600x600>"
  },
  default_style: "product",
    ftp_servers: [
      {
        host: "X.X.X.X",
        user: "your-ftp-user-name",
        password: "your-ftp-password"
      }
    ],
  ftp_connect_timeout: 5,
  ftp_ignore_failing_connections: true,
  ftp_keep_empty_directories: true
}

attachment_config.each do |key, value|
  Spree::Image.attachment_definitions[:attachment][key.to_sym] = value
end

That is all for Spree part. Now for DigitalOcean, you gonna need 2 things.

  • 1 is a nginx server – to serve the static content only (png, jpg,..)
  • 2 ftp server – for your Spree project upload asset to

Setup an nginx server on DigitalOcean quite easy and straight forward. One thing to keep in mind that you need to config Nginx to serve only static content (nothing else) and only authorize public users to access to the asset folder which you defined in the Spree config above.

For FTP server setup, you can follow great tutorial here https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-on-ubuntu-12-04 – This can be easily setup within 15 – 30 minutes. Then one last thing you need to do the config for FTP server also. By edit the following file `/etc/vsftpd.conf`. Here is my config as an example

Screen Shot 2016-03-15 at 9.15.46 PM

That’s all. Now I can upload all of my assets into DigitalOcean. The result was pretty great for me. Nginx handle static images very fast. I happy with it and now I gonna move all images from S3 to DigitalOcean.

Advertisements
Replace S3 with DigitalOcean as asset server for Spree commerce

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s