Skip to content

Commit

Permalink
v1.1.0: Added support for overwriting existing files
Browse files Browse the repository at this point in the history
  • Loading branch information
SiddhantSadangi committed Aug 20, 2023
1 parent 2f1a2cc commit b62cc71
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 27 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ A Streamlit connection component to connect Streamlit to Supabase Storage and Da
bucket_id = st.text_input("Enter the bucket_id")
uploaded_file = st.file_uploader("Choose a file")
destination_path = st.text_input("Enter destination path")

overwrite = "true" if st.checkbox("Overwrite if exists?") else "false"

with open(uploaded_file.name, "wb") as f:
f.write(uploaded_file.getbuffer())

Expand All @@ -96,7 +97,10 @@ A Streamlit connection component to connect Streamlit to Supabase Storage and Da
supabase_client.storage.from_(bucket_id).upload(
path=destination_path,
file=f,
file_options={"content-type": uploaded_file.type},
file_options={
"content-type": uploaded_file.type,
"x-upsert": overwrite,
},
)

```
Expand All @@ -115,9 +119,12 @@ A Streamlit connection component to connect Streamlit to Supabase Storage and Da
bucket_id = st.text_input("Enter the bucket_id")
uploaded_file = st.file_uploader("Choose a file"):
destination_path = st.text_input("Enter destination path")
overwrite = "true" if st.checkbox("Overwrite if exists?") else "false"

if st.button("Upload"):
st_supabase_client.upload(bucket_id, "local", uploaded_file, destination_path)
st_supabase_client.upload(
bucket_id, "local", uploaded_file, destination_path, overwrite,
)
```
<tr>
</table>
Expand Down
48 changes: 30 additions & 18 deletions demo/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,14 +344,16 @@

if source == "local":
file = rcol.file_uploader("Choose a file")
if file:
destination_path = st.text_input(
"Destination path in the bucket",
value=file.name,
)
lcol, rcol = st.columns([3, 1])

destination_path = lcol.text_input(
"Destination path in the bucket",
value=file.name if file else "",
)
overwrite = "true" if rcol.checkbox("Overwrite if exists?") else "false"

constructed_storage_query = f"""
st_supabase.{operation}("{bucket_id}", {source=}, file={file}, destination_path="{destination_path}")
st_supabase.{operation}("{bucket_id}", {source=}, file={file}, destination_path="{destination_path}", {overwrite=})
# `UploadedFile` is the `BytesIO` object returned by `st.file_uploader()`
"""
else:
Expand All @@ -360,12 +362,14 @@
placeholder="path/to/file.txt",
help="This is the path of the file on the Streamlit hosted filesystem",
)
destination_path = st.text_input(
lcol, rcol = st.columns([3, 1])
destination_path = lcol.text_input(
"Destination path in the bucket",
value=file,
)
overwrite = "true" if rcol.checkbox("Overwrite if exists?") else "false"
constructed_storage_query = f"""
st_supabase.{operation}("{bucket_id}", {source=}, {file=}, destination_path="{destination_path}")
st_supabase.{operation}("{bucket_id}", {source=}, {file=}, destination_path="{destination_path}", {overwrite=})
"""
st.session_state["storage_disabled"] = False if all([bucket_id, file]) else True
elif operation == "list_buckets":
Expand Down Expand Up @@ -535,12 +539,12 @@
options=["local", "hosted"],
help="Filesystem from where the file has to be uploaded",
)

overwrite = "false"
if source == "local":
file = rcol.file_uploader("Choose a file")

constructed_storage_query = f"""
st_supabase.{operation}("{bucket_id}", {source=}, {path=}, token="***", file={file})
st_supabase.{operation}("{bucket_id}", {source=}, {path=}, token="***", file={file}, {overwrite=})
# `UploadedFile` is the `BytesIO` object returned by `st.file_uploader()`
"""
elif source == "hosted":
Expand All @@ -550,7 +554,7 @@
help="This is the path of the file on the Streamlit hosted filesystem",
)
constructed_storage_query = f"""
st_supabase.{operation}("{bucket_id}", {source=}, {path=}, token="***", {file=})
st_supabase.{operation}("{bucket_id}", {source=}, {path=}, token="***", {file=}, {overwrite=})
"""
st.session_state["storage_disabled"] = False if all([bucket_id, token, path]) else True

Expand All @@ -563,11 +567,8 @@
else:
st.code(constructed_storage_query, language="python")

st.session_state["storage_disabled"] = (
True
if st.session_state["project"] == "demo" and operation in RESTRICTED_STORAGE_OPERATORS
else st.session_state["storage_disabled"]
)
if st.session_state["project"] == "demo" and operation in RESTRICTED_STORAGE_OPERATORS:
st.session_state["storage_disabled"] = True

if st.session_state["project"] == "demo" and operation in RESTRICTED_STORAGE_OPERATORS:
help = f"'{selected_operation.capitalize()}' not allowed in demo project"
Expand All @@ -586,7 +587,13 @@
):
try:
if operation == "upload":
response = st_supabase.upload(bucket_id, source, file, destination_path)
response = st_supabase.upload(
bucket_id,
source,
file,
destination_path,
overwrite,
)
elif operation == "download":
file_name, mime, data = eval(constructed_storage_query)
st.success(
Expand All @@ -601,7 +608,12 @@
)
elif operation == "upload_to_signed_url":
response = st_supabase.upload_to_signed_url(
bucket_id, source, path, token, file
bucket_id,
source,
path,
token,
file,
overwrite,
)
else:
response = eval(constructed_storage_query)
Expand Down
16 changes: 10 additions & 6 deletions src/st_supabase_connection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from streamlit.connections import ExperimentalBaseConnection
from supabase import Client, create_client

__version__ = "1.0.1"
__version__ = "1.1.0"


class SupabaseConnection(ExperimentalBaseConnection[Client]):
Expand Down Expand Up @@ -167,13 +167,13 @@ def create_bucket(
)
return response.json()

# TODO: Support overwriting existing files
def upload(
self,
bucket_id: str,
source: Literal["local", "hosted"],
file: Union[str, Path, BytesIO],
destination_path: str,
overwrite: Literal["true", "false"] = "false",
) -> dict[str, str]:
"""Uploads a file to a Supabase bucket.
Expand All @@ -189,7 +189,9 @@ def upload(
or the `BytesIO` object returned by `st.file_uploader()` if `source="local"`.
destination_path : str
Path is the bucket where the file will be uploaded to.
Folders will be created as needed. Defaults to `/filename.fileext`
Folders will be created as needed. Defaults to `/filename.fileext`.
overwrite : str
Whether to overwrite existing file. Defaults to `false`.
"""

if source == "local":
Expand All @@ -199,14 +201,17 @@ def upload(
response = self.client.storage.from_(bucket_id).upload(
path=destination_path or f"/{file.name}",
file=f,
file_options={"content-type": file.type},
file_options={"content-type": file.type, "x-upsert": overwrite},
)
elif source == "hosted":
with open(file, "rb") as f:
response = self.client.storage.from_(bucket_id).upload(
path=destination_path or f"/{os.path.basename(f.name)}",
file=f,
file_options={"content-type": mimetypes.guess_type(file)[0]},
file_options={
"content-type": mimetypes.guess_type(file)[0],
"x-upsert": overwrite,
},
)
return response.json()

Expand Down Expand Up @@ -446,7 +451,6 @@ def create_signed_upload_url(self, bucket_id: str, path: str) -> dict[str, str]:
"path": path,
}

# TODO: Support overwriting existing files
def upload_to_signed_url(
self,
bucket_id: str,
Expand Down

0 comments on commit b62cc71

Please sign in to comment.