Skip to content

Commit ed99717

Browse files
authored
fix: use requests for upload (#121)
* fix: use requests for upload * 'Refactored by Sourcery' Co-authored-by: Sourcery AI <>
1 parent 4b2a181 commit ed99717

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

poetry.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ postgrest-py = "0.7.0"
2020
realtime = "^0.0.4"
2121
gotrue = "^0.3.0"
2222
httpx = ">=0.19,<0.22"
23+
requests = "^2.27.1"
24+
requests-toolbelt = "^0.9.1"
2325

2426
[tool.poetry.dev-dependencies]
2527
pre-commit = "^2.16.0"

supabase/lib/storage/storage_file_api.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from typing import Any
22

33
import httpx
4+
import requests
45
from httpx import HTTPError
6+
from requests import HTTPError as RequestsHTTPError
7+
from requests_toolbelt import MultipartEncoder
58

69

710
class StorageFileAPI:
@@ -71,8 +74,7 @@ def get_public_url(self, path: str):
7174
"""
7275
try:
7376
_path = self._get_final_path(path)
74-
public_url = f"{self.url}/object/public/{_path}"
75-
return public_url
77+
return f"{self.url}/object/public/{_path}"
7678
except:
7779
print("Public URL not found")
7880

@@ -140,7 +142,7 @@ def list(self, path: str = None, options: dict = {}):
140142
try:
141143
body = dict(self.DEFAULT_SEARCH_OPTIONS, **options)
142144
headers = dict(self.headers, **{"Content-Type": "application/json"})
143-
body["prefix"] = path if path else ""
145+
body["prefix"] = path or ""
144146
getdata = httpx.post(
145147
f"{self.url}/object/list/{self.bucket_id}",
146148
json=body,
@@ -189,15 +191,18 @@ def upload(self, path: str, file: Any, file_options: dict = None):
189191
headers = dict(self.headers, **self.DEFAULT_FILE_OPTIONS)
190192
headers.update(file_options)
191193
filename = path.rsplit("/", maxsplit=1)[-1]
192-
files = {"file": (filename, open(file, "rb"), headers["contentType"])}
194+
files = MultipartEncoder(
195+
fields={"file": (filename, open(file, "rb"), headers["contentType"])}
196+
)
197+
headers["Content-Type"] = files.content_type
193198
_path = self._get_final_path(path)
194199
try:
195-
resp = httpx.post(
200+
resp = requests.post(
196201
f"{self.url}/object/{_path}",
197-
files=files,
202+
data=files,
198203
headers=headers,
199204
)
200-
except HTTPError as http_err:
205+
except RequestsHTTPError as http_err:
201206
print(f"HTTP error occurred: {http_err}") # Python 3.6
202207
except Exception as err:
203208
raise err # Python 3.6

0 commit comments

Comments
 (0)