|
|
from __future__ import annotations |
|
|
|
|
|
import json |
|
|
import os |
|
|
from typing import Any, Dict |
|
|
|
|
|
from .registry import ToolRegistry |
|
|
|
|
|
|
|
|
def _s3_client(): |
|
|
try: |
|
|
import boto3 |
|
|
except Exception as e: |
|
|
return None, f"boto3 not available: {e}" |
|
|
try: |
|
|
s3 = boto3.client( |
|
|
"s3", |
|
|
endpoint_url=os.getenv("AWS_ENDPOINT_URL"), |
|
|
region_name=os.getenv("AWS_DEFAULT_REGION", "us-east-1"), |
|
|
) |
|
|
return s3, None |
|
|
except Exception as e: |
|
|
return None, str(e) |
|
|
|
|
|
|
|
|
def t_s3_list(args: Dict[str, Any]) -> str: |
|
|
bucket = args.get("bucket") |
|
|
prefix = args.get("prefix", "") |
|
|
s3, err = _s3_client() |
|
|
if err: |
|
|
return json.dumps({"error": err}) |
|
|
if not bucket: |
|
|
return json.dumps({"error": "bucket required"}) |
|
|
try: |
|
|
resp = s3.list_objects_v2(Bucket=bucket, Prefix=prefix) |
|
|
items = [o["Key"] for o in resp.get("Contents", [])] |
|
|
return json.dumps({"items": items}) |
|
|
except Exception as e: |
|
|
return json.dumps({"error": str(e)}) |
|
|
|
|
|
|
|
|
def register_tools(reg: ToolRegistry) -> None: |
|
|
reg.register( |
|
|
name="s3_list", |
|
|
description="List objects from S3/R2 compatible storage.", |
|
|
parameters={"type": "object", "properties": {"bucket": {"type": "string"}, "prefix": {"type": "string"}}, "required": ["bucket"]}, |
|
|
handler=t_s3_list, |
|
|
) |
|
|
|
|
|
|