Skip to content

Instantly share code, notes, and snippets.

@PatelUtkarsh
Created June 22, 2022 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PatelUtkarsh/0438735990e0b6f373b4deaf7f9be277 to your computer and use it in GitHub Desktop.
Save PatelUtkarsh/0438735990e0b6f373b4deaf7f9be277 to your computer and use it in GitHub Desktop.
Cloudflare worker proxy object storage image
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
const hiddenImageOrigin = "s3 url without trailing shash"
const requestURL = new URL(request.url)
// Append the request path such as "/assets/image1.jpg" to the hiddenImageOrigin.
// You could also process the path to add or remove directories, modify filenames, etc.
const imageURL = hiddenImageOrigin + requestURL.pathname;
// This will fetch image from the given URL, but to the website's visitors this
// will appear as a response to the original request. Visitor’s browser will
// not see this URL.
return fetch(imageURL)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment