How to Store Ghost Blog Images on Amazon S3 to Optimize Hosting Storage

How to Store Ghost Blog Images on Amazon S3 to Optimize Hosting Storage

By default, Ghost stores any images uploaded to Ghost Admin locally to its filesystem, and delivers them via the same Ghost front-end service which delivers Ghost themes. It’s possible to replace this layer entirely using a custom storage adapter.

Ghost Storage Adapter S3:

This module allows you to read and write images from Amazon S3 instead of storing them locally.

After installing, new images that you save will use an absolute URL to S3. Any requests to /content/images/ will be proxied to S3, so that any previous images in your blog will not be affected.

Step 1: Installation

Navigate to your Ghost project's directory and install the adapter using npm. The easiest way to do this is using the command below:

npm install ghost-storage-adapter-s3
mkdir -p ./content/adapters/storage
cp -r ./node_modules/ghost-storage-adapter-s3 ./content/adapters/storage/s3

Step 2: AWS S3 Configuration

Now login to your AWS and create a new IAM user. Assign the following policy to grant the necessary access rights to your S3 bucket. Save the ACCESS_KEY and ACCESS_SECRET_KEY.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "s3:ListBucket",
            "Resource": "arn:aws:s3:::<your_bucket_name>"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "s3:PutObjectVersionAcl",
                "s3:DeleteObject",
                "s3:PutObjectAcl"
            ],
            "Resource": "arn:aws:s3:::<your_bucket_name>/*"
        }
    ]
}

Remember to replace <your_bucket_name> with the name of your S3 bucket.

Step 3: Configure the Adapter in Ghost

After setting up your IAM user, open the config.js file located in the root directory of your Ghost installation and add a storage block:

"storage": {
  "active": "s3",
  "s3": {
    "accessKeyId": "Put_your_access_key_here",
    "secretAccessKey": "Put_your_secret_key_here",
    "region": "Put_your_bucket_region_here",
    "bucket": "Put_your_bucket_name_here",
    "pathPrefix": "Put_your_bucket_subfolder_here"
  }
},

Replace placeholders with your actual AWS credentials, S3 bucket region, name, and any specific subfolder you might want to use within your bucket

Step 4: Test Your Setup

With everything configured, it's time to test. Upload a new image through Ghost Admin, and confirm that it's being stored and served from Amazon S3 by checking the image URL.

Conclusion

By adapting Amazon S3 for image storage in Ghost, you optimize your blog's performance and scalability, all while keeping your server lean and efficient. This setup not only future-proofs your content delivery but also leverages Amazon’s robust infrastructure for enhanced reliability and availability.

Got questions about using S3 with Ghost? Feel free to leave a comment or reach out for more in-depth assistance. Happy blogging!

Do you enjoy this blog post?

Read more