diff --git a/anytask/common/s3_migrate.py b/anytask/common/s3_migrate.py index fd3c4eae..52c4df40 100644 --- a/anytask/common/s3_migrate.py +++ b/anytask/common/s3_migrate.py @@ -5,6 +5,12 @@ from storage import S3OverlayStorage +class S3NotFound(Exception): + def __init__(self, name, *args, **kwargs): + self.name = name + super().__init__(*args, **kwargs) + + def upload_to_s3(file_field, dest_storage, dry_run=True): """Saves content of file_field to S3, adjusting path in database @@ -26,7 +32,7 @@ def upload_to_s3(file_field, dest_storage, dry_run=True): raise ValueError("Path with S3 magic: {}".format(old_path)) new_path = dest_storage.append_s3_prefix(old_path) if dest_storage.exists(new_path): - raise KeyError(new_path) + raise S3NotFound(new_path) if not dry_run: with file_field.storage.open(file_field.name, 'rb') as content: dest_storage.save(new_path, content) @@ -206,8 +212,8 @@ def s3_upload(self, field): result = None # don't update DB model for newly uploaded files else: result = new_path - except KeyError as e: - new_path = e.message + except S3NotFound as e: + new_path = e.name if (new_path in self.newly_uploaded) and self.rewrite_url_only_existing: result = None else: @@ -216,8 +222,7 @@ def s3_upload(self, field): except Exception as e: self.stdout.write(self.ERR_UNHANDLED_EXCEPTION.format(field.name, e)) result = None - finally: - return result + return result def delete_local(self, old_path): self.dest_storage.delete(old_path) diff --git a/anytask/storage.py b/anytask/storage.py index 0d9841e7..1385581d 100644 --- a/anytask/storage.py +++ b/anytask/storage.py @@ -58,9 +58,6 @@ def _dispatch(self, name, method_name, *args, **kwargs): @classmethod def is_s3_stored(cls, name): - ret = name.lstrip('/').startswith(cls.S3_STORED_MAGIC + '/') - name_stripped = name.lstrip('/') - print(f"!!!!!!!!!! NAMAE:{name} {name_stripped} {ret}") return name.lstrip('/').startswith(cls.S3_STORED_MAGIC + '/') @classmethod