Using Cloudflare with AWS S3

I’ve got an existing AWS S3 bucket with over 20TB worth of files in it which I’m looking to serve via Cloudflare.

I do know this is doable where the bucket name matches the domain name being used, however in my case that’s not possible.

I did find this article:
https://support.cloudflare.com/hc/en-us/articles/206652947-Using-Page-Rules-to-Re-Write-Host-Headers
However it seems to be quite old and it says an Enterprise plan is required.

I’m looking at a business plan at this stage, and the only feature I need to confirm is this Host rewrite page rule feature before I take the plunge.
Does anyone know if it’s currently available with Business plans or not? I’m hesitant to take the $200 plunge just to find out it’s not possible.

This was resolved with a short simple Cloudflare Worker which gets around the issue entirely.

Would you mind posting a sanitized version of the script? It sounds like a terrific approach!

No problem. This would be a handy way to ‘rewrite’ urls in many contexts not just S3.
Just replace with your S3 bucket, and likely also change the S3 endpoint as well.
I also alter the cache header as well on the response.

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request));
})

async function handleRequest(request) {
  let path = new URL(request.url).pathname;
  let response = await fetch("https://s3-ap-southeast-2.amazonaws.com/<bucket name>" + path, request);

  response = new Response(response.body, response);
  response.headers.set("Cache-Control", "public, s-max-age=14400, stale-while-revalidate=3600, stale-if-error=86400");

  return response;
}
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.