Box¶
Overview¶
Box is a commercial file sharing service similar to Dropbox and Google Drive. The current Parsons API supports listing files and folders, creating and deleting folders, and uploading/downloading Parsons tables as either CSV or JSON files.
Authentication
This connector requires authentication credentials for a Box account. The simplest form of authentication is a through a BoxDeveloperTokenAuth, and this document will cover how to use it. Information on setting up credentials for a developer access token can be found at the Create a Platform App page. On the Configuration tab, check enable “Write all files and folders stored in Box” under Application Scopes, and save, prior to generating a token. If you fail to do so, the token will not have the correct scope. Developer access tokens are not recommended for production environments. However, Box supports a variety of authentication methods.
Quickstart¶
This class requires credentials in the form of Box Authentication passed to the constructor, or made available as an environment variable in the form of an access token:
# Note: these are fake keys, provided as examples.
export BOX_ACCESS_TOKEN=7B39m3ozIGyTcazbWRbi5F2SSZ5J
Performance
Most functions in this class exist both in ‘path’-based form and ‘folder_id’/’file_id’-based form.
The path-based forms rely on the get_item_id() method,
which navigates through subfolders with sequential API calls. This can be slow and computationally
expensive if the Box path string in question is long, or intermediate folders contain many items.
If efficiency and memory management is paramount, consider using the “by_id” methods of each function.
In most cases, the id will be more accessible from returns on upload methods despite this
documentation describing both methods.
from parsons import Box
box = Box()
from parsons import Box
from box_sdk_gen import BoxDeveloperTokenAuth
access_token = "7B39m3ozIGyTcazbWRbi5F2SSZ5J"
oauth = BoxDeveloperTokenAuth(token=access_token)
box = Box(auth=oauth):
my_folder_id = box.create_folder("My Folder")
box.create_folder(path="My Folder/My Subfolder")
box_file = box.upload_table(table=my_parsons_table,
path="My Folder/My Subfolder/My Parsons Table")
sub_folder_id = box.create_folder_by_id(
folder_name="My SubFolder",
parent_folder_id=my_folder_id,
)
box_file = box.upload_table_to_folder_id(
table=my_parsons_table,
file_name="My Parsons Table",
folder_id=sub_folder_id,
)
default_folder_list = box.list()
subfolder_list = box.list("My Folder/My Subfolder")
subfolder_file_list = box.list(
path="My Folder/My Subfolder",
item_type="file",
)
subfolder_file_list = box.list_files_by_id(folder_id="533944")
subfolder_folder_list = box.list_folders_by_id(folder_id="533944")
all_items = box.list_items_by_id(folder_id="533944")
uploaded_table = box.upload_table(my_data_table, path="My Folder/My Subfolder/My Parsons Table", format="csv")
uploaded_table = box.upload_table(my_data_table, path="My Folder/My Subfolder/My Parsons Table", format="json")
downloaded_table = box.get_table(path="My Folder/My Subfolder/My Parsons Table", format="csv")
downloaded_table = box.get_table_by_file_id(file_id=box_file_id, format="csv")
uploaded_file = box.upload_file(my_file, path="My Folder/My Subfolder/My File")
downloaded_file = box.download_file("My Folder/My Subfolder/My File")
downloaded_file = box.download_file("My Folder/My Subfolder/My File", local_path="my_file.dat")
file_id = box.get_item_id(path="My Folder/My Subfolder/My Parsons Table")
box.delete_file(path="My Folder/My Subfolder/My Parsons Table")
box.delete_file_by_id(file_id=file_id)
box.delete_folder(path="My Folder/My Subfolder")
box.delete_folder_by_id(folder_id=subfolder_id)
API¶
- class parsons.box.box.Box(auth: Authentication | None = None)[source]¶
Box is a file storage provider.
- Parameters:
auth (Authentication | None) – Authentication Box Authentication – usually constructed as an access token of 32 characters, alphanumeric. Note that this is only valid for developer use only, and should not be used when creating and maintaining access for typical users. If not passed, will attempt to grab an access token from the environment variable named “BOX_ACCESS_TOKEN”.
- Returns:
Box class.
- Return type:
NOTE: All path-based methods in this class use an intermediate method that looks up the relevant folder/file id by successive API calls. This can be slow for long paths or intermediate folders that contain many items. If performance is an issue, please use the corresponding folder_id/file_id methods for each function.
- create_folder_by_id(folder_name: str, parent_folder_id: str = '0') str[source]¶
Create a Box folder.
- delete_folder(path: str) None[source]¶
Delete a Box folder.
- Parameters:
folder_id – str Box path str to the folder to delete. Any back slashes will be treated as forward slashes.
path (str)
- Return type:
None
- delete_folder_by_id(folder_id: str) None[source]¶
Delete a Box folder.
- Parameters:
folder_id (str) – str The Box id of the folder to delete.
- Return type:
None
- delete_file(path: str) None[source]¶
Delete a Box file.
- Parameters:
path (str) – str Box path str to the file to delete. Any back slashes will be treated as forward slashes.
- Return type:
None
- delete_file_by_id(file_id: str) None[source]¶
Delete a Box file.
- Parameters:
file_id (str) – str The Box id of the file to delete.
- Return type:
None
- list(path: str | None = None, item_type: Literal['file', 'folder'] | None = None) Table[source]¶
Return a Table of Box files and/or folders found at a path.
- Parameters:
path (str | None) – str If specified, Box path str of the folder to be listed. If omitted, the default folder will be used.
item_type (Literal['file', 'folder'] | None) – str Optionally which type of items should be returned, typically either file or folder. If omitted, all items will be returned. Any back slashes will be treated as forward slashes.
- Returns:
A Parsons table of items in the folder and their attributes.
- Return type:
- list_items_by_id(folder_id: str = '0', item_type: Literal['file', 'folder'] | None = None) Table[source]¶
Return a Table of Box files and/or folders found in a folder.
- Parameters:
- Returns:
A Parsons table of items in the folder and their attributes.
- Return type:
- upload_table(table: Table, path: str, format: Literal['csv', 'json'] = 'csv') str[source]¶
Save the passed table to Box.
- Parameters:
- Returns:
The uploaded Box File object’s id.
- Return type:
- upload_table_to_folder_id(table: Table, file_name: str, folder_id: str = '0', format: Literal['csv', 'json'] = 'csv') str[source]¶
Save the passed table to Box.
- Parameters:
table (Table) – Table The Parsons table to be saved.
file_name (str) – str The filename under which it should be saved in Box.
folder_id (str) – str Optionally, the id of the subfolder in which it should be saved. If omitted, the default folder will be used.
format (Literal['csv', 'json']) – str For now, only ‘csv’ and ‘json’; format in which to save table. Defaults to ‘csv’.
- Returns:
The uploaded Box File object’s id.
- Return type:
- Raises:
ValueError – Value for format argument not allowed.
SystemError – Value for format argument not allowed.
- upload_file(file: Path, path: str | None = None) str[source]¶
Save the passed file to Box.
- Parameters:
- Returns:
The uploaded Box File object’s id.
- Return type:
- upload_file_to_folder_id(file: Path, file_name: str | None = None, folder_id: str = '0') str[source]¶
Save the passed file to Box.
- Parameters:
- Returns:
The uploaded Box File object’s id.
- Raises:
SystemError – Invalid response from box upload.
- Return type:
- download_file(path: str, local_path: Path | None = None) Path[source]¶
Download a Box object to a local file.
- Parameters:
path (str) – The Box path str. Any back slashes will be treated as forward slashes.
local_path (Path | None) – The local path where the file will be downloaded. If not specified, a temporary file will be created and returned, and that file will be removed automatically when the script is done running.
- Returns:
The Path of the new file.
- Return type:
- get_table(path: str, format: Literal['csv', 'json'] = 'csv') Table[source]¶
Get a table that has been saved to Box in csv or JSON format.
- Parameters:
- Returns:
A Parsons Table.
- Return type:
- get_table_by_file_id(file_id: str, format: Literal['csv', 'json'] = 'csv') Table[source]¶
Get a table that has been saved to Box in csv or JSON format.
- Parameters:
- Returns:
A Parsons Table.
- Return type:
- Raises:
ValueError – Value for format argument not allowed.
SystemError – Value for format argument not allowed.
- get_item_id(path: str, base_folder_id: str = '0') str[source]¶
Given a Box path str, try to return the id for the file or folder at the end of the str.
NOTE: This method makes one API call for each level in path, so can be slow for long paths or intermediate folders containing very many items.
- Parameters:
- Returns:
A Box File object’s id.
- Return type:
- Raises:
ValueError – Trailing slash or file / folder not found.