Skip to content

Commit e85d675

Browse files
author
Joel Lee
committed
feat: add download function
1 parent b518ad3 commit e85d675

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

supabase_py/lib/storage/storage_file_api.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def move(self, from_path: str, to_path: str):
8282
try:
8383
response = requests.post(
8484
f"{self.url}/object/move",
85-
data={
85+
json={
8686
"bucketId": self.bucket_id,
8787
"sourceKey": from_path,
8888
"destinationKey": to_path,
@@ -133,6 +133,7 @@ def list(self, path: str = None, options: dict = {}):
133133
body = dict(self.DEFAULT_SEARCH_OPTIONS, **options)
134134
headers = dict(self.headers, **{"Content-Type": "application/json"})
135135
body["prefix"] = path if path else ""
136+
136137
getdata = requests.post(
137138
f"{self.url}/object/list/{self.bucket_id}", json=body, headers=headers
138139
)
@@ -144,5 +145,23 @@ def list(self, path: str = None, options: dict = {}):
144145
else:
145146
return getdata.json()
146147

148+
def download(self, path: str):
149+
"""
150+
Downloads a file.
151+
Parameters
152+
----------
153+
path The file path to be downloaded, including the path and file name. For example `folder/image.png`.
154+
"""
155+
try:
156+
_path = self._get_final_path(path)
157+
response = requests.get(f"{self.url}/object/{_path}", headers=self.headers)
158+
159+
except HTTPError as http_err:
160+
print(f"HTTP error occurred: {http_err}") # Python 3.6
161+
except Exception as err:
162+
raise err # Python 3.6
163+
else:
164+
return response.content
165+
147166
def _get_final_path(self, path: str):
148167
return f"{self.bucket_id}/{path}"

0 commit comments

Comments
 (0)