This project demonstrates how to use Cloudflare R2 event notifications to process PDF files automatically when they are uploaded to an R2 bucket. It uses Workers AI to summarize the PDF content and stores the summary as a text file in the same bucket.
- Upload PDF files to Cloudflare R2 bucket
- Automatically process uploaded PDFs using R2 event notifications
- Extract text from PDF files
- Summarize the extracted text using Workers AI
- Store the summary as a text file in the R2 bucket
- Cloudflare account with Workers and R2 enabled
- Node.js and npm installed on your local machine
- Wrangler CLI installed (
npm install -g wrangler)
- Clone this repository:
git clone https://github.com/your-username/pdf-summarizer.git
cd pdf-summarizer- Install dependencies:
npm install- Create a Queue
npx wrangler queues create pdf-summarizer-
Configure your wrangler.toml file with your R2 bucket and Queue consumer.
-
Deploy the Worker:
npm run deploy- Set up R2 event notifications for your bucket to trigger the worker's queue handler.
npx wrangler r2 bucket notification create <R2_BUCKET_NAME> --event-type object-create --queue pdf-summarizer --suffix "pdf"Navigate to the deployed Worker's URL and upload a PDF file. The Worker will process the PDF and store the summary as a text file in the R2 bucket.
Send a POST request to /api/upload with the PDF file in the request body:
curl -X POST -H "Content-Type: application/pdf" --data-binary "@/path/to/your/pdf/file.pdf" <WORKER_URL>/api/uploadTo list all files in the R2 bucket, send a GET request to /api/list:
curl <WORKER_URL>/api/listWhen a PDF is uploaded to the R2 bucket, it triggers an event notification.
The consumer Worker processes the event
- retrieving the PDF from the bucket.
- extract the PDF content using the unpdf library.
- Workers AI summarizes the extracted text.
- The summary is stored as a text file in the same R2 bucket with the naming convention
{original-filename}-summary.txt.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License.