How to easily integrate Dropbox cloud with your Django application.
This article is useful for tech-gigs who want to upload their data on Dropbox cloud via their Django application. Follow the steps sequentially to achieve your goal successfully.
Step 1: Activate your virtual environment and run the following command:
pip install dropbox
Step 2: Register your Dropbox API App by signing up/login here: https://www.dropbox.com/login . Choose Dropbox API app and configure your app’s permission. You’ll need to use the app key created with this app to access API v2. Go to the security section and save localhost and 127.0.0.1 in Chooser / Saver / Embedder domains option if you are running locally.
Step 3: To use the Dropbox API you will also be needing an instance of it so paste the following code in your python file whenever you are going to use Dropbox.
import dropbox dbx = dropbox.Dropbox(‘YOUR_ACCESS_TOKEN’)
Here dbx is your instance of Dropbox object. Paste your access token on right place inside ‘ ’. You can find your access token under security tab by clicking generate access token.
Step 4: For checking whether your connection is right or not try. It will return you the user’s details.
import dropboxdbx = dropbox.Dropbox(‘YOUR_ACCESS_TOKEN’) print(dbx.users_get_current_account())
Step 5: For folder creation use the below code:
import dropboxdbx = dropbox.Dropbox(‘YOUR_ACCESS_TOKEN’)new_folder_name= “_________” #name of your folderdbx.files_create_folder_v2(‘/{}’.format(new_folder_name))
Step 6: For file uploading from a local system use the below code:
import dropboxdbx = dropbox.Dropbox(‘YOUR_ACCESS_TOKEN’)file_from = ‘/local_path/’
file_to = ‘/dropbox_path/’
def upload_file(file_from, file_to):
f = open(file_from, ‘rb’)
dbx.files_upload(f.read(), file_to)response= upload_file(file_from,file_to)
print(response)
Step 7: For file uploading from a remote url use the below code:
import dropboxdbx = dropbox.Dropbox(‘YOUR_ACCESS_TOKEN’)url = '__YOUR SOURCE URL__'path = ‘/dropbox_path'dbx.files_save_url(path, url)