'Download files from a Box location using API

How to download files from a Box location programmatically?

I have a shared box location URL(Not the exact path of the box location).

I want to download all the files under the location.

I checked below sdk to connect to box but unable to find methods/library to download files from a shared link.

https://github.com/box/box-python-sdk

from boxsdk import Client
from boxsdk import OAuth2

oauth = OAuth2(
    client_id='XXX',
    client_secret='XXX',
    store_tokens='XXX',
)


data = client.make_request(
    'GET',
    '<Shared BOX URL>',
)

Please help



Solution 1:[1]

You can use the method that gives you the direct URL:

download_url = client.file(file_id='SOME_FILE_ID').get_shared_link_download_url()

And then you can use urlib to download it to your local computer:

import urllib
urllib.urlretrieve (download_url , your_local_file_name)

Could it solve your problem?

Solution 2:[2]

Pre-requisite:

oauth = OAuth2(
    client_id = 'strBoxClientID',
    client_secret = 'strBoxClientSecret', 
    access_token = access_token,
    )
client = Client(oauth)

Initial attempt (failed, it produces an empty file):

with open(Box_File.name, 'wb') as open_file:
    client.file(Box_File.id).download_to(open_file)
    open_file.close()

Final solution:

output_file = open('strFilePath' + str(Box_File.name), 'wb')
Box_File.download_to(output_file)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Luc
Solution 2 StudentAtLU