Mount Amazon S3 Buckets on Ubuntu
I wanted the ability to mount a S3 bucket straight onto my Linux server so I wouldn’t have to rely on local storage, I have a write up on how to do this:
You can do this without installing FUSE just by installing s3fs but that only allows the user who mounted it to access the bucket and it is also not possible to mount using /etc/fstab
Before you run any commands, make sure your system is up to date by running:
apt-get update && apt-get -y upgrade
Ubuntu Dependancies
apt-get install build-essential pkg-config libfuse-dev libcurl4-openssl-dev libxml2-dev automake
Check Fuse Package
pkg-config --modversion fuse
Install s3fs
Download the latest verion of s3fs from the Google Code here
Download the latest version of S3fs from Github
unzip master.zip
Change directory into the newly extracted s3fs folder
./autogen.sh
./configure
make
make install
Configure time
This is important and missing from most how-to guides! If the time of the local server isn’t within 15 minutes of the Amazon server time, it won’t connect.
Install NTP
apt-get install ntp
Edit ntp.conf
vi /etc/ntpd.conf
Comment out the existing time servers and add the following
server 0.amazon.pool.ntp.org iburst
server 1.amazon.pool.ntp.org iburst
server 2.amazon.pool.ntp.org iburst
server 3.amazon.pool.ntp.org iburst
Restart ntpd
/etc/init.d/ntp restart
Check the time of the Amazon server
telnet s3.amazonaws.com 80
GET / HTTP/1.0
<enter>
It should give you some output like this example:
telnet s3.amazonaws.com 80
Trying 54.231.244.4...
Connected to s3-2.amazonaws.com.
Escape character is '^]'.
GET / HTTP/1.0
HTTP/1.1 307 Temporary Redirect
x-amz-id-2: iXm0zQv6nf7WAeXYC3rnenEAxPjgnGHKtQP9nNeU82D1hYqDlFvItwFtpJ2u2fP6
x-amz-request-id: FB13695A8C2A8539
Date: Tue, 25 Nov 2014 23:49:00 GMT
Location: http://aws.amazon.com/s3/
Content-Length: 0
Connection: keep-alive
Server: AmazonS3
Connection closed by foreign host.
Change the timezone on your server to GMT time (as this is the timezone of the Amazon server)
dpkg-reconfigure tzdata
Select “None of the above”
Select “GMT”
Set the date and time on your server
Change the values to the same as the Amazon server time
date --set YYYY-MM-DD
date --set HH:MM:SS
Configuring s3fs
Create a file called “passwd-s3fs under /etc
vi /etc/passwd-s3fs
Put your access key and secret access key seperated by a “:”
Example: ACCCESS_KEY_ID:SECRET_ACCESS_KEY
Change the permissions for security reasons
chmod 640 /etc/passwd-s3fs
Create a tmp folder for caching
mkdir -p /tmp/cache
Create a mount point
mkdir -p /mnt/aws
Change permissions on cache and mount point folders
chmod 777 /mnt/aws /tmp/cache
Mounting s3 Bucket
Mount bucket
s3fs -o allow_other -o use_cache=/tmp/cache your-bucket-name /mnt/aws
Allow a few minutes to sync
Check to see if it is mounted:
df -h
Example:
Automounting
To mount automatically at boot, your /etc/fstab should look like this:
s3fs#bucketname /mnt/aws fuse allow_other 0 0
A brief description of the mount arguments:
- Allow_other = allow all users to access the mount point
- _netdev = The filesystem resides on a device that requires network access
- nosuid = Do not allow set-user-identifier or set-group-identifierbits to take effect
- nodev = Do not interpret character or block special devices on the file system
- url = Use HTTPS instead of HTTP when configure as above
Should you have any questions, comments or suggestions, please don’t hesitate to comment below. If you like what you have read, please share it on your favourite social media medium.
Scripts to mount bucket
Mounting
Replace “bucketname” with the bucket you wish to mount and change the mount location (/mnt/aws)
#!/bin/bash
/etc/init.d/ntp start
sleep 1m
s3fs -o allow_other -o use_cache=/tmp/cache bucketname /mnt/aws
Unmounting
#!/bin/bash
fusermount -u /mnt/aws
Errors
RequestTimeTooSkewed
The difference between the request time and the current time is too large
This is caused because the Amazon server time is not within 15 minutes of your local server time, check to make sure you have set the correct timezone, date and time in the instructions above.
Troubleshooting
1. The best way to find out if there are any problems is to run s3fs with the -f switch as it will give you full output of the what it is trying to do.
2. Check /var/log/syslog for any messages