|
1 | 1 | from typing import Any
|
2 | 2 |
|
3 | 3 | import httpx
|
| 4 | +import requests |
4 | 5 | from httpx import HTTPError
|
| 6 | +from requests import HTTPError as RequestsHTTPError |
| 7 | +from requests_toolbelt import MultipartEncoder |
5 | 8 |
|
6 | 9 |
|
7 | 10 | class StorageFileAPI:
|
@@ -71,8 +74,7 @@ def get_public_url(self, path: str):
|
71 | 74 | """
|
72 | 75 | try:
|
73 | 76 | _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}" |
76 | 78 | except:
|
77 | 79 | print("Public URL not found")
|
78 | 80 |
|
@@ -140,7 +142,7 @@ def list(self, path: str = None, options: dict = {}):
|
140 | 142 | try:
|
141 | 143 | body = dict(self.DEFAULT_SEARCH_OPTIONS, **options)
|
142 | 144 | headers = dict(self.headers, **{"Content-Type": "application/json"})
|
143 |
| - body["prefix"] = path if path else "" |
| 145 | + body["prefix"] = path or "" |
144 | 146 | getdata = httpx.post(
|
145 | 147 | f"{self.url}/object/list/{self.bucket_id}",
|
146 | 148 | json=body,
|
@@ -189,15 +191,18 @@ def upload(self, path: str, file: Any, file_options: dict = None):
|
189 | 191 | headers = dict(self.headers, **self.DEFAULT_FILE_OPTIONS)
|
190 | 192 | headers.update(file_options)
|
191 | 193 | 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 |
193 | 198 | _path = self._get_final_path(path)
|
194 | 199 | try:
|
195 |
| - resp = httpx.post( |
| 200 | + resp = requests.post( |
196 | 201 | f"{self.url}/object/{_path}",
|
197 |
| - files=files, |
| 202 | + data=files, |
198 | 203 | headers=headers,
|
199 | 204 | )
|
200 |
| - except HTTPError as http_err: |
| 205 | + except RequestsHTTPError as http_err: |
201 | 206 | print(f"HTTP error occurred: {http_err}") # Python 3.6
|
202 | 207 | except Exception as err:
|
203 | 208 | raise err # Python 3.6
|
|
0 commit comments