Skip to content

Description:

  • Interact with data in dropbox.com

Resources:

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

  • DROPBOX_ACCESS_TOKEN: a Dropbox OAuth2 access token (see authentication.ipynb)

Get info about a folder using DropboxDriveFileSystem

import os
import dropboxdrivefs as dbx
dropbox_access_token = os.environ["DROPBOX_ACCESS_TOKEN"]
fs = dbx.DropboxDriveFileSystem(token=dropbox_access_token)
fs.info("/example_folder")

Create a folder

fs = dbx.DropboxDriveFileSystem(token=dropbox_access_token)
fs.mkdir("/Data/test_dropbox")

Upload a file

fs = dbx.DropboxDriveFileSystem(token=dropbox_access_token)
from pathlib import Path
tmp_path = "/tmp/ganymede_demo"
p = Path(tmp_path).mkdir(exist_ok=True)
with open(f"{tmp_path}/test1.txt", "w") as f:
f.write("Hello")
fs.put_file(f"{tmp_path}/test1.txt", "/Data/test_dropbox/text1.txt")

Download a file

fs = dbx.DropboxDriveFileSystem(token=dropbox_access_token)
from pathlib import Path
tmp_path = "/tmp/ganymede_demo"
p = Path(tmp_path).mkdir(exist_ok=True)
remote_fp = "/Data/test_dropbox/text1.txt"
local_fp = f"{tmp_path}/testing.txt"
fs.download(remote_fp, local_fp)

List all files before removing the folder online

items = fs.ls("/Data/test_dropbox")
for item in items:
print(item["name"])
print("\nAvailable info for folder type:")
print(list(items[0].keys()))
print("\nAvailable info for file type:")
print(list(items[-1].keys()))

ganymede is made by Eugo