How to Prevent S3 Signed Url Image Caching in Android Webview

Scenario:

Image is hosted in Amazon S3. We are generating signed URL to display image in browser/Android WebView. Signed URLs are generated from backend server using boto3. On client site, this signed URL is made available using a API.

Problem:

The image is cached in Android WebView. If the image is updated, the new image is not visible to users.

Solution:

We use the Cache-Control header with no-store. Amazon S3 provides option to set custom headers. Below is the syntax for setting custom header using boto3.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11

s3.generate_presigned_url(
                    ClientMethod='get_object',
                    Params={
                        'Bucket': 's3-bucket',
                        'Key': 'image_key',
                        'ResponseCacheControl': 'no-store'
                    },
                    ExpiresIn=3600
                )