cdn questions for media
yavuzakyazici
HOBBYOP

2 years 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?

80 Replies

brody
EMPLOYEE

2 years 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
HOBBYOP

2 years 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
HOBBYOP

2 years 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


brody
EMPLOYEE

2 years ago

yes I do!


brody
EMPLOYEE

2 years 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
HOBBYOP

2 years 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


brody
EMPLOYEE

2 years ago

always happy to help!


yavuzakyazici
HOBBYOP

2 years ago

Thanks Brody 🙂


brody
EMPLOYEE

2 years ago

no problem! 🙂


brody
EMPLOYEE

2 years ago

let me know what you end up doing!


yavuzakyazici
HOBBYOP

2 years 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 )



brody
EMPLOYEE

2 years ago

perfect, it does indeed have an S3 storage backend!


yavuzakyazici
HOBBYOP

2 years 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.


brody
EMPLOYEE

2 years ago

cloudflare R2 is AWS S3 compatible


brody
EMPLOYEE

2 years ago

so yes you'd want to use the S3 backend


yavuzakyazici
HOBBYOP

2 years 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.


brody
EMPLOYEE

2 years ago

awesome, I look forward to that!


yavuzakyazici
HOBBYOP

2 years ago

Here are the examples…


yavuzakyazici
HOBBYOP

2 years ago

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



yavuzakyazici
HOBBYOP

2 years ago

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


yavuzakyazici
HOBBYOP

2 years ago


brody
EMPLOYEE

2 years ago

you work fast!


yavuzakyazici
HOBBYOP

2 years 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
HOBBYOP

2 years 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 ?


yavuzakyazici
HOBBYOP

2 years ago


brody
EMPLOYEE

2 years ago

awesome work!


brody
EMPLOYEE

2 years ago

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


yavuzakyazici
HOBBYOP

2 years 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.


brody
EMPLOYEE

2 years ago

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


yavuzakyazici
HOBBYOP

2 years ago

I'll ask and see what they say 🙂


brody
EMPLOYEE

2 years ago

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


yavuzakyazici
HOBBYOP

2 years 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


brody
EMPLOYEE

2 years 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
HOBBYOP

2 years ago

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


brody
EMPLOYEE

2 years ago

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


yavuzakyazici
HOBBYOP

2 years ago

yes.


yavuzakyazici
HOBBYOP

2 years ago

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


yavuzakyazici
HOBBYOP

2 years 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


brody
EMPLOYEE

2 years ago

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


yavuzakyazici
HOBBYOP

2 years ago

api.domain.com? yes exactly..


brody
EMPLOYEE

2 years ago

can you try again and show me what a2 says?


yavuzakyazici
HOBBYOP

2 years ago

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


yavuzakyazici
HOBBYOP

2 years ago

It says dns zone is invalid


brody
EMPLOYEE

2 years ago

have you tried just "api" for the name?


yavuzakyazici
HOBBYOP

2 years ago

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


yavuzakyazici
HOBBYOP

2 years ago

like this


brody
EMPLOYEE

2 years ago

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


yavuzakyazici
HOBBYOP

2 years ago

let me try now


yavuzakyazici
HOBBYOP

2 years ago

so where do I enter just api

1241400029864792000


yavuzakyazici
HOBBYOP

2 years ago

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


brody
EMPLOYEE

2 years ago

please see my message


yavuzakyazici
HOBBYOP

2 years ago

replace domain with my domain right?


yavuzakyazici
HOBBYOP

2 years ago

It does not accept it


brody
EMPLOYEE

2 years ago

correct, please show me what you tried?


yavuzakyazici
HOBBYOP

2 years ago

I tried this


brody
EMPLOYEE

2 years ago

please try what I suggested


yavuzakyazici
HOBBYOP

2 years ago

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


brody
EMPLOYEE

2 years ago

may I ask what you removed the image?


yavuzakyazici
HOBBYOP

2 years ago

Too many images.


yavuzakyazici
HOBBYOP

2 years ago

I can put it back in


brody
EMPLOYEE

2 years ago

I think discord can handle a few more images lol


yavuzakyazici
HOBBYOP

2 years ago

here

1241402253190103000
1241402253550817500


brody
EMPLOYEE

2 years ago

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


yavuzakyazici
HOBBYOP

2 years ago

No…


yavuzakyazici
HOBBYOP

2 years ago

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


brody
EMPLOYEE

2 years ago

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


yavuzakyazici
HOBBYOP

2 years ago

I'm not inclined.


yavuzakyazici
HOBBYOP

2 years ago

I paid the services up front for 2 yrs


brody
EMPLOYEE

2 years ago

ah I see


yavuzakyazici
HOBBYOP

2 years ago

It was before I had anything here


yavuzakyazici
HOBBYOP

2 years ago

long before 🙂


brody
EMPLOYEE

2 years ago

gotcha


yavuzakyazici
HOBBYOP

2 years 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


brody
EMPLOYEE

2 years ago

sounds good


yavuzakyazici
HOBBYOP

2 years 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


brody
EMPLOYEE

2 years ago


yavuzakyazici
HOBBYOP

2 years ago

Thanks. Fixed it right away! 🙂 Works now.


brody
EMPLOYEE

2 years ago

no problem!


Loading...