Skip to content

Commit

Permalink
🐛 Fix S3 upload not working
Browse files Browse the repository at this point in the history
  • Loading branch information
mawoka-myblock committed Jun 16, 2024
1 parent 7e5acf8 commit a686aca
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 82 deletions.
160 changes: 80 additions & 80 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion classquiz/routers/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ async def upload_file(file: UploadFile = File(), user: User = Depends(get_curren
if user.storage_used > settings.free_storage_limit:
raise HTTPException(status_code=409, detail="Storage limit reached")
file_id = uuid4()
file_size = len(await file.read())
file_size = 0
file_obj = StorageItem(
id=file_id,
uploaded_at=datetime.now(),
Expand Down
10 changes: 9 additions & 1 deletion classquiz/storage/s3_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,15 @@ def _generate_aws_signature_v4(self, method: str, path: str, expiry: int = None)
# skipcq: PYL-W0613
async def upload(self, file: BinaryIO, file_name: str, size: int | None, mime_type: str | None = None) -> None:
headers, url = self._generate_aws_signature_v4(method="PUT", path=f"/{file_name}")
headers["Content-Length"] = str(size)
file_size = 0
while True:
chunk = file.read(1024)
file_size += len(chunk)
if not chunk:
file.seek(0)
break
headers["Content-Length"] = str(file_size)

async with ClientSession() as session, session.put(url, headers=headers, data=file) as resp:
if resp.status == 200:
return None
Expand Down

0 comments on commit a686aca

Please sign in to comment.