Specific url forwarding with shortening

Hello

I have a cloudfare pages SPA deployed on my own domain on address domain1.com, and there is also a DNS rule of type: CNAME, name: file, content: f1.backblazeb2.com.

it forwards all requests for files from urls like

  • files.domain1.com/bucket1/filename.ext

to an external URL

  • f1.backblazeb2.com/bucket1/filename.ext

this solution has a problem, that anybody could access ANY bucket from my domain name, e.g. following shall not be possible:

  • f1.backblazeb2.com/bucket2/filename.ext accessed from files.domain1.com/bucket2/filename.ext
  • f1.backblazeb2.com/bucket3/filename.ext accessed from files.domain1.com/bucket3/filename.ext
  • etc.

problem 1

I would like to limit the access to a specific bucket, in the example above the only valid request is for the bucket1.

In other words - the url must be of very specific format: files.domain1.com/bucket1/*.*, anything other than this - shall not be valid.

How to achieve it?

problem 2

secondly, I would like to shorten the request URL to format files.domain1.com/*.*, e.g.:

  • files.domain1.com/filename.ext

must be forwarded to:

  • f1.backblazeb2.com/bucket1/filename.ext

If I understand correctly, if I can solve the problem 2. then I don’t really need to solve the problem 1. because the bucket name will be obfuscated from the request URL. However, I would still love to hear the solution to the first problem, if possible (as if the problem 2. would not exist). Just of the curiosity.

How to achieve it?

note

Another point I would like to mention is - if I understood correctly I could use the Page Rules somehow to solve the above problems, but I am also curious if the same task could be accomplished with the cloudflare pages functions (given that the website is deployed by cloudflare pages). That would be very interesting to see… Please let me know if it’s preferable or even possible to go that route…

Thank you!

I was able to resolve the problem by creating the following transformation rule:

name: Add file and bucket name
URI Path: (not starts_with(http.request.uri.path, "/file/mybucket"))
rewrite to dynamic: `concat("/file/mybucket", http.request.uri.path)

now the links are accessible by files.domain.com/filename.ext. I have not yet figured out any shortcomings with this solution…

tried implementing the pages function that would download the file, but had some problems with it:

interface Env {
  KV: KVNamespace
}

export const onRequest: PagesFunction<Env> = async (context) => {
  const request = context.request

  //return await fetch(request.url)
  return await fetch('https://f1.backblazeb2.com/file/mybucket/filename.ext')
}

if I just leave the line return await fetch(request.url) uncommented then the website goes into endless loop…

on the other hand if I fetch the file from direct url like so return await fetch('https://f1.backblazeb2.com/file/mybucket/filename.ext') then the file is downloaded but it’s name is download, missing the actual filename and the extension… And because of previous issue I have no idea how to “bypass file download” and simply return the default page contents.

I have figured that it’s too complex to try to solve the function error so I ended up using the URL transformation approach above…

Please let me know if there are better ways of doing what I am trying to do :slight_smile: Also would be nice to know how to make the functional approach work :slight_smile:

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