I want to turn off the download function in Jupyter UI in Amazon SageMaker AI.
Resolution
To turn off the download function in SageMaker AI hosted Jupyter notebooks, create a lifecycle configuration script for your target resource type. In the Scripts section, enter one of the following code examples.
SageMaker AI notebook instance:
cat <<END >>/home/ec2-user/.jupyter/handlers.py
from tornado import web
from notebook.base.handlers import IPythonHandler
class ForbidFilesHandler(IPythonHandler):
@web.authenticated
def head(self, path):
self.log.info("HEAD: File download forbidden.")
raise web.HTTPError(403)
@web.authenticated
def get(self, path, include_body=True):
self.log.info("GET: File download forbidden.")
raise web.HTTPError(403)
END
cat <<END >>/home/ec2-user/.jupyter/jupyter_notebook_config.py
import os, sys
sys.path.append('/home/ec2-user/.jupyter/')
import handlers
c.ContentsManager.files_handler_class = 'handlers.ForbidFilesHandler'
c.ContentsManager.files_handler_params = {}
END
reboot
Note: For SageMaker AI notebook instances, enter your script in the Start Notebook editor.
SageMaker AI Studio Classic:
__conda_setup="$('/opt/conda/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/opt/conda/etc/profile.d/conda.sh" ]; then
. "/opt/conda/etc/profile.d/conda.sh"
else
export PATH="/opt/conda/bin:$PATH"
fi
fi
unset __conda_setup
conda activate studio
jupyter labextension disable @jupyterlab/docmanager-extension:download
jupyter labextension disable @jupyterlab/filebrowser-extension:download
restart-jupyter-server
Note: When you add the script, select JupyterServer App.
SageMaker AI Studio JupyterLab space:
jupyter labextension disable @jupyterlab/docmanager-extension:download
jupyter labextension disable @jupyterlab/filebrowser-extension:download
After you create the configuration, attach the lifecycle configuration to your resource. Then, launch your target resource.
Related information
Customization of a SageMaker AI notebook instance using an LCC script