Skip to content
Related Articles

Related Articles

How to Automate the Storage using Dropbox API in Python ?
  • Last Updated : 21 Apr, 2021

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

  • 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




# 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




# 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.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :