Skip to content

Commit

Permalink
fix: add correct return data for upload to signed url (#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
silentworks authored Oct 28, 2024
1 parent fca2f00 commit 748067f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
7 changes: 5 additions & 2 deletions storage3/_async/file_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ async def upload_to_signed_url(
token: str,
file: Union[BufferedReader, bytes, FileIO, str, Path],
file_options: Optional[FileOptions] = None,
) -> Response:
) -> UploadResponse:
"""
Upload a file with a token generated from :meth:`.create_signed_url`
Expand Down Expand Up @@ -139,9 +139,12 @@ async def upload_to_signed_url(
headers.pop("content-type"),
)
}
return await self._request(
response = await self._request(
"PUT", final_url, files=_file, headers=headers, data=_data
)
data: UploadData = response.json()

return UploadResponse(path=path, Key=data.get("Key"))

async def create_signed_url(
self, path: str, expires_in: int, options: URLOptions = {}
Expand Down
9 changes: 7 additions & 2 deletions storage3/_sync/file_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def upload_to_signed_url(
token: str,
file: Union[BufferedReader, bytes, FileIO, str, Path],
file_options: Optional[FileOptions] = None,
) -> Response:
) -> UploadResponse:
"""
Upload a file with a token generated from :meth:`.create_signed_url`
Expand Down Expand Up @@ -139,7 +139,12 @@ def upload_to_signed_url(
headers.pop("content-type"),
)
}
return self._request("PUT", final_url, files=_file, headers=headers, data=_data)
response = self._request(
"PUT", final_url, files=_file, headers=headers, data=_data
)
data: UploadData = response.json()

return UploadResponse(path=path, Key=data.get("Key"))

def create_signed_url(
self, path: str, expires_in: int, options: URLOptions = {}
Expand Down
11 changes: 7 additions & 4 deletions tests/_async/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,15 @@ async def test_client_upload_to_signed_url(
"""Ensure we can upload to a signed URL"""
data = await storage_file_client.create_signed_upload_url(file.bucket_path)
assert data["path"]
upload_result = await storage_file_client.upload_to_signed_url(
await storage_file_client.upload_to_signed_url(
data["path"], data["token"], file.file_content, {"content-type": file.mime_type}
)
upload_data = upload_result.json()
assert upload_data
assert upload_data.get("error") is None
image = await storage_file_client.download(file.bucket_path)
files = await storage_file_client.list(file.bucket_folder)
image_info = next((f for f in files if f.get("name") == file.name), None)

assert image == file.file_content
assert image_info.get("metadata", {}).get("mimetype") == file.mime_type


async def test_client_create_signed_url(
Expand Down
11 changes: 7 additions & 4 deletions tests/_sync/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,15 @@ def test_client_upload_to_signed_url(
"""Ensure we can upload to a signed URL"""
data = storage_file_client.create_signed_upload_url(file.bucket_path)
assert data["path"]
upload_result = storage_file_client.upload_to_signed_url(
storage_file_client.upload_to_signed_url(
data["path"], data["token"], file.file_content, {"content-type": file.mime_type}
)
upload_data = upload_result.json()
assert upload_data
assert upload_data.get("error") is None
image = storage_file_client.download(file.bucket_path)
files = storage_file_client.list(file.bucket_folder)
image_info = next((f for f in files if f.get("name") == file.name), None)

assert image == file.file_content
assert image_info.get("metadata", {}).get("mimetype") == file.mime_type


def test_client_create_signed_url(
Expand Down

0 comments on commit 748067f

Please sign in to comment.