aadeshkulkarni / figuringout

Blogging + Social Media + AI | Opensource | Javascript | ReactJS | HonoJS | Prisma
https://figuringout.life
MIT License
74 stars 78 forks source link

25$ Bounty | Local Development: Cloudflare Worker (HonoJS) Accessing R2 Files #125

Open aadeshkulkarni opened 1 month ago

aadeshkulkarni commented 1 month ago

Scenario:

Our backend is a cloudflare worker. It's a serverless architecture. Cloudflare also provides R2, an object store, like AWS S3, to manage file uploads.

When we upload a file from production instance, the file gets uploaded to the R2 bucket successfully, and a key is generated. To access this uploaded file, we can setup subdomains that facilitate a base-url that can be used for the file downloads.

file-path = subDomainUrl + key

wrangler.toml file > R2_SUBDOMAIN_URL


Here is the catch:

When we upload a file from dev mode (locally), the file gets uploaded to the local cache (within .wrangler/state/../R2/blob) instead of the R2 bucket. Now, To access this uploaded file, we need a url. Without the url, we cannot generate a file path.
And without the filepath, we cannot access the file on the frontend.


I'd like to find a solution that allows me (developer) to test my Worker's upload & download file functionality with Cloudflare R2 during local development.

For example, from local dev instance, A user should be able to successfully upload their profile picture and should be able to access the profile picture as a url.

Points to consider:

aliraza556 commented 1 month ago

Hi @aadeshkulkarni, Could you please assign me?

aliraza556 commented 1 month ago

@aadeshkulkarni, I have already set up the project. Could you please assign me the next task?

darshanmodi13 commented 1 month ago

@aadeshkulkarni Can you please describe in detail the reproduction steps again?

aadeshkulkarni commented 1 month ago

@aliraza556 - Assigning it to you. As this is time-critical issue with a bounty, You will have 48 hours to come up with a solution. If you're unable to solve it by then, you will be unassigned automatically, so that someone else can pick it up.

aadeshkulkarni commented 1 month ago

@aadeshkulkarni Can you please describe in detail the reproduction steps again?

Step 1: Run the backend & frontend server locally. Step 2: Ensure that you comment Production link for BACKEND_URL and uncomment the local link for BACKEND_URL in frontend/src/config.ts file Step 3: Spin up the frontend & backend using npm run dev in both frontend and backend folders. Step 4: Open the app on localhost:5173 and Signup, signin. Step 5: Go to Profile (from top right) > About > Update profile picture > Save

Screenshot 2024-06-03 at 11 41 15 PM

After saving, IDEALLY, you should be able to see the updated profile picture everywhere in the app. But, that won't happen on Dev mode.

The challenge is to make this Step 5 work locally using cloudflare R2.

aliraza556 commented 1 month ago

@aliraza556 - Assigning it to you. As this is time-critical issue with a bounty, You will have 48 hours to come up with a solution. If you're unable to solve it by then, you will be unassigned automatically, so that someone else can pick it up.

@aadeshkulkarni, Ok

aliraza556 commented 1 month ago

Hi @aadeshkulkarni, I tried to set up the project locally, but I am facing an issue connecting to the database. Could you please help me with this?

.env file:

image

wrangler.toml file:

image

config.ts file:

image

When I sign up, I get this error:

image

aadeshkulkarni commented 1 month ago

@aliraza556 - You will need to set the PRISMA_DATABASE_URL in .env with the same value as DATABASE_URL which is in wrangler.toml.

You will also need to run the db migration script. Check package.json within backend for the migrate command.

aliraza556 commented 1 month ago

Hi @aadeshkulkarni, Please review it, I'm going to make a pull request (PR).

image

Ashutoshpadhi629 commented 1 month ago

hello @aadeshkulkarni , can you assign me to this issue?

aadeshkulkarni commented 6 days ago

Sure, here you go

Ashutoshpadhi629 commented 6 days ago

@aadeshkulkarni

We need to run wrangler dev --remote for dev mode

Screenshot 2024-07-04 at 2 23 06 AM

docs link : https://developers.cloudflare.com/r2/api/workers/workers-api-usage/#4-access-your-r2-bucket-from-your-worker

Screenshot 2024-07-04 at 2 34 58 AM

also we need a different Bucket for local development

Screenshot 2024-07-04 at 2 23 16 AM

docs link : https://developers.cloudflare.com/workers/testing/local-development/#develop-using-remote-resources-and-bindings

Updated wrangler.toml

Screenshot 2024-07-04 at 2 35 10 AM
Ashutoshpadhi629 commented 2 days ago

should i continue with this approach @aadeshkulkarni ?

aadeshkulkarni commented 1 day ago

Sure, please go ahead