Reputation: 41
I need to upload a file to a folder inside box. I chose python to implement this logic at server side.
But I find that box-sdk python doesn't supports to check if a folder exists with its name? Is there any way to check if a folder exists just with its name rather than Id ?
Upvotes: 0
Views: 658
Reputation: 36
There in fact a way to search by exact name, however sometimes it could be an access issue. consider this python script:
from boxsdk import JWTAuth, Client
class CFG:
"""config class"""
JWT_CONFIG_FILE = ".jwt.config.json"
AS_USER = "18622116055"
PARENT_FOLDER_ID = "0" # folder id 0 is root folder
def get_box_client(as_user: bool = False):
"""get a box client"""
auth = JWTAuth.from_settings_file(CFG.JWT_CONFIG_FILE)
service_client = Client(auth)
if not as_user:
return service_client
user = service_client.user(CFG.AS_USER)
return service_client.as_user(user)
def print_items(items):
"""print items"""
print("\n")
print("Type\tID\tName")
print("----\t--\t----")
for item in items:
print(f"{item.type}\t{item.id}\t{item.name}\t")
def main():
"""main function"""
client = get_box_client(as_user=True)
# print current user info
user = client.user().get()
print(f"Current User: {user.name}\tid:{user.id}")
# this will check if the user has access to the folder
# and can see its content
items = client.folder(CFG.PARENT_FOLDER_ID).get_items()
print_items(items)
if __name__ == "__main__":
main()
print("\n")
print("-" * 80)
print("All Done!")
Resulting in:
Type ID Name
---- -- ----
folder 172759373899 Barduino User Folder
folder 172599089223 Bookings
folder 162833533610 Box Reports
folder 163422716106 Box UI Elements Demo
folder 191176042455 Bulk Upload
folder 189803765719 ClassificationService
folder 185583279315 Internal Only Folder
folder 184121760895 [email protected] - Managed User A Files and Folders
folder 184901009434 malware-test-files
folder 191494027812 Media Samples
folder 156592455267 My Box Notes
folder 157064745449 My Sign Requests
folder 157065079893 My Signed Documents
folder 165803865043 Preview Samples
folder 172796453399 Shared Folder Public
folder 172797684789 Shared Folder Test
folder 172800574368 Shared with JWT
folder 169427162522 UI Elements Demo
file 1010742636771 This is a box note.boxnote
web_link 22625801630 Shared Folder - GBP Order Forms
This establishes that the security context of the script impersonating a user, can list the contents of the root folder and it has an assortment of files, folder and even a web link.
Now let's search the string 'Box' and return only folders:
# Find the string Box and return only folders
query = "Box"
items = client.search().query(query, type="folder")
print_items(items)
Resulting in:
Type ID Name
---- -- ----
folder 163422716106 Box UI Elements Demo
folder 162833533610 Box Reports
folder 156592455267 My Box Notes
folder 163436720758 Uploads
folder 168248338385 test
folder 170836397950 Box-Dive-Waiver.pdf 2022-08-29 11.51.08
folder 170839228830 Box-Dive-Waiver (3).pdf 2022-08-29 12.11.44
folder 170839433786 Box-Dive-Waiver (4).pdf 2022-08-29 12.15.50
folder 157065079893 My Signed Documents
folder 172959716319 Test
folder 157540513307 BoxAPISingDemoDoc-1646332428797 (1).pdf 2022-03-03 12.19.02
folder 157543073537 BoxAPISingDemoDoc (1).pdf 2022-03-03 12.37.07
folder 157548515146 BoxAPISingDemoDoc (2).pdf 2022-03-03 14.11.24
folder 170836837536 Waiver_Template (6).pdf 2022-08-29 11.36.55
folder 170837067919 Waiver_Template (1).pdf 2022-08-29 11.36.11
folder 170835809391 Waiver_Template (3).pdf 2022-08-29 11.36.26
folder 170835510039 Waiver (2).pdf 2022-08-29 11.35.48
folder 170835829493 Waiver_Template (5).pdf 2022-08-29 11.36.46
folder 170835528770 Waiver_Template (4).pdf 2022-08-29 11.36.37
folder 170836924218 Waiver_Template.pdf 2022-08-29 11.36.02
folder 170835861857 Waiver_Template (2).pdf 2022-08-29 11.36.18
folder 170824922599 Waiver (1).pdf 2022-08-29 08.25.56
folder 169879976942 Single Page.pdf 2022-08-16 14.28.21
At least we only got folders this time...
So what is going on here?
Box search looks for the string in many places, including, name, description, tags, and inside the content it self.
Note that it found folders that are not direct descendants of the root folder, since we did not specify an ancestor folder. However it can be done, and might be useful for your use case. Just specify the ancestor_folders="a_folder_id" as a query argument.
Let's limit the search of the string only to name:
# Find the string Box only in name field
query = "Box"
items = client.search().query(
query,
type="folder",
content_types=("name",), # funky iterable str
)
Resulting in:
Type ID Name
---- -- ----
folder 163422716106 Box UI Elements Demo
folder 162833533610 Box Reports
folder 156592455267 My Box Notes
folder 170836397950 Box-Dive-Waiver.pdf 2022-08-29 11.51.08
folder 170839228830 Box-Dive-Waiver (3).pdf 2022-08-29 12.11.44
folder 170839433786 Box-Dive-Waiver (4).pdf 2022-08-29 12.15.50
folder 157540513307 BoxAPISingDemoDoc-1646332428797 (1).pdf 2022-03-03 12.19.02
folder 157543073537 BoxAPISingDemoDoc (1).pdf 2022-03-03 12.37.07
folder 157548515146 BoxAPISingDemoDoc (2).pdf 2022-03-03 14.11.24
As you can see these have the string Box in the name. Finally we can request an exact match for the string in the name:
# Find the exact match only in name field
query = '"My Box Notes"'
items = client.search().query(
query,
type="folder",
content_types=("name",),
)
Final result:
Type ID Name
---- -- ----
folder 156592455267 My Box Notes
Of course if in the example if 'My Box Notes' existed in another folder level it would show up multiple times.
Upvotes: 0