Prachit Patil
Prachit Patil

Reputation: 461

How to upload file to box using python

I am new to python and I want to know if we can upload files from our local system to box.com? Or else can we take help from any mediator like Jenkins to upload this files?

Upvotes: 3

Views: 7898

Answers (2)

Lijo Abraham
Lijo Abraham

Reputation: 881

You can use the below boxsdk library code.

def upload_file_to_box(client, folder_id, filename):
        folder = client.folder(folder_id=folder_id)
        items = folder.get_items()
        for item in items:
            if item.name == filename:
                updated_file = client.file(item.id).update_contents(item.name)
                print('File "{0}" has been updated'.format(updated_file.name))
                return
        uploaded_file = folder.upload(filename)
        print('File "{0}" has been uploaded'.format(uploaded_file.name))

This will check for a specific file name and compare it with all files names in the folder and updates a new version if exists, otherwise uploads a new file.

Also you can search the filename inside a folder using search API by using the below code. But the search API has a time lag of 10 minutes or greater.

items = client.search().query(query='"{}"'.format(filename), limit=100, ancestor_folders=[folder])
    

Upvotes: 2

Jonathan R
Jonathan R

Reputation: 3928

I don't know if I understood your question correctly, but there is a package for python to connect to the box platform through an API http://opensource.box.com/box-python-sdk/tutorials/intro.html

Upvotes: 1

Related Questions