Join BoxWorks in San Francisco Nov 12-13! Keynotes, product demos, and Box Master Classes. Reserve your spot!

Python script for uploading files to box using OAuth2

New post

Comments

5 comments

  • Howard

    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!

    0
    Comment actions Permalink
  • bhanuprakashemm

     

    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 

    0
    Comment actions Permalink
  • Jason

     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

    0
    Comment actions Permalink
  • VBK

    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.  

    0
    Comment actions Permalink
  • VBK

    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)

     

    0
    Comment actions Permalink

Please sign in to leave a comment.