All Blog Articles

Integrating Payload NextJS with S3 Object Storage: A Step-by-Step Guide

Media
March 13, 2025
Conecte sin problemas Payload CMS con el almacenamiento de objetos S3 para una gestión eficiente de archivos en su aplicación NextJS.

In this fourth part of our Payload NextJS setup series, we'll walk through how to seamlessly integrate S3 object storage into your Payload project. This straightforward process will ensure your file uploads are efficient and reliable.

This step-by-step guide provides a comprehensive written walkthrough for integrating S3 object storage with your Payload CMS and NextJS application. For visual learners or those who prefer following along with video instruction, you can find the complete video tutorial on YouTube at the following link:
https://www.youtube.com/watch?v=25BCE_hx2yA
The video covers the same content but offers additional context and real-time demonstrations of each step in the process.


Table of Contents

Introduction to S3 Storage in Payload NextJS

Integrating S3 storage into your Payload NextJS project enhances file management capabilities. With S3, you can store, retrieve, and manage files efficiently, ensuring your application scales without limitations. This guide will walk you through the steps to set up S3 storage, focusing on using Linode as your cloud provider.

Creating Your Linode Account

To get started, you'll need a Linode account. If you don’t have one, visit the Linode website and sign up. Linode offers a referral link that gives new users $100 in credits for 60 days. This is a great way to explore their services without any initial costs.

Creating-New-Linode-Account.png


Setting Up Your Storage Bucket

Once your account is ready, log in and navigate to the object storage section. Here’s how to create your storage bucket:

  1. Click on Object Storage in the left panel.
  2. Select Create Bucket.
  3. Enter a name for your bucket; for example, basic setup bucket.
  4. Choose a region that is closest to your target audience, like Frankfurt, Germany.
  5. Click on Create Bucket to finalize the process.


Creating Access Keys for Your Bucket

Next, you'll need to create access keys that will allow your application to interact with the bucket. Follow these steps:

  1. In the Access Keys section, click on Create Access Key.
  2. Label your key, for instance, basic setup key.
  3. Enable limited access to restrict the key's permissions to your newly created bucket.
  4. Set permissions to Read and Write for your specific bucket.
  5. Click on Create and ensure you copy both the access key and secret key for later use.


Installing Required Libraries in Payload Project

With your bucket and access keys ready, it's time to set up your Payload project. Open your terminal and navigate to your project directory. You will need to install necessary libraries for S3 integration:

npm install @payloadcms/plugin-cloud-storage

Make sure to check the documentation for any additional libraries you might need.

Installing-Required-Libraries -Payload-Project.png


Configuring Environment Variables

Environment variables are crucial for securely storing sensitive data such as access keys. Create a `.env` file in your project root and add the following variables:

  • BUCKET_NAME - Set this to your bucket name, e.g., basic setup bucket.
  • ACCESS_KEY - Paste your access key here.
  • SECRET_KEY - Paste your secret key here.
  • ENDPOINT - Use the S3 endpoint provided by Linode, adjusted for your region.
  • REGION - Specify the region, e.g., eu-central-1.


Creating a Storage Adapter in Payload

The final step involves creating a storage adapter in your Payload configuration. This adapter connects your Payload project to the S3 storage. Here's how to set it up:

  1. Open your payload.config.js file.
  2. At the top of the file, import the S3 adapter:

import s3Adapter from '@payloadcms/plugin-cloud-storage/s3';

  1. Create an adapter configuration object that includes your credentials.
  2. Add the cloud storage plugin to the plugins array in your configuration:

plugins: [s3Adapter(config)]

  1. Specify the collections you want to enable S3 for, such as media.
Creating-Storage-Adapter-Payload.png


Adding the Cloud Storage Plugin

Now that you've set up your Linode account and created a storage bucket, it’s time to integrate the Cloud Storage Plugin into your Payload NextJS project. This plugin will facilitate seamless interactions between your application and the S3 storage. Let’s dive into the integration process.

First, make sure you navigate to your project directory in the terminal. You’ll need to install the Cloud Storage Plugin for Payload. Use the following command:

npm install @payloadcms/plugin-cloud-storage

This command installs the necessary library to enable S3 storage in your Payload project.


Creating a Media Collection for Testing

With the plugin installed, the next step is to create a media collection that will utilize the S3 storage. This collection will allow you to upload and manage media files directly through your Payload dashboard.

To create a media collection, follow these steps:

  1. Create a new file named media.js in your collections directory.
  2. In this file, define your media collection as follows:
export const media = {
        slug: 'media',
        labels: {
            singular: 'Media',
            plural: 'Media',
        },
        upload: true,
        fields: [
            {
                name: 'file',
                type: 'upload',
                relationTo: 'media',
                required: true,
            },
        ],
    };


Ensure that the slug matches the one you configured in the cloud storage adapter. This consistency is essential for the integration to work smoothly.


Testing File Uploads

Now that your media collection is set up, it’s time to test the file upload functionality. Start your development server by running the following command in your terminal:

npm run dev

Once the server is running, navigate to http://localhost:4000 in your web browser. You’ll need to create your first user to access the dashboard. After creating a user, log in to your Payload dashboard.

In the dashboard, select your media collection and click on Create New. Here, you can upload a file. Choose an image or any media file you want to test with.

Payload-file-upload-testing.png


Verifying Uploads in Linode

After successfully uploading a file, it’s crucial to verify that the upload went through correctly. Head back to your Linode account and navigate to the object storage section.

Locate your basic setup bucket and check the contents. You should see the file you uploaded reflected here.

For example, if you uploaded an image, it should appear in the bucket with the corresponding size. You can click on the file to view its details or download it to ensure that it’s intact.


Conclusion and Next Steps

Congratulations! You’ve successfully integrated Payload NextJS with S3 object storage using Linode. This integration allows for efficient media management and enhances the capabilities of your application.

As a next step, consider exploring additional features of the Payload CMS. You can customize your media collection further by adding more fields or implementing different types of media handling. Additionally, make sure to monitor your storage usage and optimize your bucket settings as your application scales.

FAQ

What is Payload NextJS?

Payload NextJS is a flexible content management system that allows developers to create custom applications with ease. It provides a rich set of features for managing content, including file uploads and integrations with cloud storage providers.

Why use S3 storage with Payload?

S3 storage offers reliable, scalable, and cost-effective solutions for managing media files. Integrating S3 with Payload NextJS ensures that your application can handle large amounts of media without compromising performance.

Can I use other cloud providers with Payload?

Yes! Payload supports various cloud storage providers, including Google Cloud Storage and DigitalOcean Spaces. You can easily switch providers by configuring the appropriate adapter in your Payload setup.