diff --git a/poetry.lock b/poetry.lock index 22e92320..99f8da6c 100644 --- a/poetry.lock +++ b/poetry.lock @@ -822,7 +822,7 @@ websockets = ">=9.1,<10.0" name = "requests" version = "2.27.1" description = "Python HTTP for Humans." -category = "dev" +category = "main" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*" @@ -840,7 +840,7 @@ use_chardet_on_py3 = ["chardet (>=3.0.2,<5)"] name = "requests-toolbelt" version = "0.9.1" description = "A utility belt for advanced users of python-requests" -category = "dev" +category = "main" optional = false python-versions = "*" @@ -1007,7 +1007,7 @@ python-versions = ">=3.6" name = "urllib3" version = "1.26.8" description = "HTTP library with thread-safe connection pooling, file post, and more." -category = "dev" +category = "main" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, <4" @@ -1074,7 +1074,7 @@ testing = ["pytest (>=6)", "pytest-checkdocs (>=2.4)", "pytest-flake8", "pytest- [metadata] lock-version = "1.1" python-versions = "^3.7" -content-hash = "4afeceeaf06dbfe514f648451e8c7cb3ab9938b356606e425dfa82034b736d52" +content-hash = "c0ff5410d14f26d403fcf2d48daf5ad26b914c382afccffade59c9c37091ab85" [metadata.files] anyio = [ diff --git a/pyproject.toml b/pyproject.toml index 4d73e5bd..b70a8a26 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,6 +20,8 @@ postgrest-py = "0.7.0" realtime = "^0.0.4" gotrue = "^0.3.0" httpx = ">=0.19,<0.22" +requests = "^2.27.1" +requests-toolbelt = "^0.9.1" [tool.poetry.dev-dependencies] pre-commit = "^2.16.0" diff --git a/supabase/lib/storage/storage_file_api.py b/supabase/lib/storage/storage_file_api.py index 9c5d7c39..5af7f732 100644 --- a/supabase/lib/storage/storage_file_api.py +++ b/supabase/lib/storage/storage_file_api.py @@ -1,7 +1,10 @@ from typing import Any import httpx +import requests from httpx import HTTPError +from requests import HTTPError as RequestsHTTPError +from requests_toolbelt import MultipartEncoder class StorageFileAPI: @@ -71,8 +74,7 @@ def get_public_url(self, path: str): """ try: _path = self._get_final_path(path) - public_url = f"{self.url}/object/public/{_path}" - return public_url + return f"{self.url}/object/public/{_path}" except: print("Public URL not found") @@ -140,7 +142,7 @@ def list(self, path: str = None, options: dict = {}): try: body = dict(self.DEFAULT_SEARCH_OPTIONS, **options) headers = dict(self.headers, **{"Content-Type": "application/json"}) - body["prefix"] = path if path else "" + body["prefix"] = path or "" getdata = httpx.post( f"{self.url}/object/list/{self.bucket_id}", json=body, @@ -189,15 +191,18 @@ def upload(self, path: str, file: Any, file_options: dict = None): headers = dict(self.headers, **self.DEFAULT_FILE_OPTIONS) headers.update(file_options) filename = path.rsplit("/", maxsplit=1)[-1] - files = {"file": (filename, open(file, "rb"), headers["contentType"])} + files = MultipartEncoder( + fields={"file": (filename, open(file, "rb"), headers["contentType"])} + ) + headers["Content-Type"] = files.content_type _path = self._get_final_path(path) try: - resp = httpx.post( + resp = requests.post( f"{self.url}/object/{_path}", - files=files, + data=files, headers=headers, ) - except HTTPError as http_err: + except RequestsHTTPError as http_err: print(f"HTTP error occurred: {http_err}") # Python 3.6 except Exception as err: raise err # Python 3.6