box / boxcli

A command line interface for interacting with the Box API.
https://developer.box.com
Apache License 2.0
222 stars 59 forks source link

BOX CLI CMDS #352

Closed chrys6571 closed 2 years ago

chrys6571 commented 2 years ago

I am new to the Box cli and trying to pull some data. We are new to Box and new to the Cli as I mentioned.

I am running this command:

box folders:items (Root Folder ID) --save-to-file-path=C:\test.csv --as-user=(BOX ADMIN ACCT)

Under this Root folder i have a bunch of folder in the following structure

ROOT Folder/Sub- Folder 1/Sub-Folder 2/Sub-Folder 3/Sub-Folder 4/Sub-Folder 5/USER FILES HERE.PDF

How do I get the IDS for all the Sub Folders under the root folder or how do I get the ID for all the Sub-folder 5 that exist in the structure.

There are about 17k Sub folders at level 1 and each one has the same structure listed above. There are about 9 million folders that exist at Sub Folder 5 level.

lukaszsocha2 commented 2 years ago

Hi @chrys6571, the only way to get all items inside the root folder is to use a command you mentioned box folders:items 0 --as-user=USER_ID. There is no easy way to get only folders - you would have to filter them from other items on your own. To get the id of the Sub-folder 5 you have to list items folder by folder starting from the root folder until you got there. Alternatively you can just go to this folder in a browser and copy the folder id from the path.

image

@lukaszsocha2

chrys6571 commented 2 years ago

I would need to do this for 17k folders, that would be extremely time consuming. Darn, anything else you can think of? The only reason I ask is the File and Folders report in the GUI is broken. Hence trying to get this data from CLi.

lukaszsocha2 commented 2 years ago

Hi @chrys6571 , may I ask what is the result you want to achieve? Do you want to simply list all the files in a folder with a given path e.g. ROOT Folder/Sub- Folder 1/Sub-Folder 2/Sub-Folder 3/Sub-Folder 4/Sub-Folder 5? If so then cli is not sufficient for such task. You have to write some custom script - I suggest to use either Python or Node and writing bash script will be much harder. Then you could use Box SDK to build your custom solution. I prepared you really simple Python script that list all items inside the folder with given path:

from boxsdk import OAuth2
from boxsdk.client import Client
from boxsdk.object.folder import Folder

PATH = 'Sub- Folder 1/Sub-Folder 2/Sub-Folder 3/Sub-Folder 4/Sub-Folder 5'
DEVELOPER_TOKEN = 'INESRT YOUR TOKEN HERE'

def get_item_id_by_name_in_folder(folder: Folder, item_name: str):
    for item in folder.get_items():
        if item.name == item_name:
            return item.id
    raise Exception(f"No item found with name {item_name}")

def get_id_by_path(client: Client, path: str):
    subfolders = path.split('/')

    current_dir = client.root_folder()
    for item_name in subfolders:
        item_id = get_item_id_by_name_in_folder(current_dir, item_name)
        current_dir = client.folder(item_id)

    return list(current_dir.get_items())

if __name__ == "__main__":
    auth = OAuth2(access_token=DEVELOPER_TOKEN, client_id='', client_secret='')
    client = Client(auth)

    print(get_id_by_path(client, PATH))

You need to replace PATH and DEVELOPER_TOKEN values. Not sure if above script will be helpful as I don't know what do you want to achieve. Please let me know and then maybe I'll be able to help better.

@lukaszsocha2

chrys6571 commented 2 years ago

First thank you very much for taking the time to write that!

Second I basically need all the folder ID’s for every sub folder at level 5.

ROOT Folder/Sub- Folder 1/Sub-Folder 2/Sub-Folder 3/Sub-Folder 4/Sub-Folder 5

I am trying to set view access only to all folders from the Root Folder through Sub-Folder 4, and give the users edit access only at sub-folder 5 level.

This way the directory structure is maintained, right now users have edit access across the entire structure and they are messing with the structure.

