Skip to content

Commit 086d925

Browse files
authored
fix: use httpx in storage file upload (#130)
* chore: format headers like js client (https://github.com/supabase/storage-js/blob/904d1b9e5804dac21e736b9a5a4b12424c093ffb/src/lib/StorageFileApi.ts#L83-L84) * chore: use httpx in update * fix: replace [ ] by ( )
1 parent 729b2d9 commit 086d925

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

supabase/lib/storage/storage_file_api.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
from typing import Any
22

33
import httpx
4-
import requests
54
from httpx import HTTPError
6-
from requests import HTTPError as RequestsHTTPError
7-
from requests_toolbelt import MultipartEncoder
85

96

107
class StorageFileAPI:
@@ -17,8 +14,8 @@ class StorageFileAPI:
1714
},
1815
}
1916
DEFAULT_FILE_OPTIONS = {
20-
"cacheControl": "3600",
21-
"contentType": "text/plain;charset=UTF-8",
17+
"cache-control": "3600",
18+
"content-type": "text/plain;charset=UTF-8",
2219
"x-upsert": "false",
2320
}
2421

@@ -191,18 +188,15 @@ def upload(self, path: str, file: Any, file_options: dict = None):
191188
headers = dict(self.headers, **self.DEFAULT_FILE_OPTIONS)
192189
headers.update(file_options)
193190
filename = path.rsplit("/", maxsplit=1)[-1]
194-
files = MultipartEncoder(
195-
fields={"file": (filename, open(file, "rb"), headers["contentType"])}
196-
)
197-
headers["Content-Type"] = files.content_type
191+
files = {"file": (filename, open(file, "rb"), headers.pop("content-type"))}
198192
_path = self._get_final_path(path)
199193
try:
200-
resp = requests.post(
194+
resp = httpx.post(
201195
f"{self.url}/object/{_path}",
202-
data=files,
196+
files=files,
203197
headers=headers,
204198
)
205-
except RequestsHTTPError as http_err:
199+
except HTTPError as http_err:
206200
print(f"HTTP error occurred: {http_err}") # Python 3.6
207201
except Exception as err:
208202
raise err # Python 3.6

0 commit comments

Comments
 (0)