Where are Uploaded Files Stored?

I read a few other posts on this, but I didn’t exactly understand and they were closed. I’m trying to figure out where the files that are uploaded using st.file_uploader are stored.

I didn’t see anywhere in my streamlit account to access files that have been uploaded or similar. If the files aren’t accessible anywhere, does anyone have a suggestion for a small (~15gb/yr) amount of storage that will play nicely with streamlit’s upload and then data processing?

Code attached

st.file_uploader does not save files to disk. The returned file(s) exist in memory in your Python environment. Just like x=2 stores an integer value assigned to x, y=st.file_uploader("Files") stores an UploadedFile (extension of BytesIO) assigned to y. (…or list of UploadedFile when multiple files are allowed)

If you need to work with your files on disk, you’ll have to manually write them to a file or use tempfile to create a temporary file on disk.

1 Like

One other thing to note is that your files exist on disk until the application is restarted. If you want to persist them, you should use a cloud storage solution such as GCS or AWS S3.

2 Likes

What if I do not want to persist the files but I don’t want to store the files in memory?

In my use case it would be useful to have users essentially have their own instance of the app and its backing filesystem. I was kind of hoping that we could specify that when a new user connects to the app, a new system and environment would be spun up.

I understand that we could use tempfile to create a unique directory or use files in memory. The former would be non-ideal because I’d like users to be able to reload their created files if they reconnect to a new session (like if they used the app on their own laptop). The latter would be non-ideal because sometimes memory usage in the app can be very high and very many files could be generated.

Is there any way to force a new app instance for each session or user? Or is a shared deployment just the way Streamlit Community Cloud works?

Thank you!

@mathcatsand @Alexandru_Toader

When you deploy an app to Community Cloud, you have one Streamlit server running your app. When a user visits you app, a thread is created to handle their session. You have one file system in one environment running that one Streamlit server. Files on disk are accessible to that Streamlit server and thus to all sessions. Session State is something that is unique to each session, but it is something that is “in memory” and not on disk.

If you don’t want files to persist, but do want them on disk, there isn’t anything to logically isolate those files for a Community Cloud app (at leas for files written to the Community Cloud environment). Tempfile is the best thing I know about to write things temporarily to disk.

If you want to save files that users can reload, I recommend using an external storage solution for that, like @Alexandru_Toader mentioned.

1 Like

Got it, much appreciated @mathcatsand!

1 Like