Fetching AWS S3 images using Worker

I am trying to serve our image files from our AWS S3 bucket using CF worker. I have found this guide:

https://support.cloudflare.com/hc/en-us/articles/360013791312-Fetching-object-storage-assets-through-the-Cloudflare-CDN-using-a-Cloudflare-Worker

but after following step by step I get this error in the worker editor:

NoSuchKey The specified key does not exist./index.html0C1A594F49644B29Mr2oq8h8Nlquve5YQTqULFVYXGKp6QhPaLTNhE2C6QZ6r+omzBk1OysVbX0V0U8S9ozXpuoNOmM=

1 Like

Show your worker code.

I’m using the below basic script and changing the return fetch url as mentioned in the guide.

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://Cloudflare-example-space.nyc3.digitaloceanspaces.com” + path)
}

addEventListener(β€˜fetch’, event => {
event.respondWith(handleRequest(event.request))
})

Did you replace https://Cloudflare-example-space.nyc3.digitaloceanspaces.com and path with your own URL/path?

he specified key does not exist is AWS 404 error. Check your URL to AWS.

I have one worker that is that exact code, different url.
Are the objects public?

I replaced https://Cloudflare-example-space.nyc3.digitaloceanspaces.com with our own url, do I also have to add anything for (+path)?

I believe our objects are public because I can view the image files from any browser using the image url.

But i may have to recheck the bucket status.

Here is my retried script but i’m getting ReferenceError: S3 not define

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://newbium.s3.amazonaws.com” + s3.amazonaws.com/newbium)
}

addEventListener(β€˜fetch’, event => {
event.respondWith(handleRequest(event.request))
})

TheURL you are trying to fetch is:

https://newbium.s3.amazonaws.com/s3.amazonaws.com/newbium

and is incorrect. When I try to open it in browser:

Whatever resource you want to use first check the URL in browser.

Ok found the correct amazon url but getting the access denied error so need to update our bucket permission to make our objects public.

Thanks a lot for your help.

1 Like