Skip to content

Commit

Permalink
fix: use requests for upload (#121)
Browse files Browse the repository at this point in the history
* fix: use requests for upload

* 'Refactored by Sourcery'

Co-authored-by: Sourcery AI <>
  • Loading branch information
leynier authored Jan 17, 2022
1 parent 4b2a181 commit ed99717
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
19 changes: 12 additions & 7 deletions supabase/lib/storage/storage_file_api.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ed99717

Please sign in to comment.