Create a BOX API for upload and download a folder from box

SOLVED
Go to solution
Highlighted
Occasional Contributor

Create a BOX API for upload and download a folder from box

I want to create a BOX API using which I want to connect to BOX in python.

I need to upload and download a files from box.

 

Can someone provide some reference which I can use to do the same.

10 REPLIES
Box Employee

Re: Create a BOX API for upload and download a folder from box

Hello, 

 

Thanks so much for using our platform and development forum! 

 

If you'd like to use python, I'd recommend taking a look at using our python SDK

 

If you choose not to use that, here are the endpoints you would need: 

Best, 

Kourtney 

Occasional Contributor

Re: Create a BOX API for upload and download a folder from box

Can you please let me know which script I need to look at under https://github.com/box/box-python-sdk ?

Occasional Contributor

Re: Create a BOX API for upload and download a folder from box

@ch_srikanth ,

 

i found an example of uploading a file under the demo code on GitHub (https://github.com/box/box-python-sdk/blob/1.5/demo/example.py) 

 

 

def upload_file(client):
    root_folder = client.folder(folder_id='0')
    file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'file.txt')
    a_file = root_folder.upload(file_path, file_name='i-am-a-file.txt')
    try:
        print('{0} uploaded: '.format(a_file.get()['name']))
    finally:
        print('Delete i-am-a-file.txt succeeded: {0}'.format(a_file.delete()))

to download a file as bytes (https://stackoverflow.com/questions/29594289/how-to-download-files-with-box-api-python) :

 

client.file(file_id='SOME_FILE_ID').content()

or download to a file (https://stackoverflow.com/questions/29594289/how-to-download-files-with-box-api-python)

 

with open('FileFromBox.xls', 'wb') as open_file:
    client.file('FileId_of_box_file').download_to(open_file)
    open_file.close()

 

 

 

 

 

Occasional Contributor

Re: Create a BOX API for upload and download a folder from box

@BB_Jake

I am not able to access the github path which you shared.Can you please copy the code here.

(https://github.com/box/box-python-sdk/blob/1.5/demo/example.py)  

Occasional Contributor

Re: Create a BOX API for upload and download a folder from box

 

Hello,

In my BOX Api I added my colleague as a collaborator, My colleague is uploading files in UI to one of her new folder(folder1),

I want to access those files from that folder1 using my python BOX Api code.

And also vice versa, If my BOX API is uploading any files to new folder(folder2), my colleague should be able to view those files in her UI access.

Will I be able to do that ? 

Occasional Contributor

Re: Create a BOX API for upload and download a folder from box

@ch_srikanth

 

any account that is a collaborator on the folders will be able to see the files being uploaded so make sure the account you are using with the python sdk is a collaborator.

The easiest way is to create a folder at the root level and add every account you need as collaborators on that folder and they will be able to see anything that lives somewhere underneath that created folder.

 

I think i messed up the URL to the github link, try this https://github.com/box/box-python-sdk/blob/1.5/demo/example.py

 

Occasional Contributor

Re: Create a BOX API for upload and download a folder from box

@BB_Jake

 

thanks for the example.py script, when I try to add a collaborator to my root folder I am getting the below error.

collaboration = root_folder.add_collaborator('****', CollaborationRole.VIEWER)

TypeError: Collaborator must be User, Group, or unicode string.

 

I am able to create a Sub folder successfully but not able to add my colleague as a Collaborator.

 

May I please know where am I going wrong ?

 

 

Below is my code:

```

from boxsdk import JWTAuth,Client,LoggingClient
import pandas as pd
import io
from io import StringIO
from io import BytesIO
import logging

# Setting logger properties
logger = logging.getLogger("**")

def main():
auth = JWTAuth(
client_id="***",
client_secret="***",
jwt_key_id= "***",
enterprise_id= "**",
rsa_private_key_passphrase="**",
rsa_private_key_file_sys_path='**',
)
logger.info('JWTAuth authentication created..')
access_token = auth.authenticate_instance()
print(access_token)
client = LoggingClient(auth)

# Upload a file to Box!
from StringIO import StringIO
stream = StringIO()

root_folder = client.folder(folder_id='0')
collab_folder = root_folder.create_subfolder('collab folder')

print('Folder {0} created'.format(collab_folder.get()['name']))
collaboration = collab_folder.add_collaborator('***', CollaborationRole.VIEWER)

```

 

 

Occasional Contributor

Re: Create a BOX API for upload and download a folder from box

@ch_srikanth

 

https://github.com/box/box-python-sdk/blob/master/docs/usage/collaboration.md#add-a-collaboration

 

user = client.user(user_id='11111')
collaboration = client.folder(folder_id='22222').collaborate(user, CollaborationRole.VIEWER)

OR

 

email_of_invitee = '***email address removed for privacy***'
collaboration = client.folder(folder_id='22222').collaborate_with_login(email_of_invitee, CollaborationRole.VIEWER)

 

Occasional Contributor

Re: Create a BOX API for upload and download a folder from box

Thanks @BB_Jake for all your response.

Occasional Contributor

Re: Create a BOX API for upload and download a folder from box

Thanks @kourt for your response.