DEV Community

Cover image for S3cret Agent: Build an AI That Generates AWS Presigned URLs on Command
Chandrani Mukherjee
Chandrani Mukherjee

Posted on

S3cret Agent: Build an AI That Generates AWS Presigned URLs on Command

This project implements an AI-powered LangChain agent that dynamically generates AWS S3 Presigned URLs (GET or PUT) based on natural language input. The agent is wrapped in a FastAPI server and supports integration into secure backend workflows.

๐Ÿง  What It Does

  • Accepts input like: Generate download link bucket=my-bucket key=path/to/file.txt
  • Uses LangChain tools to interpret commands
  • Generates a presigned URL via boto3
  • Responds with a secure, time-limited URL (GET/PUT)

๐Ÿงฑ Tech Stack

๐Ÿ“ Folder Structure

langchain_s3_agent/
โ”œโ”€โ”€ main.py # FastAPI app
โ”œโ”€โ”€ agent.py # LangChain agent logic
โ”œโ”€โ”€ aws_utils.py # Presigned URL generator
โ”œโ”€โ”€ .env # AWS credentials (not committed)
โ”œโ”€โ”€ requirements.txt
โ””โ”€โ”€ Dockerfile (optional)

๐Ÿš€ Quick Start

1๏ธโƒฃ Clone and Install

git clone https://github.com/your-repo/langchain-s3-agent.git cd langchain-s3-agent pip install -r requirements.txt 
Enter fullscreen mode Exit fullscreen mode

2๏ธโƒฃ Add AWS Credentials to .env
env
AWS_ACCESS_KEY_ID=your_key
AWS_SECRET_ACCESS_KEY=your_secret
AWS_REGION=us-east-1
3๏ธโƒฃ Run the API

uvicorn main:app --reload 
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ก How to Use
POST /query
Request Body:

 { "query": "Generate download link bucket=my-bucket key=path/to/file.txt" } 
Enter fullscreen mode Exit fullscreen mode

Response:

 { "response": "GET URL for `path/to/file.txt`: https://s3.amazonaws.com/..." } 
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Supported Prompts
Action Prompt Format Example
Download Generate download link bucket=my-bucket key=folder/file.txt
Upload Generate upload link bucket=my-bucket key=uploads/newfile.txt

๐Ÿงช Example CURL Test

 curl -X POST http://localhost:8000/query \ -H "Content-Type: application/json" \ -d '{"query": "Generate upload link bucket=my-bucket key=upload/test.txt"}' 
Enter fullscreen mode Exit fullscreen mode

๐Ÿณ Optional: Docker Support
Dockerfile
dockerfile

FROM python:3.11 WORKDIR /app COPY . . RUN pip install -r requirements.txt CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"] 
Enter fullscreen mode Exit fullscreen mode

Build & Run

docker build -t s3-agent . docker run -p 8000:8000 --env-file .env s3-agent 
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” IAM Policy Required
Your AWS IAM user or role must have:

 { "Effect": "Allow", "Action": ["s3:GetObject", "s3:PutObject"], "Resource": "arn:aws:s3:::your-bucket-name/*" } 
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“Œ Roadmap Ideas
Add expiration input (expires_in=600)

Enable presigned POST for HTML form uploads

Add token auth to protect the API

Web UI with React or Streamlit

๐Ÿ“˜ Resources
AWS S3 Presigned URLs

Boto3 Docs

LangChain Docs

FastAPI Docs

Top comments (1)

Collapse
 
aiden_benjamin_52c3f6771f profile image
Aiden Benjamin

Absolutely brilliant ideaโ€”turning presigned URL generation into a natural language task is next-level productivity!