Skip to content

Description:

  • Interact with data in box.com

Resources:

This notebook reads its settings from environment variables. Set them before starting Jupyter:

  • BOX_JWT_CONFIG_PATH: path to the JWT app config JSON downloaded from the Box developer console
import fsspec
from boxsdk import JWTAuth
import os
import fsspec
from boxsdk import JWTAuth
# The JWT app config JSON from the Box developer console holds clientID, clientSecret,
# publicKeyID, privateKey, passphrase and enterpriseID. Keep it outside the repository.
oauth = JWTAuth.from_settings_file(os.environ["BOX_JWT_CONFIG_PATH"])
root_id = "0" # root folder ID

For simple file access, you can use fsspec.open

Section titled “For simple file access, you can use fsspec.open”
with fsspec.open("box://Documents/test_file.txt", "wb", oauth=oauth, root_id=root_id) as f:
f.write(b"This file was produced using boxfs")

For more control, you can use fsspec.filesystem

Section titled “For more control, you can use fsspec.filesystem”
from fsspec import filesystem
fs = fsspec.filesystem('box', oauth=oauth)
# Create new directory
fs.mkdir("Documents")
# # List directory contents
fs.ls('Documents')
# Print file contents
fs.cat('Documents/test_file.txt/test_file_2.txt')
# Note: touch won't work as it hasn't been implemented by boxfs
fs.touch('Documents/test_file.txt')

ganymede is made by Eugo