How to Automate the Storage using Dropbox API in Python ?
In this Data Era Storing, Managing, and organizing the Data is an important factor considered by every business. Dropbox is one of the most popular cloud storage systems on the market, and it continues to improve its features. In this article, we will be demonstrating how one can connect to dropbox API using Python and perform Storage Automation efficiently.
Get Secret Key to Connect To Your Dropbox
- Go to https://www.dropbox.com/developers/apps/create, You will see a Page like this. Select the “Scoped Access” option and in step 2 select “Full Dropbox”, Name your app with any significant name.
- In the Oauth2 Section, Set Access Token Expiration to “No Expiration“, generate an Access Token.
- Go to Permissions Tab, Select Appropriate Permissions that are required, and click Submit.
- With The Token Key Generated from the Above steps, We can use this token to connect to dropbox and perform storage Automation. You can install the python library using the below command:
pip install dropbox
Demonstration video to generate a token:
Using the token key to connect to Dropbox:
Use the token generated to connect to dropbox and create an object.
- Python3
Python3
# importing necessary libraries import dropbox # Token Generated from dropbox TOKEN = "access_token" # Establish connection def connect_to_dropbox(): try : dbx = dropbox.Dropbox(TOKEN) print ( 'Connected to Dropbox successfully' ) except Exception as e: print ( str (e)) return dbx dbx = connect_to_dropbox() |
Output:
Connected to Dropbox successfully
We have successfully connected to Dropbox. We can now list folders/files, read files, upload files, delete files, etc.
Listing Files in a Folder
First, call the above method to connect and create a dropbox object, then assign a path for the folder to be scanned, then iterate through the older using file_list_folder() method and for a loop.
- Python3
Python3
# explicit function to list files def list_files_in_folder(): # here dbx is an object which is obtained # by connecting to dropbox via token dbx = connect_to_dropbox() try : folder_path = "/folder_path" # dbx object contains all functions that # are required to perform actions with dropbox files = dbx.files_list_folder(folder_path).entries print ( "------------Listing Files in Folder------------ " ) for file in files: # listing print ( file .name) except Exception as e: print ( str (e)) list_files_in_folder() |
Output:
Now we have successfully connected to dropbox using python, Listed the Folders present in it.