TheDotProduct

Web-tech nerd stuff

Mounting a Google Cloud Storage Bucket as a local disk

These days it's a pretty common usage pattern to store files of all sorts in a "storage as a service" offering such as Google Cloud Storage (GCS). It's simple enough to push and pull content to and from GCS but if, for example, you want to store your static website in GCS and serve it to the internet, what do you do? A simple solution might be to periodically sync the content of your GCS bucket to the local filesystem of your server(s) but that's got inherent lag due to the periodicity of the sync. Enter GCSFuse...

GCSFuse

GCSFuse is an implementation of the Fuse filesystem which allows you to mount your GCS bucket on your server(s). That's pretty awesome as it means that functionally, you can treat your GCS bucket and the files (well, technically objects since GCS is an object store, not a filesystem) it contains as if they were on a physical or networked volume. Admittedly there's some lag in the transfer to and from GCS but for many read-only use cases (as a static website would be), that's only really an issue on the first access of a file as the file is cached by the operating system. You're currently reading content served in exactly this way.

If this sounds like a solution you might find useful, the good news is that it's incredibly simple to get started. I'm going to assume that your use case is like mine, i.e.:

Installation

Follow the installation instructions for your OS. This essentially boils down to:

It really is that simple

Configuration

All you need to do now is mount the volume in read-only mode via your fstab. We'll assume that:

So, replace MY-GCS-BUCKET with the name of your GCS bucket and you can run:

# Create the mount point in your local filesystem
sudo mkdir /mnt/MY-GCS-BUCKET

# Add an entry to your `fstab` to ensure the bucket is mounted even after reboot
sudo echo "MY-GCS-BUCKET /mnt/MY-GCS-BUCKET gcsfuse ro,uid=33,gid=33,noatime,_netdev,noexec,user,implicit_dirs,allow_other 0 0" >> /etc/fstab

# Mount the bucket
sudo mount /mnt/MY-GCS-BUCKET

Explaining the mount options:

Serving

You should now be able to configure your web server with a document root of the mount point (/mnt/MY-GCS-BUCKET) and you're done!

Created: Sat, 30 Sep 2017 19:54:20 GMT
Last modified: Sat, 30 Sep 2017 20:40:17 GMT