Skip to main content

New blog: How to recursively upload a folder to Box using Python

New blog: How to recursively upload a folder to Box using Python
rbarbosa Box

Hi folks,

Check out the new blog article:

When uploading files recursively, we also need to check if the folders and files already exists, because the user may upload an entire folder a second and third time.

Take a look at this example, where we use the prefilght_check to verify if the file can be accepted, and decide if we are uploading a new file or creating a new version of an existing one.

Here is something to whet your appetite:

def main():
    """main app demo"""
    settings = get_settings()

    # check if sample folder exist and create them if not
    sample_folder = check_sample_folders(settings.sample_folder_base_dir)

    # get a client
    service_client = box_client_get(settings.jwt_config_path)

    # get a client as user
    client = box_client_as_user_get(service_client, settings.as_user_id)

    # create a demo upload folder in root if not exists
    item = [
        item
        for item in client.folder("0").get_items()
        if (item.name == settings.default_upload_folder 
            and item.type == "folder")
    ]
    if len(item) == 0:
        demo_folder = client.folder("0").create_subfolder(
            settings.default_upload_folder
        )
    else:
        demo_folder = item[0].get()

    print("Box Python SDK - Upload Folder Demo")
    print("=" * 40)
    print(f" Uploading folder {sample_folder}")
    print("-" * 40)
    folder_upload(client, demo_folder, settings.sample_folder_base_dir)


if __name__ == "__main__":
    main()
    print("=" * 40)
    print("All done")

Enjoy!

Did this topic help you find an answer to your question?

0 replies

Be the first to reply!

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings