Open In App

Integrating Google Cloud Storage with WordPress: Storing Media Files

Now, before learning how to integrate Google Cloud Storage with WordPress, let’s try and understand why you might want to do that. To answer our question, let’s imagine that you have built an awesome cat website that allows users to share pics of their cats. The website is getting popular, I mean who doesn’t like cats? Slowly, thousands of users are uploading their cat pics and you have to store GB’s of cat images daily. Now, how would you store all the images? In a normal WordPress setup, your server disk would fill up soon. What would you do next? Add more disks? How long would you keep doing it?

Simply adding more disks to your server would not be feasible because of two reasons: cost and performance. General block storage is generally more expensive, and traversing through large storage will also impact your performance negatively.



Among the many different solutions, one is managed object storage. How about we offload all the images to something like Google Cloud Storage and load them into our website directly from there? It will generally be cheaper and will also not slow down our server.

Now that you know the reason, let’s learn how to offload images and videos from WordPress to Google Cloud Storage, which is a managed object storage service. We are going to use a plugin called WP Offload Media Lite.



Google Cloud Storage

Google Cloud Storage is an S3 compatible object storage service. Object storage is scalable, durable, and secure. Once we store our data in object storage it can be accessed from anywhere, which means the object storage is region independent. You can read more about it here.

How to Integrate Google Cloud Storage with WordPress: Storing Media Files

Step 1: Install WP Offload Media Lite

  1. Go to your WordPress admin dashboard.
  2. Go to Add New Plugin from the sidebar.
  3. Search for WP Offload Media Lite from top right.
  4. Click on Install Now and then Activate.

Install WP Offload Media Lite

Step 2: Create Service Account Key File

  1. Login to Google Cloud Console.
  2. Search for Credentials and open it.
  3. Click Create Credentials > Service Account.
  4. Give the service account any name, like cat images service.
  5. Click Create and continue.
  6. Click Select a role.
  7. Search for Storage Admin and select it.
  8. Click on Done.
  9. Open the newly created service account details by clicking on it.
  10. Click on Keys > Add Key > Create new key.
  11. Select JSON and click Create.

This will download a file to your desktop.

Step 3: Set Connection Method

If you know how to SSH into your server, you should copy the JSON file somewhere like /creds/config-file.json, and add the below code to your wp-config.php.




define( 'AS3CF_SETTINGS', serialize( array(
    'provider' => 'gcp',
    'key-file-path' => '/path/to/key/file.json', // replace the path
) ) );

If you have copied the file to server and added the above code block to wp-config.php, you can skip the steps 4 and 5.

  1. Open the downloaded JSON file in a text editor and copy everything
  2. Go to WP Offload Media settings from the sidebar
  3. Select Google Cloud Storage
  4. Select the third option in Connection Method
  5. Paste everything in the Add Credentials field
  6. Click on Save and Continue

Additionally, you can also select the second option, that is IAM, if your WordPress server is on Google Cloud, and you know what you are doing.

Step 4: Create Bucket

  1. Select Create New Bucket
  2. Enter any name, like, cat-images-bucket
  3. Select any of the region closest to you
    • I suggest choosing a region that starts with Multi Region
  4. Click on Create New Bucket
  5. Leave rest of the options as default

The plugin will now automatically connect your WordPress website with the bucket you just created using the Service Account that we had created in step 2. You can think of a service account as another account that can make changes to your Google Cloud Resources based on the permissions that you have delegated.

In our case, the service account has all the permissions related to storage.

Offload Media

Congratulations! You have successfully integrated Google Cloud Storage with your WordPress website. Any new media files uploaded to your website will now be stored and served from Google Cloud Storage.

If you want, you can also enable the Remove Local Media option. This will store the newly uploaded file only on Google Cloud and not your server. By default, the files will be stored on both your server and Google Cloud Storage.

Remove Local Media

Verification

Now let’s verify if the plugin is working properly and new new images are being served from Google Cloud storage.

  1. Go to Media and normally upload an image
  2. Click on the image after it is uploaded
  3. See File URL in the description

If the URL starts with <bucket-name>.storage.googleapis.com, the image has successfully been upload to the bucket.

The image has been uploaded to Google Cloud Storage

Let’s also verify it from the Google Cloud Console.

  1. Login to Google Cloud Console
  2. Go to Cloud Storage
  3. Click on your bucket name, like cat-images-bucket
  4. Keep opening the folders
    • For example: /wp-content/uploads/2023/11/15205759
  5. You will see a bunch of files

The cat image has been uploaded to the bucket

Conclusion

WP Offload Media Lite is one of the most trusted plugins that you can use to offload media to not just Google Cloud Storage but other providers like AWS too. You can also use the plugin WP-Stateless for it.

You can free up your WordPress server from the burden of performance and space by using Google Cloud storage. You will also be paying less because object storage is generally cheaper that block storage (normal server disk attached to your server).

Frequently Asked Questions

1. What is Google Cloud Storage, and why integrate it with WordPress?

Google Cloud Storage is an S3-compatible object storage service that offers scalability, durability, and security. Integrating it with WordPress helps manage large volumes of media files efficiently, reducing server load and costs associated with storage.

2. Why should I use object storage like Google Cloud Storage instead of traditional server disks for media files?

Object storage, like Google Cloud Storage, is more cost-effective and scalable than traditional block storage. Storing media files on a server’s disk can lead to performance issues and increased expenses as storage needs grow.

3. Can I use other providers besides Google Cloud Storage?

Yes, WP Offload Media Lite supports other providers like AWS. You can also consider using WP-Stateless for similar functionality.

4. Does integrating Google Cloud Storage affect existing media files on my WordPress site?

By default, existing media files remain on your server and Google Cloud Storage. You can choose to enable the option to remove local media, storing new uploads exclusively on Google Cloud Storage.


Article Tags :