Fetching AWS S3 images using Worker

#1

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

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

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

#2

Show your worker code.

#3

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))
})

#4

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.

#5

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

#6

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

#7

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.

#8

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))
})

#9

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.

#10

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