How to upload files to Cloudflare R2.
How to upload files to Cloudflare R2 from a web app by using the S3-compatible client and the right bucket credentials.

Loading the lesson…
Loading activities…
How to upload files to Cloudflare R2 from a web app by using the S3-compatible client and the right bucket credentials.

Loading the lesson…
Loading activities…
Here's a simple example of uploading a file to R2. You'll usually lean on a library to make this easier.
Install the SDK first with npm install @aws-sdk/client-s3. The upload itself doesn't return a link, so the function builds one from the file's key. That files.your-domain.com address only works for public files after you connect a custom domain to the bucket in the R2 settings (private files get a signed URL instead, like you saw last lesson). You save that URL or the key to your database, then use it anywhere in your app to show the file. That's really it.
The user uploads their photo. You save it to R2.
Save the R2 URL to the user's profile in your database.
When you show the profile, drop the URL into an image tag.
Some apps let users upload documents like PDFs and spreadsheets. Same idea.
Apps like Instagram or Twitter let people upload images and videos with their posts. Here's how that usually shakes out.