Monday, November 05, 2012

S3FS: Mounting an S3 Bucket in Ubuntu


Recently I had an app on EC2 that needed to manage files in an S3 bucket. Normally I would use a gem to handle the uploading etc., but I figured I would give s3fs a crack.

Essentially s3fs allows you to mount an S3 bucket as an external file system, allowing you to manage files transparently as part of the OS.

It was relatively simple to get it working on an EC2 instance running Ubuntu 10.04.3 LTS

OVERVIEW:

 - install the dependencies
 - install s3fs
 - create the password file
 - add the s3 bucket to fstab
 - mount and symlink to deployment dir


Installing Dependencies:
$ sudo apt-get install build-essential libfuse-dev fuse-utils libcurl4-openssl-dev libxml2-dev mime-support


Install Fuse 2.8.4:
$ wget http://sourceforge.net/projects/fuse/files/fuse-2.X/2.8.4/fuse-2.8.4.tar.gz/download -O fuse-2.8.4.tar.gz
$ tar xvzf fuse-2.8.4.tar.gz
$ cd fuse-2.8.4
$ ./configure --prefix=/usr
$ make
$ sudo make install


Install S3FS:
$ wget http://s3fs.googlecode.com/files/s3fs-1.61.tar.gz
$ tar xvzf s3fs-1.61.tar.gz
$ cd s3fs-1.61/
$ ./configure --prefix=/usr
$ make
$ sudo make install


Create /etc/passwd-s3fs:
$ sudo vim /etc/passwd-s3fs

Populate it with
[your_aws_access_id]:[your_aws_secret]

Change permissions:
$ sudo chmod 640 /etc/passwd-s3fs

Add Mount Point:
$ sudo mkdir /mnt/bucket

Add to FSTAB:
s3fs#[bucket-name] /mnt/bucket fuse netdev,default_acl=public-read,use_cache=/tmp,use_rrs=1,allow_other 0 0

Symlink to Mount Point:
$ ln -s /mnt/bucket /[deployed_app_path]/public

FYI ONCE OFF MOUNT COMMAND CMD:
$ AWSACCESSKEYID=[your_aws_access_id] AWSSECRETACCESSKEY=[your_aws_secret] sudo /usr/bin/s3fs [bucket-name] /mnt/bucket -odefault_acl=public-read