Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/metorial/_file_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,19 @@ def _upload_with_url(
}
)

_put_to_storage(upload.get("method") or "PUT", upload["url"], source)
_put_to_storage(
upload.get("method") or "PUT", upload["url"], source, upload.get("headers") or {}
)

complete: dict[str, Any] = {"mode": "complete", "file_upload_id": session["id"]}
if instance_id is not None:
complete["instance_id"] = instance_id
return manager._request("POST", MetorialRequest(path=["files"], body=complete))


def _put_to_storage(method: str, url: str, source: _Source) -> None:
def _put_to_storage(
method: str, url: str, source: _Source, extra_headers: dict[str, str] | None = None
) -> None:
# The URL signature covers the content type, so it must match the declared
# file_type, and Metorial credentials must not be sent to the storage backend.
try:
Expand All @@ -135,6 +139,7 @@ def _put_to_storage(method: str, url: str, source: _Source) -> None:
url,
data=source.stream,
headers={
**(extra_headers or {}),
"Content-Type": source.content_type,
"Content-Length": str(source.size),
},
Expand Down
7 changes: 6 additions & 1 deletion tests/test_file_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ def handle(self, handler: BaseHTTPRequestHandler) -> None:
{
"object": "file_upload",
"id": "fup_1",
"upload": {"url": f"http://{host}/storage", "method": "PUT"},
"upload": {
"url": f"http://{host}/storage",
"method": "PUT",
"headers": {"x-ms-blob-type": "BlockBlob"},
},
},
)
else:
Expand Down Expand Up @@ -235,6 +239,7 @@ def test_large_files_use_an_upload_url(
assert put["method"] == "PUT"
assert put["body"] == b"large contents"
assert put["headers"]["Content-Type"] == "application/octet-stream"
assert put["headers"]["x-ms-blob-type"] == "BlockBlob"
assert "Authorization" not in put["headers"]
assert complete["json"] == {
"mode": "complete",
Expand Down
Loading