From: Łukasz Socha @.> Sent: Thursday, July 7, 2022 12:12 PM To: box/boxcli @.> Cc: Christian Taveras @.>; Mention @.> Subject: Re: [box/boxcli] BOX CLI CMDS (Issue #352)

Berkowitz Pollack Brant Advisors Warning: Sender @@.***​.com is not yet trusted by your organization. Please be careful before replying or clicking on the URLs. Report Phishinghttps://cloud.graph.us/feedback?msgId=PGJveC9ib3hjbGkvaXNzdWVzLzM1Mi8xMTc3ODcwMjc0QGdpdGh1Yi5jb20%2B&orgDomain=YnBiY3BhLmNvbQ%3D%3D&opt=unsafe Mark Safehttps://cloud.graph.us/feedback?msgId=PGJveC9ib3hjbGkvaXNzdWVzLzM1Mi8xMTc3ODcwMjc0QGdpdGh1Yi5jb20%2B&orgDomain=YnBiY3BhLmNvbQ%3D%3D&opt=safe powered by Graphus®

This message originated outside our environment. Do not click any hyperlinks, respond or open any attached files unless you have validated that the message is legitimate.


Hi @chrys6571https://github.com/chrys6571 , may I ask what is the result you want to achieve? Do you want to simply list all the files in a folder with a given path e.g. ROOT Folder/Sub- Folder 1/Sub-Folder 2/Sub-Folder 3/Sub-Folder 4/Sub-Folder 5? If so then cli is not sufficient for such task. You have to write some custom script - I suggest to use either Python or Node and writing bash script will be much harder. Then you could use Box SDK to build your custom solution. I prepared you really simple Python script that list all items inside the folder with given path:

from boxsdk import OAuth2

from boxsdk.client import Client

from boxsdk.object.folder import Folder

PATH = 'Sub- Folder 1/Sub-Folder 2/Sub-Folder 3/Sub-Folder 4/Sub-Folder 5'

DEVELOPER_TOKEN = 'INESRT YOUR TOKEN HERE'

def get_item_id_by_name_in_folder(folder: Folder, item_name: str):

for item in folder.get_items():

    if item.name<http://item.name> == item_name:

        return item.id<http://item.id>

raise Exception(f"No item found with name {item_name}")

def get_id_by_path(client: Client, path: str):

subfolders = path.split('/')

current_dir = client.root_folder()

for item_name in subfolders:

    item_id = get_item_id_by_name_in_folder(current_dir, item_name)

    current_dir = client.folder(item_id)

return list(current_dir.get_items())

if name == "main":

auth = OAuth2(access_token=DEVELOPER_TOKEN, client_id='', client_secret='')

client = Client(auth)

print(get_id_by_path(client, PATH))

You need to replace PATH and DEVELOPER_TOKEN values. Not sure if above script will be helpful as I don't know what do you want to achieve. Please let me know and then maybe I'll be able to help better.

@lukaszsocha2https://github.com/lukaszsocha2

— Reply to this email directly, view it on GitHubhttps://github.com/box/boxcli/issues/352#issuecomment-1177870274, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AZ6LR4HFHSOYFDUOE62D4P3VS36U5ANCNFSM523G6Y4A. You are receiving this because you were mentioned.Message ID: @.**@.>>

Christian Taveras | Network Administrator Berkowitz Pollack Brant Advisors + CPAs

200 S Biscayne Blvd., 7th Floor, Miami, FL 33131-5310 Direct: 305-960-1245 | Office: 305-379-7000 | Fax: 305-960-9084 Email: @.*** | Web: www.bpbcpa.com | vCardhttps://www.bpbcpa.com/vcards/1604.vcf

Offices in Miami, Ft. Lauderdale, Boca Raton, West Palm Beach and New York City

The information contained in this electronic message is privileged and/or confidential and is intended only for the use of the individual or entity named above. If you are not the intended recipient, or if you are responsible for delivering it to the intended recipient, you are hereby notified that any dissemination, distribution or copying of the communication is not authorized, allowed or intended by the sender. If you have received this communication in error, please immediately notify us by telephone at the above number and forward the original message to the sender above. Thank you.

lukaszsocha2 commented 2 years ago

Hi @chrys6571, I modified the script to list ids of all subfolders it the given path. To do further steps I advice to take a look here: https://github.com/box/box-python-sdk/blob/main/docs/usage/folders.md. There you can find more info about folders usage. Here comes the script:

from boxsdk import OAuth2
from boxsdk.client import Client
from boxsdk.object.folder import Folder

PATH = 'Sub- Folder 1/Sub-Folder 2/Sub-Folder 3/Sub-Folder 4/Sub-Folder 5'
DEVELOPER_TOKEN = 'INSERT_TOKEN_HERE'

def get_item_id_by_name_in_folder(folder: Folder, item_name: str):
    for item in folder.get_items():
        if item.name == item_name:
            return item.id
    raise Exception(f"No item found with name {item_name}")

def get_all_folder_ids_in_path(client: Client, path: str):
    subfolders = path.split('/')

    current_dir = client.root_folder()
    for item_name in subfolders:
        item_id = get_item_id_by_name_in_folder(current_dir, item_name)
        current_dir = client.folder(item_id)

    return [item.id for item in current_dir.get_items() if item.type == 'folder']

if __name__ == "__main__":
    auth = OAuth2(access_token=DEVELOPER_TOKEN, client_id='', client_secret='')
    client = Client(auth)

    list_of_folder_ids = get_all_folder_ids_in_path(client, PATH)
    print(list_of_folder_ids)

It should print ids of all subfolders inside Sub-Folder 5. Best, @lukaszsocha2

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not been updated in the last 30 days. It will be closed if no further activity occurs within the next 7 days. Feel free to reach out or mention Box SDK team member for further help and resources if they are needed.

stale[bot] commented 2 years ago

This issue has been automatically closed due to maximum period of being stale. Thank you for your contribution to Box CLI and feel free to open another PR/issue at any time.