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

Remotely backing up PFsense Configuration (BASH)
FFMPEG: Converting RMVB to X264 MP4 (BASH)
Recursively print table (print_r equivalent) (LUA)
Bulk Delete Comments from (Self-Hosted) JIRA Issues (Misc)
Add a static entry to the ARP table (BASH)
SSL Cipher Hex codes to Human Readable Names (Misc)
Convert Ascii to Binary (BASH)
Intercepting Outbound DNS Queries (BASH)
Handle Google Verification files within NGinx Configuration (NGinx)
Getting WhatsApp Rich Snippet Previews Working (Misc)

Copyright © 2018 Ben Tasker | Sitemap | Privacy Policy
Available at snippets.bentasker.co.uk and snippets.6zdgh5a5e6zpchdz.onion