Skip to main content
Question

Python script for uploading files to box using OAuth2

  • May 22, 2025
  • 5 replies
  • 44 views

Forum|alt.badge.img

Hello Box team,

 

Some one please give me python script for uploading files to box ?

I know we have to interact with browser to get initial authentication.

 

I'm looking to script which accepts a folder containing files and upload's them to box location we prefer ?

 

Please help

5 replies

Forum|alt.badge.img

Hi ,

 

Thanks for your post. Have you checked out our developer documentation here?

That may help you get started with uploads and other Box actions. Thanks for clarifying and checking out the link!


Forum|alt.badge.img

 

Helo Howard,

 

Is there any script which does the same, not from searching thru box content.

I need an end to end which accepts folder, and uploads them to box -- I can manage the token refresh for uninterrupted uploads ?

 

Thanks 


Forum|alt.badge.img

 Unfortunately Box's API does not accept a folder as an input. You must create a folder and specify individual files for upload in separate requests. 

 

You can see some excerpts here for uploading an individual file:

https://github.com/box/box-python-sdk/blob/master/docs/usage/files.md#upload-a-file

 

And creating a folder:

https://github.com/box/box-python-sdk/blob/master/docs/usage/folders.md#create-a-folder

 

If you are doing this through a front-end, you may wish to use our Content Uploader Javascript library:

https://developer.box.com/docs/box-content-uploader

 

Hope that helps! Let us know if you have any other questions!

 

Thanks,

Jason


Forum|alt.badge.img

Is there a way to make the upload function force overwrite of existing files? I'm currently getting the following error:

BoxAPIException: Message: Item with the same name already exists
Status: 409
Code: item_name_in_use

Ideally, I would like for the file to be overwritten, and version number of the file incremented.  


Forum|alt.badge.img

I figured it out:

 

try:
    new_file = client.folder(folder_id).upload(local_file_path)
except BoxAPIException as e:
    print('File %s exists. Updating contents.'%local_file_path)
    file_id = e.context_info['conflicts']['id']
    new_file = client.file(file_id).update_contents(local_file_path)