Skip to main content


Got an interesting question from a developer:





I have been provided a box folder from another user for me to upload files. I have a lot of files to upload to this folder. I created a personal account so I can get access. The files are on a remote Linux box. Is there a convenient way to bulk upload these files from the command-line? I’ve tried different ways (SFTP, generating access token, etc) but having issues with one restriction or another. I posted to your community support page but haven’t heard back.



What’s the best way to bulk-upload from Linux to Box?

Have you tried the box-cli?



For a remote linux box you need to use JWT authentication, take a look here.



Walkthrough:



❯ box -v

@box/cli/3.7.0 linux-x64 node-v14.21.2



Get your config.json from creating a JWT app. (save it on the remote server, be conscious of security)



Configure your box-cli:



❯ box configure:environments:add -n your-app-name ./.config.json

Successfully added CLI environment "your-app-name"



Switch to the configuration:



❯ box configure:environments:set-current

? Which environment?

uie-sample

❯ your-app



Check connection and user context:



❯ box users:get --fields=id,name

Type: user

ID: '20344589936'

Name: UI-Elements-Sample



I have create a “Bulk Upload” folder in the root, so lets get that folder id:



❯ box folders:items 0 --csv

type,id,sequence_id,etag,name

folder,163422716106,0,0,Box UI Elements Demo

folder,189803765719,2,2,ClassificationService

folder,177388203339,0,0,100k

folder,172599089223,0,0,Bookings

folder,172611202270,0,0,My Signed Documents

folder,170845975022,1,1,Waivers

folder,176837925976,0,0,Webhook



As you can see it is not there… This is because I created the folder on my person user (Rui) and the CLI is using the service account associated with the app and the JWT.



So we need to use my person user.



To get a list of users do:



❯ box users --fields=id,name --csv

type,id,name

user,18662105676,Administrator

user,18662356345,Administrator

user,18661971368,Administrator

user,22240548078,Investment User

user,22240405099,Wealth User

user,22240545678,Wholesale User

user,**18622116055**,**Rui** Barbosa



So if I wanted my JWT app to impersonate me I would use my id on the --as-user



❯ box users:get --as-user=18622116055 --fields=id,name --csv

type,id,name

user,18622116055,Rui Barbosa



And now I can list the folders of Rui:



❯ box folders:items 0 --csv --as-user=18622116055

type,id,sequence_id,etag,name,file_version.type,file_version.id,file_version.sha1,sha1

folder,163422716106,0,0,Box UI Elements Demo,,,,

folder,172599089223,0,0,Bookings,,,,

folder,172759373899,0,0,Barduino User Folder,,,,

**folder,191176042455,0,0,Bulk Upload,,,,**

folder,189803765719,2,2,ClassificationService,,,,



Now we can use the destination folder if directly.



I also have a folder with the files to upload:



❯ l files_to_upload

total 0

-rw-rw-r-- 1 lab lab 0 Jan 20 21:10 file_a.txt

-rw-rw-r-- 1 lab lab 0 Jan 20 21:10 file_b.txt

-rw-rw-r-- 1 lab lab 0 Jan 20 21:10 file_c.txt



Now we need to upload the files to the folder, in my case: 191176042455



We have several options depending if you are dealing with a large list of files and their location.



To upload an entire folder:



❯ box folders:upload --parent-folder 191176042455 --as-user 18622116055 files_to_upload --csv --fields type,id,name

type,id,name

folder,191177421988,files_to_upload



To upload a single file:



❯ box files:upload --parent-id 191176042455 --as-user 18622116055 files_to_upload/file_a.txt --csv --fields type,id,name

type,id,name

file,1119062117269,file_a.txt



Of course you can play with bash or use a CSV as an input file with a list of paths.



Let us know if this helped.



The end result:



Reply