Zend certified PHP/Magento developer

Magento2: Remove cache part from media url when using remote S3 storage

I migrated Magento 2 (version 2.4.6 deployed via Helm chart on Kubernetes) media folder to an AWS S3 bucket following the instructions from this documentation: https://experienceleague.adobe.com/docs/commerce-operations/configuration-guide/storage/remote-storage/remote-storage-aws-s3.html?lang=en. It worked successfully, and I observed that Magento had migrated the media folder to the bucket, only storing the cache locally in /var. However, later, when I attempted to use AWS CloudFront CDN to route all requests with /media/* in the path to S3, I noticed that it wasn’t working as expected. Magento stopped displaying the images, and the image requests were returning a 403 error.

After some investigation, I realized that the problem was related to the image URLs containing a cache segment. For example, https://mysite.com/media/catalog/product/cache/b71e4bc1e267574dger6f046eb2626ba8af/1/1/image.jpg, and this cache folder with the specific ID didn’t exist in my S3 bucket. Further testing revealed that if I didn’t use CloudFront for media, allowing the requests to be processed by Magento, the cache folder with the correct ID was created in the S3 bucket, and the images were accessible. However, if I accessed the images through CloudFront to S3 without first going through Magento, the necessary folder wasn’t being created.

This behavior of Magento raised some doubts. Is it normal behavior by default, or is there something wrong with the configuration? Is it possible to configure Magento to generate correct links without the cache segment? I believe that without this cache segment in the URL, everything should work correctly, as all the images are actually stored in the catalog/product folder, and Magento, for some reason, copies them into the cache folders.”