Box

Class that allows for access to the box API and methods to directly upload files. If you wish to use the Box API you should view the install.

Authentication

In order for the Box API to work, it needs to get a user specific access and refresh token. Box provides access tokens to users which are a session key. They remain active for one hour at which time they must be refreshed using the refresh token. Once a new access and refresh token has been given, the old one will no longer work.

The tokens can be provided in three ways. First, you can run Box(...).connect() which will start a flask webserver. You can then proceed to 127.0.0.1:5000 and log in with your Box username and password. This is done securely through Box and you username and password cannot be extracted. Second, you can insert the access and refresh token in the config file. Then the Box class will use these tokens. The final way to provide the tokens is by directly passign them to Box(...).connect(access_token=<your access token>, refresh_token=<your refresh token>).

On a server, where you have no access to a web browser, you will need to get the tokens using a computer which has a web browser. You can then place those tokens in the config file or directly pass them to the connect() method.

Config

The best way to use Box is with a configuration file. Box attributes can be added to the config file as a JSON object which is the value for the key box. An sample config file for box is shown bellow.

{
    "box" : {
        "client_id" : "xxx",
        "client_secret" : "xxx",
        "access_token" : "xxx",
        "refresh_token" : "xxx",
        "refresh_time" : "xxx"
    }
}
class dapt.storage.box.Box(*args, **kwargs)

Bases: dapt.storage.base.Storage

Class which allows for connection to box API. You must either provide a Config object or client_id and client_secret.

Keyword Arguments:
 
  • config (Config) – A Config object which contains the client_id and client_secret.
  • client_id (str) – The Box client ID.
  • client_secret (str) – The Box client secret.
connect(access_token=None, refresh_token=None)

Tries to connect to box using arguments provided in Config and starts server for authorization if not.

Parameters:
  • access_token (str) – Optional argument that allows DAPT to connect to box without going through web authentication (assuming refresh_token is given and not expired).
  • refresh_token (str) – Optional argument that allows DAPT to connect to box without going through web authentication (assuming access_token is given and not expired).
Returns:

Box client if successful

delete_file(file_id)

Delete the the given file.

Parameters:file_id (str) – The file identification to be downloaded
Returns:True if successful and False otherwise
delete_folder(folder_id)

Delete the given folder.

Parameters:folder_id (str) – The folder identification to be downloaded
Returns:True if successful and False otherwise
download_file(file_id, path='.', overwrite=True)

Download the file at the given file_id to the given path.

Parameters:
  • file_id (str) – The file identification to be downloaded
  • path (str) – The path where the file should be saved
  • overwrite (bool) – Should the data on your machine be overwritten. True by default.
Returns:

True if successful and False otherwise

download_folder(folder_id, path='.', overwrite=True)

Download the folder at the given file_id to the given path.

Parameters:
  • folder_id (str) – The folder identification to be downloaded
  • path (str) – The path where the file should be saved
  • overwrite (bool) – Should the data on your machine be overwritten. True by default.
Returns:

True if successful and False otherwise

rename_file(file_id, name)

Rename the given file.

Parameters:
  • file_id (str) – The file identification to be downloaded
  • name (str) – The new name of the file or folder
Returns:

True if the file or folder was renamed, False otherwise.

rename_folder(folder_id, name)

Rename the given folder.

Parameters:
  • folder_id (str) – The folder identification to be downloaded
  • name (str) – The new name of the file or folder
Returns:

True if the file or folder was renamed, False otherwise.

update_tokens(access_token)

Refresh the access and refresh token given a valid access token

Parameters:access_token (string) – box access token to be refreshed
Returns:Box client
upload_file(folder_id, path, name=None, overwrite=True)

Upload a file to the given folder.

Parameters:
  • folder_id (str) – The folder identification to be downloaded
  • path (str) – The path to the file or folder to be uploaded
  • name (str) – The name the file or folder should be saved with. If None then the leaf of the path is used as the name.
  • overwrite (bool) – Should the data on your machine be overwritten. True by default.
Returns:

True if the upload was successful and False otherwise.

upload_folder(folder_id, path, name=None, overwrite=True)

Upload a folder to the given folder.

Parameters:
  • folder_id (str) – The folder identification to be downloaded
  • path (str) – The path to the file or folder to be uploaded
  • name (str) – The name the file or folder should be saved with. If None then the leaf of the path is used as the name.
  • overwrite (bool) – Should the data on your machine be overwritten. True by default.
Returns:

True if the upload was successful and False otherwise.