Magento - Storing all images on cdn not my hosting server

Magento - Storing all images on cdn not my hosting server

August 3, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - Storing all images on cdn not my hosting server

Optimizing Magento for Low Disk Space: A Complete Guide

As a beginner with Magento, you’re likely looking for ways to optimize your site’s performance while minimizing server resource usage. One of the most effective strategies is to offload all image files to a content delivery network (CDN) like Amazon CloudFront.

Understanding the Current Scenario

When using the CloudFront CDN plugin alongside your distribution setup, images are indeed served from the CDN. However, these files still exist on your own server as a backup copy.

This dual storage configuration leads to increased disk space usage on your main server, which is not ideal for maintaining low resource consumption. To resolve this issue and ensure all images reside solely on the CDN, you’ll need to implement additional steps.

Setting Up a Fully Offloaded Image Storage Solution

To achieve an image storage solution that keeps your main server’s disk space at a minimum:

  1. Upload Images Directly to Your CDN

Instead of uploading images directly through Magento’s media folder on your server, upload them to your CloudFront distribution via the AWS management console or an API tool like Boto3.

  1. Update Image URLs in the Database

The next step is to update the database records pointing to these images so they refer to the CDN URL instead of the local one. This can be done using a custom script:

$connection = Mage::getSingleton('core/resource')->getConnection('core_read');

$imageTable = $connection->select()
    ->from($connection->quoteIdentifier('catalog_product_entity_media_gallery'))
    ->where("file IS NOT NULL AND value LIKE '%/media/catalog%/%'");

$result = $connection->fetchAll($imageTable);

foreach ($result as $row) {
    $newUrl = str_replace('/media/catalog', '<your-cdn-url>', $row['value']);
    $updateQuery = "UPDATE catalog_product_entity_media_gallery SET value=? WHERE entity_id=? AND attribute_id=?";
    $connection->query($updateQuery, array($newUrl, $row['entity_id'], $row['attribute_id']));
}

Remember to replace <your-cdn-url> with your actual CloudFront distribution domain.

  1. Configure Magento’s Media URLs

Update the base URLs for both media and skin in your Magento configuration:

<config>
    <global>
        <media>
            <base_url use_base_media_url="1">https://d2h0b7mz689r54.cloudfront.net/</base_url>
        </media>
        <skin>
            <base_url use_base_skin_url="1">https://d2h0b7mz689r54.cloudfront.net/</base_url>
        </skin>
    </global>
</config>

Monitoring and Maintenance

Ensure you regularly monitor your CDN’s performance and usage. Use tools like AWS CloudWatch or third-party services for real-time insights.

Also, set up an automated script to sync new images directly to your CloudFront distribution whenever they are uploaded through the Magento admin panel.

The Benefits of a Fully Offloaded Image Solution

By following this approach, you effectively reduce your main server’s disk space footprint significantly. This leads to improved performance, easier backups, and better control over your infrastructure costs.

At Laravel Company, we specialize in Magento optimization services, helping businesses like yours achieve the best possible performance and resource efficiency.

Conclusion

Offloading all image files to a CDN is a critical step for optimizing Magento sites with low disk space requirements. By combining the CloudFront plugin, direct uploads to your distribution, database updates, and proper configuration, you can maintain a lean and efficient server environment while ensuring superior customer experience through fast image loading.

Remember, the key lies in integrating multiple tools and approaches to create a seamless, fully offloaded solution tailored to your specific needs. Don’t hesitate to reach out to our team at Laravel Company if you require personalized assistance with your Magento setup.

Database Performance API Extensions

Our network