Skip to content

Commit

Permalink
Feature : Adds Head endpoint for get files
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitijrajsharma committed Jan 16, 2024
1 parent de5b730 commit fe52f5b
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions API/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
import humanize
from boto3.session import Session
from botocore.exceptions import NoCredentialsError
from fastapi import APIRouter, HTTPException, Path, Query, Request
from fastapi import APIRouter, Header, HTTPException, Path, Query, Request
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse, RedirectResponse, StreamingResponse
from fastapi.responses import (
JSONResponse,
RedirectResponse,
Response,
StreamingResponse,
)
from fastapi_versioning import version

from src.config import AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, BUCKET_NAME
Expand All @@ -16,7 +21,6 @@

router = APIRouter(prefix="/s3", tags=["S3"])

BUCKET_NAME = "production-raw-data-api"
AWS_REGION = "us-east-1"
session = Session()
s3 = session.client(
Expand Down Expand Up @@ -103,6 +107,33 @@ async def read_meta_json(bucket_name, file_path):
)


@router.head("/get/{file_path:path}")
@limiter.limit(f"{RATE_LIMIT_PER_MIN}/minute")
@version(1)
async def head_s3_file(
request: Request,
file_path: str = Path(..., description="The path to the file or folder in S3"),
):
bucket_name = BUCKET_NAME
encoded_file_path = quote(file_path.strip("/"))
try:
response = s3.head_object(Bucket=bucket_name, Key=encoded_file_path)
return Response(
status_code=200,
headers={
"Last-Modified": response["LastModified"].strftime(
"%a, %d %b %Y %H:%M:%S GMT"
),
"Content-Length": str(response["ContentLength"]),
},
)
except Exception as e:
if e.response["Error"]["Code"] == "404":
return Response(status_code=404)
else:
raise HTTPException(status_code=500, detail=f"AWS Error: {str(e)}")


@router.get("/get/{file_path:path}")
@limiter.limit(f"{RATE_LIMIT_PER_MIN}/minute")
@version(1)
Expand Down

0 comments on commit fe52f5b

Please sign in to comment.