Skip to content

Commit

Permalink
Merge pull request #90 from hotosm/image-upload
Browse files Browse the repository at this point in the history
Generate Pre-Signed URL for Drone Imagery Upload
  • Loading branch information
spwoodcock authored Jul 24, 2024
2 parents c9c9769 + c44ce37 commit 93d4ae2
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_and_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ jobs:
- name: Deploy to VM
run: |
bash contrib/pg-upgrade/upgrade-db.sh
docker compose --file docker-compose.vm.yml --env-file .env up \
--detach --remove-orphans --force-recreate --pull=always
env:
Expand Down
Empty file modified contrib/pg-upgrade/upgrade-db.sh
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion docs/images/dev_roadmap_badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/images/docs_badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/images/timeline_badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/images/user_roadmap_badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 16 additions & 9 deletions src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ async def preview_split_by_square(


@router.post("/generate-presigned-url/", tags=["Image Upload"])
async def generate_presigned_url(data: project_schemas.PresignedUrlRequest):
async def generate_presigned_url(
data: project_schemas.PresignedUrlRequest, user: AuthUser = Depends(login_required)
):
"""
Generate a pre-signed URL for uploading an image to S3 Bucket.
Expand All @@ -183,14 +185,19 @@ async def generate_presigned_url(data: project_schemas.PresignedUrlRequest):
try:
# Generate a pre-signed URL for an object
client = s3_client()

url = client.presigned_put_object(
settings.S3_BUCKET_NAME,
f"publicuploads/{data.image_name}",
expires=timedelta(hours=data.expiry),
)

return url
urls = []
for image in data.image_name:
image_path = f"publicuploads/{data.project_id}/{data.task_id}/{image}"

url = client.get_presigned_url(
"PUT",
settings.S3_BUCKET_NAME,
image_path,
expires=timedelta(hours=data.expiry),
)
urls.append({"image_name": image, "url": url})

return urls
except Exception as e:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
Expand Down
8 changes: 5 additions & 3 deletions src/backend/app/projects/project_schemas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import uuid
from pydantic import BaseModel, computed_field, Field, validator
from typing import Any, Optional, Union
from typing import Any, Optional, Union, List
from geojson_pydantic import Feature, FeatureCollection, Polygon
from app.models.enums import ProjectVisibility, State
from shapely import wkb
Expand Down Expand Up @@ -126,5 +126,7 @@ def outline_geojson(self) -> Optional[Feature]:


class PresignedUrlRequest(BaseModel):
image_name: str
expiry: int # Expiry time in seconds
project_id: uuid.UUID
task_id: uuid.UUID
image_name: List[str]
expiry: int # Expiry time in hours

0 comments on commit 93d4ae2

Please sign in to comment.