Apache - retrieve file from S3 if it does not exist on disk



Published: 2018-03-02 15:37:39 +0000
Categories: Misc,

Language

Misc

Description

On hosted systems with limited disk space, you might occasionally want to archive files out to Amazon S3 (or some other filer). This would usually result in URLs changing, though, so the ideal situation would be to have Apache simply proxy the request through to your filer if it cannot find the file on disk

This snippet checks for the file within the document root and if it does not exist, sends a request our to an S3 bucket

Snippet

SSLProxyEngine On

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ https://s3.[amazonregion].amazonaws.com/[yourbucketname]/$1 [P,QSA,L]

Usage Example

SSLProxyEngine On

RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ https://s3.eu-west-3.amazonaws.com/examplebucket/$1 [P,QSA,L]

Requires

  • mod_rewrite
  • mod_ssl
  • mod_proxy

Keywords

apache, proxy, conditional proxy, Amazon S3, filer,

Latest Posts


Copyright © 2022 Ben Tasker | Sitemap | Privacy Policy
Available at snippets.bentasker.co.uk, http://phecoopwm6x7azx26ctuqcp6673bbqkrqfeoiz2wwk36sady5tqbdpqd.onion and http://snippets.bentasker.i2p
hit counter