Skip to content

fix: use requests for upload #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 17, 2022
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
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