written by Eric J. Ma on 2023-04-11 | tags: python dropbox file download programming requests library data storage jupyter notebooks portability internet connection content delivery personal cdn data files tutorials linux wget
Today, I cracked the code on downloading files from Dropbox programmatically! 🎉 Using Python and the Requests library, I can now store large data files for my tutorials on Dropbox, making my Jupyter notebooks portable and internet-friendly. 🌐 It's like having my own personal CDN! 💽
Today I learned how to programmatically download a file from a Dropbox public folder. Storing it here so I remember how to do it in the future.
# Download the blog index from Dropbox using Requests. # Taken from: https://stackoverflow.com/a/46005478 import requests headers = {'user-agent': 'Wget/1.16 (linux-gnu)'} # <-- the key is here! r = requests.get("https://www.dropbox.com/s/xad2xf0n7isrhrz/blog_index.json?dl=0", stream=True, headers=headers) with open("/tmp/blog_index.json", 'wb') as f: for chunk in r.iter_content(chunk_size=1024): if chunk: f.write(chunk)
I find it's useful to use Dropbox to host large data files for my Python-based tutorials, allowing me to ensure that my Jupyter notebooks are portable from computer to computer, as long as they can maintain an internet connection.
This is neat because it allows me to use Dropbox, for which I pay for storage, effectively as a personal CDN (content delivery network) with easily referenced URLs.
@article{
ericmjl-2023-how-api,
author = {Eric J. Ma},
title = {How to programmatically download a file from Dropbox using Requests},
year = {2023},
month = {04},
day = {11},
howpublished = {\url{https://ericmjl.github.io}},
journal = {Eric J. Ma's Blog},
url = {https://ericmjl.github.io/blog/2023/4/11/how-to-programmatically-download-a-file-from-dropbox-without-the-dropbox-api},
}
I send out a newsletter with tips and tools for data scientists. Come check it out at Substack.
If you would like to sponsor the coffee that goes into making my posts, please consider GitHub Sponsors!
Finally, I do free 30-minute GenAI strategy calls for teams that are looking to leverage GenAI for maximum impact. Consider booking a call on Calendly if you're interested!