-
-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from supabase/j0_add_storage_bucket
Add Storage Bucket API
- Loading branch information
Showing
6 changed files
with
163 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
from typing import Dict, Any | ||
|
||
import requests | ||
from requests import HTTPError | ||
|
||
|
||
class StorageBucketAPI: | ||
"""This class abstracts access to the endpoint to the Get, List, Empty, and Delete operations on a bucket""" | ||
|
||
def __init__(self, url, headers): | ||
self.url = url | ||
self.headers = headers | ||
|
||
def list_buckets(self) -> Dict[str, Any]: | ||
"""Retrieves the details of all storage buckets within an existing product.""" | ||
try: | ||
response = requests.get(f"{self.url}/bucket", headers=self.headers) | ||
response.raise_for_status() | ||
except HTTPError as http_err: | ||
print(f"HTTP error occurred: {http_err}") # Python 3.6 | ||
except Exception as err: | ||
print(f"Other error occurred: {err}") # Python 3.6 | ||
else: | ||
return response.json() | ||
|
||
def get_bucket(self, id: str) -> Dict[str, Any]: | ||
"""Retrieves the details of an existing storage bucket. | ||
Parameters | ||
---------- | ||
id | ||
The unique identifier of the bucket you would like to retrieve. | ||
""" | ||
try: | ||
response = requests.get(f"{self.url}/bucket/{id}", headers=self.headers) | ||
response.raise_for_status() | ||
except HTTPError as http_err: | ||
print(f"HTTP error occurred: {http_err}") # Python 3.6 | ||
except Exception as err: | ||
print(f"Other error occurred: {err}") # Python 3.6 | ||
else: | ||
return response.json() | ||
|
||
def create_bucket(self, id: str) -> Dict[str, Any]: | ||
"""Creates a new storage bucket. | ||
Parameters | ||
---------- | ||
id | ||
A unique identifier for the bucket you are creating. | ||
""" | ||
try: | ||
response = requests.post(f"{self.url}/bucket", data={"id": id}, headers=self.headers) | ||
response.raise_for_status() | ||
except HTTPError as http_err: | ||
print(f"HTTP error occurred: {http_err}") # Python 3.6 | ||
except Exception as err: | ||
print(f"Other error occurred: {err}") # Python 3.6 | ||
else: | ||
return response.json() | ||
|
||
def empty_bucket(self, id: str) -> Dict[str, Any]: | ||
"""Removes all objects inside a single bucket. | ||
Parameters | ||
---------- | ||
id | ||
The unique identifier of the bucket you would like to empty. | ||
""" | ||
try: | ||
response = requests.post(f"{self.url}/bucket/{id}/empty", data={}, headers=self.headers) | ||
response.raise_for_status() | ||
except HTTPError as http_err: | ||
print(f"HTTP error occurred: {http_err}") # Python 3.6 | ||
except Exception as err: | ||
print(f"Other error occurred: {err}") # Python 3.6 | ||
else: | ||
return response.json() | ||
|
||
def delete_bucket(self, id: str) -> Dict[str, Any]: | ||
"""Deletes an existing bucket. Note that you cannot delete buckets with existing objects inside. You must first | ||
`empty()` the bucket. | ||
Parameters | ||
---------- | ||
id | ||
The unique identifier of the bucket you would like to delete. | ||
""" | ||
try: | ||
response = requests.delete(f"{self.url}/bucket/{id}", data={}, headers=self.headers) | ||
|
||
response.raise_for_status() | ||
except HTTPError as http_err: | ||
print(f"HTTP error occurred: {http_err}") # Python 3.6 | ||
except Exception as err: | ||
print(f"Other error occurred: {err}") # Python 3.6 | ||
else: | ||
return response.json() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from supabase_py.lib.storage.storage_bucket_api import StorageBucketAPI | ||
|
||
|
||
class SupabaseStorageClient(StorageBucketAPI): | ||
""" | ||
Manage the storage bucket and files | ||
Examples | ||
-------- | ||
>>> url = storage_file.create_signed_url("poll3o/test2.txt", 80) # signed url | ||
>>> loop.run_until_complete(storage_file.download("poll3o/test2.txt")) #upload or download | ||
>>> loop.run_until_complete(storage_file.upload("poll3o/test2.txt","path_file_upload")) | ||
>>> list_buckets = storage.list_buckets() | ||
>>> list_files = storage_file.list("pollo") | ||
""" | ||
|
||
def __init__(self, url, headers): | ||
super().__init__(url, headers) | ||
|
||
# def StorageFileApi(self, id_, replace=False): | ||
# return StorageFileApi(self.url, self.headers, id_, replace) | ||
|
||
def StorageBucketAPI(self): | ||
return StorageBucketAPI(self.url, self.headers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters