In our 14 Advent Calendar post, Inpsyder Cristiano gives an overview about how you can integrate Cloud Storage in WordPress projects.
Usually basic handling of WordPress uploads is a no brainer: install WordPress and you’re done. There are basic settings you can configure, as the structure of the uploads directories, thumbnail sizes, what mime types can be uploaded and others, but you can go with the defaults.
This works fine if you’re serving WordPress from a single server, but if your website needs to handle a lot of traffic, it most likely needs to be running on a cluster of servers behind a load balancer. This means your uploads directory need to be shared by all the servers in the cluster and also that you either have hosting that handle this for you or you need to handle that yourself with a centralized media server, data syncing between servers or most likely cloud storage.
With this article, I’m giving a high-level overview of why and how you can integrate cloud storage into your WordPress project.
Why Cloud Storage?
Using a centralized media server would be a possibility, but this means adding extra complexity to your infrastructure, requires networking know-how and maybe modifying the way WordPress handles its uploads.
Data syncing between servers could possibly work as well, but once again you are adding extra complexity to your infrastructure, files might not be instantly accessible on all servers and all kinds of syncing issues could happen and time would be lost tracking down, debugging and fixing those issues.
With cloud storage you will interact with a single HTTP API and offload all media requests from your servers and move the technical responsibility to the third-party provider, including availability, backups, versioning (usually optional) and access control. You will also hit the ground running without having to do any in-house development to move around these files.
Most of the obvious cloud storage providers also have integrations with other services and products they provide, so the possibilities do not end with just uploading and serving files.
Integrating Cloud Storage in WordPress
So what would you need to move your WordPress library to the cloud? First, you would obviously need to select a cloud storage provider that has an API that at least allows the basic CRUD operations, then choose the structure of the uploads directory, decide what metadata you need to store if any, and at last, hook into WordPress in the right places.
If you’re migrating a project that already contains uploads, you also need to first copy all those uploads to your cloud storage, making sure the correct accessibility is set for all those, which for WordPress uploads usually means public accessibility for everything.
Flysystem and its adapters
There is a great PHP project named Flysystem by The League of Extraordinary Packages that allows easy integration with many of the most relevant cloud storage providers through adapters. If you don’t want to handle everything by yourself, using this library would be a good and fast start.
Uploading WordPress files to cloud storage
The first step into the WordPress integration would be to hook into the wp_update_attachment_metadata filter. This way you make sure every single WordPress upload goes to your cloud storage.
Modifying WordPress media URLs
To make sure all your WordPress API calls will return the correct URLs for your media, you need to hook into the wp_get_attachment_url, wp_get_attachment_metadata and wp_calculate_image_srcset filters.
Attachments meta data
For a basic configuration, if the cloud storage provider allows you to mirror your WordPress uploads directory in their system and also gives you URLs that match that structure, you don’t need to store any meta data at all, since all you need is a base URL to prefix all your uploads with. Nonetheless, there are cases, like Dropbox, that will generate an URL that does not match the uploads directory structure, so you need to store meta data for the original file and all the intermediate sizes of the attachment, containing the URL that Dropbox generates for each one of them.
Relevant Cloud Storage perks
With Google Cloud and AWS at the forefront of the cloud providers list in all ways imaginable, the advantages/possibilities of having your uploads in the cloud are immense, but I wanted to list two advantages that I find very useful and quite common.
1. Trigger events after upload
Depending on the provider of your choice, you could be able to optimize or watermark images, transcode videos or generate video thumbnails and trigger any other flow that you might need for your business. Obviously, you could do all this on your application server with a good amount of invested time, but offloading these tasks to the cloud ensures the processing power of your servers are only focused on serving WordPress and most likely simplifies DevOps for your application servers.
2. Restricted access
If you ever need to implement restrict access to your uploads, you can do so by marking the visibility of those file uploads as private and use signed URLs or Access Control Lists to display any private media whenever needed.
Despite being an article that just scratches the surface of WordPress integration with cloud storage, I hope this article is enough to get you a kickstart into trying this for yourself and maybe help you find the best solution for your WordPress media management challenges.