CF Worker For Accessing Amazon S3 Images

We have a bucket setup on Amazon S3 for images. I’m following the guide at https://support.cloudflare.com/hc/en-us/articles/360013791312-Fetching-object-storage-assets-through-the-Cloudflare-CDN-using-a-Cloudflare-Worker and am stuck.

We’ve setup the Cname and that’s working just fine. The issue is with the worker configuration.

Step 2 of the doc above references a recipe. But there’s no script to copy on that page (https://developers.cloudflare.com/workers/recipes/static-site/). I found a worker from another thread here and tried that but am getting the error:

Uncaught SyntaxError: Invalid or unexpected token at line 5

Here’s the code I am trying:

async function handleRequest(request) {
const parsedUrl = new URL(request.url)
let path = parsedUrl.pathname

let lastSegment = path.substring(path.lastIndexOf(’/’))
if (lastSegment.indexOf(’.’) === -1) {
path += ‘/index.html’
}

return fetch(“https://assets.ourdomain.com” + path)
}

addEventListener(‘fetch’, event => {
event.respondWith(handleRequest(event.request))
})

It’s hard given it’s not set as code, but those apostrophes are the wrong kind of apostrophe. Substitute them with the correct one.

Basically you have the top one, set it as the lower one in here:

Yep, that was it. Thanks.

1 Like