Environment variables¶
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
In [ ]:
Copied!
import fsspec
from boxsdk import JWTAuth
import fsspec
from boxsdk import JWTAuth
Load your credentials¶
In [ ]:
Copied!
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
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¶
In [ ]:
Copied!
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")
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¶
In [ ]:
Copied!
from fsspec import filesystem
fs = fsspec.filesystem('box', oauth=oauth)
# Create new directory
fs.mkdir("Documents")
from fsspec import filesystem
fs = fsspec.filesystem('box', oauth=oauth)
# Create new directory
fs.mkdir("Documents")
In [ ]:
Copied!
# # List directory contents
fs.ls('Documents')
# # List directory contents
fs.ls('Documents')
In [ ]:
Copied!
# Print file contents
fs.cat('Documents/test_file.txt/test_file_2.txt')
# Print file contents
fs.cat('Documents/test_file.txt/test_file_2.txt')
In [ ]:
Copied!
# Note: touch won't work as it hasn't been implemented by boxfs
fs.touch('Documents/test_file.txt')
# Note: touch won't work as it hasn't been implemented by boxfs
fs.touch('Documents/test_file.txt')