R
Railway•7mo ago
yavuz

cdn questions for media

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?
61 Replies
Percy
Percy•7mo ago
Project ID: dba6cd6e-5ea1-494c-b77f-81377e24fbde
Brody
Brody•7mo 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
yavuz
yavuzOP•7mo 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. 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
Brody•7mo ago
yes I do! 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 https://developers.cloudflare.com/r2/buckets/public-buckets/#caching
yavuz
yavuzOP•7mo 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
Brody•7mo ago
always happy to help!
yavuz
yavuzOP•7mo ago
Thanks Brody 🙂
Brody
Brody•7mo ago
no problem! 🙂 let me know what you end up doing!
yavuz
yavuzOP•7mo 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
Brody•7mo ago
perfect, it does indeed have an S3 storage backend!
yavuz
yavuzOP•7mo 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
Brody•7mo ago
cloudflare R2 is AWS S3 compatible so yes you'd want to use the S3 backend
yavuz
yavuzOP•7mo 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
Brody•7mo ago
awesome, I look forward to that!
yavuz
yavuzOP•7mo ago
Here are the examples... 1. This one shows FastAPI, SqlAdmin, CloudFlare R2 integration. boto3 wrapped with another class called S3Storage for easier integration to admin.
yavuz
yavuzOP•7mo ago
GitHub
GitHub - yavuzakyazici/s3_example
Contribute to yavuzakyazici/s3_example development by creating an account on GitHub.
yavuz
yavuzOP•7mo ago
Here is the second example with regular fastapi upload/download file without any admin integration. This one is done with boto3 library..
yavuz
yavuzOP•7mo ago
GitHub
GitHub - yavuzakyazici/cf_s3_fastapi
Contribute to yavuzakyazici/cf_s3_fastapi development by creating an account on GitHub.
Brody
Brody•7mo ago
you work fast!
yavuz
yavuzOP•7mo 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. 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 ?
yavuz
yavuzOP•7mo ago
GitHub
GitHub - yavuzakyazici/cf_s3_fastapi
Contribute to yavuzakyazici/cf_s3_fastapi development by creating an account on GitHub.
Brody
Brody•7mo ago
awesome work! looks like this is turning out to be exactly what you want?
yavuz
yavuzOP•7mo 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
Brody•7mo ago
yeah A2 likely wouldnt support root level cnames, best to use cloudflare for your nameservers
yavuz
yavuzOP•7mo ago
I'll ask and see what they say 🙂
Brody
Brody•7mo ago
please note you can not use A or ALIAS types despite what they may say, that will break your site
yavuz
yavuzOP•7mo 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
Brody•7mo ago
yeah thats up to you, but switching to cloudflare's nameservers is incredibly easy, they even copy over your dns settings for you!
yavuz
yavuzOP•7mo ago
Yes, I noticed it was very easy and It took like a minute to update.
Brody
Brody•7mo ago
do you have different root domains for the cdn and what you would use for the api?
yavuz
yavuzOP•7mo ago
yes. they were bot subdomains. CloudFlare worked. But I did it from cloudflare settings. I did not need to touch A2 Panel. 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
Brody•7mo ago
you tried in the style of api.domain.com? any dns provider should support cnames on a subdomain
yavuz
yavuzOP•7mo ago
api.domain.com? yes exactly..
Brody
Brody•7mo ago
can you try again and show me what a2 says?
yavuz
yavuzOP•7mo ago
oki.. I'll try and take screenshots as well It says dns zone is invalid
Brody
Brody•7mo ago
have you tried just "api" for the name?
yavuz
yavuzOP•7mo ago
I did. It does not let me. If I reverse the values. It accepts it but It adds stuff like this
Brody
Brody•7mo ago
have you tried "api" as the name, and then the record "domain.up.railway.app." (with a period at the end)
yavuz
yavuzOP•7mo ago
let me try now Suggested domain at railway is yynudahm.up.railway.app
Brody
Brody•7mo ago
please see my message
yavuz
yavuzOP•7mo ago
replace domain with my domain right? It does not accept it
Brody
Brody•7mo ago
correct, please show me what you tried?
yavuz
yavuzOP•7mo ago
I tried this
Brody
Brody•7mo ago
please try what I suggested
yavuz
yavuzOP•7mo ago
when I type api, it completes it to what you saw
Brody
Brody•7mo ago
may I ask what you removed the image?
yavuz
yavuzOP•7mo ago
Too many images. I can put it back in
Brody
Brody•7mo ago
I think discord can handle a few more images lol A2 too complicated, move nameservers to cloudflare, there's no reason to stay with A2 anyway right?
yavuz
yavuzOP•7mo ago
No... I'll look into it. Let me talk to support first.
Brody
Brody•7mo ago
may I ask why you are inclined to stay with A2?
yavuz
yavuzOP•7mo ago
I'm not inclined. I paid the services up front for 2 yrs
Brody
Brody•7mo ago
ah I see
yavuz
yavuzOP•7mo ago
It was before I had anything here long before 🙂
Brody
Brody•7mo ago
gotcha
yavuz
yavuzOP•7mo 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
Brody•7mo ago
sounds good
yavuz
yavuzOP•7mo 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
yavuz
yavuzOP•7mo ago
Thanks. Fixed it right away! 🙂 Works now.
Brody
Brody•7mo ago
no problem!
Want results from more Discord servers?
Add your server