cdn questions for media

yavuzakyazici
HOBBY

a year ago

Project ID : dba6cd6e-5ea1-494c-b77f-81377e24fbde

Hello everyone,

I have been a hobby plan subscriber before, then I tried my already purchased server on A2 hosting with my project. It seemed very limited compared to what I had here. Now I am back.

I have a project with FastAPI backend and React Native Mobile Expo Go on the front end.
It works fine, expo consumes the API I have here fine.
However, I have many static files including videos ( 115 of them so far ) 5-10mb range and lesson posters ( 150KB range ). I would like to use CDN if possible.
I had setup a media volume here, thanks to Brody for that BTW, which works fine.
I have a domain that I could use to point this server and use free cloudflare CDN to test.
I can pay a little if necessary.
I am thinking as the app grows, I might end up having 2-300 videos all around 5 to 20mb range.
This is for a jazz education software.
What is my best option?

I am thinking my options are…

  1. Leave API here and have it point to other servers with CDN for videos
    I don't like this option very much since it requires a not so small re-write for API.

  2. Use the volume I have here and get a CDN for it somehow ( may be using my custom domain to point here ? )

  3. Use a bigger service like Azure or AWS for static serving files?
    Which I do not like since they can get expensive petty quick.

  4. Do nothing and leave everything as is and hope performance will be OK once I go live with my app.
    I am expecting/hoping 50 to 100 users in the next few months and may be 1000 users in a year?
    If that happens I will have to upgrade my account here from hobby to pro of course but I don't how I could prepare to grow ( if it happens.. )

I do not have any performance issues now BTW.

Any suggestions?

0 Replies

a year ago

for option 1, how not small of a rewrite would it be? you are using a storage engine that ties into postgres and uses local storage (a volume when on railway)
does this storage engine provide an S3 layer? that way you could use cloudflare R2 with a custom domain and then you'd have a no egress bucket and files would be cached


yavuzakyazici
HOBBY

a year ago

I guess I could do that. Now that I think about it, mysql has a record of the file name, to get started, I could modify the the db records adding prefix or suffixes, and have the records point to other server with cloudflare. This is rather a minimal rewrite. May be not even a rewrite. This would save me a lot of network bandwidth as well I think. Let me think about this a bit but thanks Brody.


yavuzakyazici
HOBBY

a year ago

I dont know if you remember but you helped me get my media drive up and running for videos and streaming stuff. You even did an example with fastapi


a year ago

yes I do!


a year ago

I forget the fine details of it though, so if the storage engine supports S3 + MySQL as a backend, it should be relatively simple, store the files in a cloudflare R2 bucket, and using a custom domain to get the files cached


yavuzakyazici
HOBBY

a year ago

Yes, that works. I'll check that out. I event have the files on an other server too with a custom domain. So, I could use that server for videos. I just need to integrate the admin panel to save the video to the other server. If integrating admin does not work out, I know admin that I use supports S3 so, I'll what you suggested now. Thanks.
When you initially helped me, my files werenot surviving between deploys since I used the wrong mount point. Now I use /app/media.. Also static files did not have both slashes etc.. You made an example for it. Thanks for that


a year ago

always happy to help!


yavuzakyazici
HOBBY

a year ago

Thanks Brody πŸ™‚


a year ago

no problem! πŸ™‚


a year ago

let me know what you end up doing!


yavuzakyazici
HOBBY

a year ago

I'll post it here when I make a move. I am reading the R2 option looks promising. I use SQL Alchemy Admin with FastAPI which has S3 Storage option, so it should be compatible with what you suggested ( Cloudflare R2 )



a year ago

perfect, it does indeed have an S3 storage backend!


yavuzakyazici
HOBBY

a year ago

Unfortunately, I could not set this up after spending a full day. R2 docs are not that good. I could not find any examples doing this with fastapi_storage. I found one example for regular S3. I will check that out and if that does not work, then I have to find another solution for CDN.


a year ago

cloudflare R2 is AWS S3 compatible


a year ago

so yes you'd want to use the S3 backend


yavuzakyazici
HOBBY

a year ago

I managed to get file info and download a file from the bucket. I was working on it with an example FastAPI project. If I can manage everything, including PresignedUrl etc… , I will post the github example here. Let's see how it's gonna workout.


a year ago

awesome, I look forward to that!


yavuzakyazici
HOBBY

a year ago

Here are the examples…


yavuzakyazici
HOBBY

a year ago

  1. This one shows FastAPI, SqlAdmin, CloudFlare R2 integration. boto3 wrapped with another class called S3Storage for easier integration to admin.



yavuzakyazici
HOBBY

a year ago

Here is the second example with regular fastapi upload/download file without any admin integration. This one is done with boto3 library..



a year ago

you work fast!


yavuzakyazici
HOBBY

a year ago

