Skip to content

Commit 067e961

Browse files
authored
Merge pull request #498 from hotosm/refactor-processing
fix: copying file within s3 bucket
2 parents 131c90f + 6de3c94 commit 067e961

File tree

3 files changed

+50
-43
lines changed

3 files changed

+50
-43
lines changed

src/backend/app/migrations/versions/e23c05f21542_change_task_events_state.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,17 @@ def upgrade():
5555
# Step 2: Add a new column with the new enum type
5656
op.add_column("task_events", sa.Column("new_state", new_state_enum, nullable=True))
5757

58-
# Step 3: Populate the new state column with the transformed data
59-
op.execute(
60-
"""
61-
UPDATE task_events
62-
SET new_state =
63-
CASE
64-
WHEN state = 'IMAGE_PROCESSED' THEN 'IMAGE_PROCESSING_FINISHED'
65-
ELSE state::text
66-
END::state_new
67-
"""
68-
)
58+
# # Step 3: Populate the new state column with the transformed data
59+
# op.execute(
60+
# """
61+
# UPDATE task_events
62+
# SET new_state =
63+
# CASE
64+
# WHEN state = 'IMAGE_PROCESSED' THEN 'IMAGE_PROCESSING_FINISHED'
65+
# ELSE state::text
66+
# END::state_new
67+
# """
68+
# )
6969

7070
# Step 4: Drop the old state column
7171
op.drop_column("task_events", "state")
@@ -110,17 +110,17 @@ def downgrade():
110110
# Step 3: Add the old state column with the old enum type
111111
op.add_column("task_events", sa.Column("state_old", old_state_enum, nullable=True))
112112

113-
# Step 4: Populate the old state column with the transformed data
114-
op.execute(
115-
"""
116-
UPDATE task_events
117-
SET state_old =
118-
CASE
119-
WHEN state = 'IMAGE_PROCESSING_FINISHED' THEN 'IMAGE_PROCESSED'
120-
ELSE state::text
121-
END::state
122-
"""
123-
)
113+
# # Step 4: Populate the old state column with the transformed data
114+
# op.execute(
115+
# """
116+
# UPDATE task_events
117+
# SET state_old =
118+
# CASE
119+
# WHEN state = 'IMAGE_PROCESSING_FINISHED' THEN 'IMAGE_PROCESSED'
120+
# ELSE state::text
121+
# END::state
122+
# """
123+
# )
124124

125125
# Step 5: Drop the new_state column
126126
op.drop_column("task_events", "state")

src/backend/app/projects/image_processing.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -484,37 +484,41 @@ async def process_assets_from_odm(
484484
conn, dtm_project_id, dtm_task_id, "assets_url", s3_path_url
485485
)
486486

487-
# If the project has only one task, copy the s3_ortho path to the project level
488-
# And mark the project processing status as completed, no need to process again
489-
total_tasks = await project_logic.get_all_tasks_for_project(
487+
# If the project has only one task, copy the orthophoto and assets to the project level
488+
# Mark the project processing status as completed to avoid redundant processing
489+
tasks = await project_logic.get_all_tasks_for_project(
490490
dtm_project_id, conn
491491
)
492-
if len(total_tasks) == 1:
493-
s3_ortho_project_path = f"dtm-data/projects/{dtm_project_id}/orthophoto/odm_orthophoto.tif"
492+
493+
if len(tasks) == 1:
494+
project_ortho_path = f"dtm-data/projects/{dtm_project_id}/orthophoto/odm_orthophoto.tif"
494495
log.info(
495-
f"Copying orthophoto to project level: {s3_ortho_project_path}"
496+
f"Copying orthophoto to project level: {project_ortho_path}"
496497
)
497-
copy_file_within_bucket(
498+
499+
ortho_copy_status = copy_file_within_bucket(
498500
settings.S3_BUCKET_NAME,
499501
s3_ortho_path,
500-
s3_ortho_project_path,
502+
project_ortho_path,
501503
)
502504

503-
# copy the assets.zip to the project level
504-
s3_assets_project_path = (
505+
project_assets_path = (
505506
f"dtm-data/projects/{dtm_project_id}/assets.zip"
506507
)
507508
log.info(
508-
f"Copying assets to project level: {s3_assets_project_path}"
509-
)
510-
copy_file_within_bucket(
511-
settings.S3_BUCKET_NAME, s3_path, s3_assets_project_path
509+
f"Copying assets to project level: {project_assets_path}"
512510
)
513-
# update the image processing status
514-
await project_logic.update_processing_status(
515-
conn, dtm_project_id, ImageProcessingStatus.SUCCESS
511+
512+
assets_copy_status = copy_file_within_bucket(
513+
settings.S3_BUCKET_NAME, s3_path, project_assets_path
516514
)
517515

516+
# Update project processing status if both copies were successful
517+
if ortho_copy_status and assets_copy_status:
518+
await project_logic.update_processing_status(
519+
conn, dtm_project_id, ImageProcessingStatus.SUCCESS
520+
)
521+
518522
status = ImageProcessingStatus.SUCCESS
519523
if not dtm_task_id:
520524
# Update the image processing status

src/backend/app/s3.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from datetime import timedelta
77
from urllib.parse import urljoin
88
from minio.error import S3Error
9+
from minio.commonconfig import CopySource
910

1011

1112
def s3_client():
@@ -302,14 +303,16 @@ def copy_file_within_bucket(
302303
log.warning(f"Source file not found: {source_path} in bucket {bucket_name}")
303304
return False
304305

306+
# Create CopySource object
307+
copy_source = CopySource(bucket_name, source_path)
308+
305309
# Copy the object within the same bucket
306-
client.copy_object(
307-
bucket_name, destination_path, f"{bucket_name}/{source_path}"
308-
)
310+
result = client.copy_object(bucket_name, destination_path, copy_source)
309311

310312
log.debug(
311313
f"Successfully copied object within {bucket_name} "
312-
f"from {source_path} to {destination_path}"
314+
f"from {source_path} to {destination_path}. "
315+
f"Object name: {result.object_name}, Version ID: {result.version_id}"
313316
)
314317
return True
315318

0 commit comments

Comments
 (0)