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 = m
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 = itemi0].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!