Upload multiple files to a temporary folder on a hosted application

Quick question. I have a Streamlit app that locally works well. Locally, I can input the folder path to the location of files to be read and uploaded somewhere else like on Sharepoint.

upload_url = st.text_input ("Please Copy and Paste the folder path to the files to be uploaded")
dir_list = os.listdir(upload_url )

I understand that on a hosted app on say Heroku, the local folder path will not be read. However, I still need to read the files and upload them to Sharepoint. Work around would probably be to upload the files to a temporary folder on the server from where the upload to a Sharepoint location can be made. Is this practically possible and if so, how can this be achieved?

uploaded_files = st.file_uploader("Choose the files to be  uploaded", accept_multiple_files=True)
for uploaded_file in uploaded_files:
       bytes_data = uploaded_file.read()
       st.write(uploaded_files)

Hi @wandabwa2004 -

In terms of your question, you probably can write to a temporary directory somehow, but at the same time, it’s important to understand how the file_uploader widget works.

When the user does a file_uploader operation, the bytes of the file are transmitted through the browser to Streamlit. These bytes reside in RAM. In the manner you are talking about, you could write these bytes to a file in temp space somewhere, but then you would need to read them back in to Python so that you could upload via the Sharepoint API (I assume you’re talking about an API?). So you can skip the step of writing to a temporary file location on the server where Streamlit is running, and instead process those files while they are in Python coming from file_uploader and send them to Sharepoint.

If it’s the case that Sharepoint actually needs to read from a directory, such as in a batch process of some sort, then you can take the bytes provided from file_uploader, then use the appropriate library to send that to an S3 bucket, Google Driver, SFTP, wherever that location needs to be. Then your batch process can read out of the directory.

Best,
Randy

2 Likes

Thank you @randyzwitch. I managed to come up with a solution and is here

A working version is here https://sharepointuploader.herokuapp.com/ to try.