Managing Files

This notebook shows how you can manage files with the ThanoSQL library. We will cover how to upload files, get file information, and delete files. First, initialize a ThanoSQL client with your API token and engine URL.

from thanosql import ThanoSQL

client = ThanoSQL(
    api_token="THANOSQL_API_TOKEN",
    engine_url="THANOSQL_ENGINE_URL"
)

Uploading Files and Creating Folders

You can upload files from your local computer to your ThanoSQL workspace. By default, files will be uploaded to the user data root. You can use the path parameter to specify another folder location relative to the user data root. If the directory does not exist, the API call will return an error.

dir_path = "path/to/existing/workspace/dir"
file_path = "path/to/local/my_file.jpeg"
res = client.file.create(dir_path, file_path)
assert res.content_info.name == "my_file"
assert res.content_info.type == "file"
assert res.content_info.path == "path/to/existing/workspace/dir/my_file.jpeg"

You can also use this API to create an empty folder. If the second parameter (file) is not set, an empty folder with the specified path will be created.

new_dir_path = "path/to/new/folder"
res = client.file.create(new_dir_path)
assert res.content_info.name == "folder"
assert res.content_info.type == "directory"

Listing and Downloading Files

You can get the list of files in a directory under the user data root using the get() function. By default, the function will list the contents of the user data root.

res = client.file.get()
res.content_info.content

You can also specify the path to a target file or directory. Note that this function is not recursive; files inside subdirectories will not be listed.

client.file.get("path/to/existing/workspace/dir/my_file.jpeg")
# will output
#
# Content({
#    "content_info": {
#       "name": "my_file",
# ...

get can also be used to download files from your workspace to your local machine. You only need to specify “download” in the option parameter. Directory download is currently not supported.

client.file.get("path/to/existing/workspace/dir/my_file.jpeg", option="download")
# will output nothing except <Response [204]>, but my_file.jpeg will be downloaded to the current local directory

Deleting Files

You can also delete files and directories from your workspace using the client. Note that deleting a directory will also delete all contents inside it.

client.file.delete("path/to/new/folder")