I'm trying πŸ™‚ Now, I will look into generating PresignedUrl for each video with 20 seconds expiration. This way, I can keep the bucket access private and cache CDN content. When I manage to work it, I'll add that to examples.


yavuzakyazici
HOBBY

a year ago

So I updated boto3 version with PresignedUrl method.
If you don't want your bucket contents public, you might just want your client app display it or whatever the reason you might have. You can just return a PresignedUrl expires in seconds. Once it is consumed by your client app thru API, it expires πŸ™‚ Nice isn't it ?



a year ago

awesome work!


a year ago

looks like this is turning out to be exactly what you want?


yavuzakyazici
HOBBY

a year ago

Yes it did. Thanks to your suggestion πŸ™‚ I was not expecting it to be this convenient. One thing I could not do was adding CNAME record to my custom domain at A2 hosting. I have to get suport. Adding custom domain to CloudFlare was very very easy though.


a year ago

yeah A2 likely wouldnt support root level cnames, best to use cloudflare for your nameservers


yavuzakyazici
HOBBY

a year ago

I'll ask and see what they say πŸ™‚


a year ago

please note you can not use A or ALIAS types despite what they may say, that will break your site


yavuzakyazici
HOBBY

a year ago

OK. Good to know thanks. I may not even need to get a custom domain for api. CDN was done and helps caching. Having a custom domain for the api not a must I think


a year ago

yeah thats up to you, but switching to cloudflare's nameservers is incredibly easy, they even copy over your dns settings for you!


yavuzakyazici
HOBBY

a year ago

Yes, I noticed it was very easy and It took like a minute to update.


a year ago

do you have different root domains for the cdn and what you would use for the api?


yavuzakyazici
HOBBY

a year ago

yes.


yavuzakyazici
HOBBY

a year ago

they were bot subdomains. CloudFlare worked. But I did it from cloudflare settings. I did not need to touch A2 Panel.


yavuzakyazici
HOBBY

a year ago

To change the api railway instructs to add a CNAME but A2 does not let me the way it is suggested. So, did not work. But I'll ask support ands see what they say


a year ago

you tried in the style of api.domain.com? any dns provider should support cnames on a subdomain


yavuzakyazici
HOBBY

a year ago

api.domain.com? yes exactly..


a year ago

can you try again and show me what a2 says?


yavuzakyazici
HOBBY

a year ago

oki.. I'll try and take screenshots as well


yavuzakyazici
HOBBY

a year ago

It says dns zone is invalid


a year ago

have you tried just "api" for the name?


yavuzakyazici
HOBBY

a year ago

I did. It does not let me. If I reverse the values. It accepts it but It adds stuff


yavuzakyazici
HOBBY

a year ago

like this


a year ago

have you tried "api" as the name, and then the record "domain.up.railway.app." (with a period at the end)


yavuzakyazici
HOBBY

a year ago

let me try now


yavuzakyazici
HOBBY

a year ago

so where do I enter just api

1241400029864792000


yavuzakyazici
HOBBY

a year ago

Suggested domain at railway is yynudahm.up.railway.app


a year ago

please see my message


yavuzakyazici
HOBBY

a year ago

replace domain with my domain right?


yavuzakyazici
HOBBY

a year ago

It does not accept it


a year ago

correct, please show me what you tried?


yavuzakyazici
HOBBY

a year ago

I tried this


a year ago

please try what I suggested


yavuzakyazici
HOBBY

a year ago

when I type api, it completes it to what you saw


a year ago

may I ask what you removed the image?


yavuzakyazici
HOBBY

a year ago

Too many images.


yavuzakyazici
HOBBY

a year ago

I can put it back in


a year ago

I think discord can handle a few more images lol


yavuzakyazici
HOBBY

a year ago

here

1241402253190103000
1241402253550817500


a year ago

A2 too complicated, move nameservers to cloudflare, there's no reason to stay with A2 anyway right?


yavuzakyazici
HOBBY

a year ago

No…


yavuzakyazici
HOBBY

a year ago

I'll look into it. Let me talk to support first.


a year ago

may I ask why you are inclined to stay with A2?


yavuzakyazici
HOBBY

a year ago

I'm not inclined.


yavuzakyazici
HOBBY

a year ago

I paid the services up front for 2 yrs


a year ago

ah I see


yavuzakyazici
HOBBY

a year ago

It was before I had anything here


yavuzakyazici
HOBBY

a year ago

long before πŸ™‚


a year ago

gotcha


yavuzakyazici
HOBBY

a year ago

Let me see what happens. I am happy the way things are if it does not work out. I might move dns to cloudflare if I can


a year ago

sounds good


yavuzakyazici
HOBBY

a year ago

It turns out, DNS was hosted at CloudFlare. I managed to change the CNAME there but now I get too many redirects error to reach the subdomain


a year ago


yavuzakyazici
HOBBY

a year ago

Thanks. Fixed it right away! πŸ™‚ Works now.


a year ago

no problem!