Skip to content

Commit 41682ad

Browse files
Joel LeeJoel Lee
Joel Lee
authored and
Joel Lee
committed
feat: Add more functions to storage file api
1 parent 24cc3fd commit 41682ad

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

Diff for: supabase_py/lib/storage/storage_file_api.py

+82-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33

44

55
class StorageFileApi:
6+
DEFAULT_SEARCH_OPTIONS = {
7+
"limit": 100,
8+
"offset": 0,
9+
"sortBy": {
10+
"column": "name",
11+
"order": "asc",
12+
},
13+
}
14+
615
def __init__(self, url: str, headers: dict, bucket_id: str):
716
"""
817
Parameters
@@ -50,7 +59,79 @@ def create_signed_url(self, path: str, expires_in: int):
5059
return data
5160

5261
def move(self, from_path: str, to_path: str):
53-
pass
62+
"""
63+
Moves an existing file, optionally renaming it at the same time.
64+
Parameters
65+
----------
66+
from_path
67+
The original file path, including the current file name. For example `folder/image.png`.
68+
to_path
69+
The new file path, including the new file name. For example `folder/image-copy.png`.
70+
"""
71+
try:
72+
response = requests.post(
73+
f"{self.url}/object/move",
74+
data={
75+
"bucketId": self.bucket_id,
76+
"sourceKey": from_path,
77+
"destinationKey": to_path,
78+
},
79+
headers=self.headers,
80+
)
81+
response.raise_for_status()
82+
except HTTPError as http_err:
83+
print(f"HTTP error occurred: {http_err}") # Python 3.6
84+
except Exception as err:
85+
print(f"Other error occurred: {err}") # Python 3.6
86+
else:
87+
return response.json()
88+
89+
def remove(self, paths: list):
90+
"""
91+
Deletes files within the same bucket
92+
Parameters
93+
----------
94+
paths
95+
An array or list of files to be deletes, including the path and file name. For example [`folder/image.png`].
96+
"""
97+
try:
98+
response = requests.delete(
99+
f"{self.url}/object/{self.bucket_id}",
100+
data={"prefixes": paths},
101+
headers=self.headers,
102+
)
103+
response.raise_for_status()
104+
except HTTPError as http_err:
105+
print(f"HTTP error occurred: {http_err}") # Python 3.6
106+
except Exception as err:
107+
raise err # Python 3.6
108+
else:
109+
return response.json()
110+
111+
def list(self, path: str = None, options: dict = {}):
112+
"""
113+
Lists all the files within a bucket.
114+
Parameters
115+
----------
116+
path
117+
The folder path.
118+
options
119+
Search options, including `limit`, `offset`, and `sortBy`.
120+
"""
121+
try:
122+
body = dict(self.DEFAULT_SEARCH_OPTIONS, **options)
123+
headers = dict(self.headers, **{"Content-Type": "application/json"})
124+
body["prefix"] = path if path else ""
125+
getdata = requests.post(
126+
f"{self.url}/object/list/{self.bucket_id}", json=body, headers=headers
127+
)
128+
getdata.raise_for_status()
129+
except HTTPError as http_err:
130+
print(f"HTTP error occurred: {http_err}") # Python 3.6
131+
except Exception as err:
132+
raise err # Python 3.6
133+
else:
134+
return getdata.json()
54135

55136
def _get_final_path(self, path: str):
56137
return f"{self.bucket_id}/{path}"

0 commit comments

Comments
 (0)