Skip to content

Commit

Permalink
Fix S3 uploading
Browse files Browse the repository at this point in the history
  • Loading branch information
znick committed Nov 5, 2023
1 parent 8a9ff4b commit 40d28ba
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
15 changes: 10 additions & 5 deletions anytask/common/s3_migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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)
3 changes: 0 additions & 3 deletions anytask/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 40d28ba

Please sign in to comment.