Search code examples
javaspringamazon-web-servicesamazon-s3storage

Storing Images on File System vs S3 and accessing them easily


Im building a marketplace application and want to store user uploaded images. The images are going to be viewable by any non-authentcated/authenticated user. It seems like S3 is the only solution I could find. But having pre-signed urls for all the images on the page, would presumably be a lot of get request. Specially as a user goes back and forth between categories. So im not sure how realistic it would be or if I need a cache system. I really do not want to overcomplicate it so if there is a better solution im open to it.


Solution

  • Cache Friendly Pre-Signed URL:s

    Creating a pre-signed url is a purely client-side operation and has neither a cost or limit associated to it. Hence, I would not be concerned about the actual creation of url:s.

    However, signing new urls for every load will make browsers unable to cache your images because the url will always change.

    To get around that you can make sure to sign all url:s during a set time-span with the same expiration time. That way the image url:s will be identical between requests for a set amount of time. For example you could always set the expiration to be the next following quarter hour, like this:

    private String generateCacheFriendlyUrl(String fileName, HttpMethod httpMethod) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
    
        // Add 5 minutes to make sure we don't get an immediately expiring url 
        calendar.add(Calendar.MINUTE, 5); 
        
        // Round time up to next quarter hour
        int unroundedMinutes = calendar.get(Calendar.MINUTE);
        int mod = unroundedMinutes % 15;
        calendar.add(Calendar.MINUTE, 15-mod);
    
        return amazonS3.generatePresignedUrl(s3BucketName, fileName, calendar.getTime(), httpMethod).toString();
    }
    

    That way images can be cached by browsers during the time-span where url:s have the same expiration time.