Skip to content

Instantly share code, notes, and snippets.

@hkulekci
Last active December 28, 2023 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hkulekci/f9055f4ec531de201b88967cba9b5112 to your computer and use it in GitHub Desktop.
Save hkulekci/f9055f4ec531de201b88967cba9b5112 to your computer and use it in GitHub Desktop.
Download Folder Recursively for Box Cloud

Usage

Create an app through developer platform :

https://<your-account>.app.box.com/developers/console

Then later, get the service account email from the general settings :

AutomationUser_0000000_xxxxxxx@boxdevedition.com

Share the folders with this service account. And run the script as :

mkdir backup
python download.py <folder-id>

Some commands for NextCloud

After downloading the files from Box you can insert into NextCloud through command line :

# invalid character problems
# Entry <>  will not be accessible due to incompatible encoding
convmv -f utf-8 -t utf-8 -r --notest --nfc <folder>

# Scan for next cloud 
cd /var/www/nextcloud
sudo -u www-data php occ files:scan <username>
import os
from boxsdk import Client, OAuth2
import sys
auth = OAuth2(
client_id='client-id',
client_secret='',
access_token='primary-access-token'
)
client = Client(auth)
service_account = client.user().get()
print(f'Service Account user ID is {service_account.id}')
print(service_account)
folder = client.folder(sys.argv[1])
print(folder.get().name)
def recursively_download(folder, path=None):
os.makedirs(path, exist_ok=True)
for item in folder.get_items():
print(f"{item.name} - {item.type}")
if item.type == 'folder':
recursively_download(item, f"{path}/{item.name}")
elif item.type == 'file':
output_file = open(f"{path}/{item.name}", 'wb')
item.download_to(output_file)
recursively_download(folder, f"backup/{folder.get().name}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment