“If we always helped one another, no one would need luck.”
It’s Monday, and that means we’re back with another post aimed at helping you overcome some of the most commonly asked questions about the APIs we work with here at Kloudless. Every Monday, we will aim to help answer some of the more repeated inquiries into the world’s most popular APIs or cloud services. Today, we will be turning back to one of the industry’s top names when it comes to cloud storage: Dropbox.
Boasting more than 500 million users, DropBox has been a bona fide giant of the cloud-based file storage world since its release in 2008. DropBox became the first of the ‘folder sync’ storage applications to become a household name, having been originally conceived when one of its co-founders left his USB flash drive at home and was stuck with no way to access his necessary files for a college class. As a result, DropBox has become ubiquitous with users looking to manage their files and documents in the cloud, and heavily utilized by applications looking to provide their users with storage integrations.
When Dropbox updated its API to Version 2, a lot of changes were put into effect. Many of these changes were reflected in the official Dropbox SDKs, and due to large swaths of functionality changing, a good handful of the associated functions were deprecated. Python — continually one of the most popular programming languages — fell victim to some of these deprecations, and a major ask of some of our readers was, “How the heck do I upload files to Dropbox now?!”
Well, rest assured. It’s still pretty easy. Let’s walk through it!
First, make sure that you’ve installed the Dropbox Python module by entering the following into your terminal:
pip install dropbox
Next, head over to the Dropbox developer portal and create an app here:
Make sure that your app has a data access type of “Files & Datastores,” and set your permission type to “Full Dropbox.” This will ensure that your app has write access, so you won’t run into any errors when uploading files from your script.
Finally, click “generate access token” and copy down the string that is produced. We’ll need that for your script to properly work, and we’ll plug it in below where the script calls for it.
Great, that’s all we need to do on Dropbox’s side, so let’s take a look at the script we’ll be using. We’ve separated the functionality into a Class object and another separate function. Make sure you don’t forget to
import the Dropbox library at the top.
def __init__(self, access_token):
self.access_token = access_token
def upload_file(self, file_from, file_to):
dbx = dropbox.Dropbox(self.access_token)
with open(file_from, 'rb') as f:
access_token = '**************'
transferData = TransferData(access_token)
file_from = 'kloudless_local.txt' # This is name of the file to be uploaded
file_to = '/test_dropbox/kloudless_cloud.txt' # This is the full path to upload the file to, including name that you wish the file to be called once uploaded.
# API v2
if __name__ == '__main__':
That’s all there is to it! You can now use this script to upload files to your Dropbox app with Python!
We’d love to hear from you on any API questions you may want us to answer in the future, so please send any requests to email@example.com.