Generate OneDrive Direct-Download Link with C# or Python | by Joe T. Santhanavanich | Towards Data Science

In many projects, you may need file hosting services to share a large number of datasets, scripts, or any files with the direct download option. You may already use the most popular hosting services such as Google Drive, Microsoft OneDrive, DropBox, and iCloud. In this article, I will focus on how to make a direct download link from the OneDrive.com.
The direct download would let you flexibly use the file in any other software. For example, if you host the excel or csv table dataset with a direct download link, the receiver can directly load it to the Pandas dataframe in Python.
I guess I do not have to introduce so much about the OneDrive. It offers 5 GB of storage space free of charge. That would be enough for any big dataset. If you have a really big dataset to share, please consider the database instead. Most of you even have Microsoft Office 365 which includes online space of 1 TB. Also, it is easy to use, share, edit online.
You just need to go to OneDrive, drag and drop to upload any file there, click on the “share” button on the top of the page and click the copy link. That’s it!
For example, I share the COVID-19 time-series dataset from CSSE as an excel table through the OneDrive. And when you access this dataset through the link you created, the browser will automatically open Excel Online for you to explore the dataset in table format. How convenient!
But, unfortunately, it needs several clicks or redirects if your receiver wants to download the dataset. So, it is better for you to share a direct download link if your purpose is to let your receiver download the file.
According to the OneDrive API docs, you can easily generate the OneDrive direct download link with this logic:
First, use base64 to encode the URL.Convert the base64 encoded result to unpadded base64url format by removing = characters from the end of the value, replacing / with _ and + with -.)Append u! to be the beginning of the string.
First, use base64 to encode the URL.
Convert the base64 encoded result to unpadded base64url format by removing = characters from the end of the value, replacing / with _ and + with -.)
Append u! to be the beginning of the string.
To follow the above logic, the OneDrive API docs also provide an example script in C# for you as follows:
string sharingUrl = "Your OneDrive Link here";string base64Value = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(sharingUrl));string encodedUrl = "u!" + base64Value.TrimEnd('=').Replace('/','_').Replace('+','-');
You may try to run this script online in C# with the following link:
rextester.com
After opening the link, you just need to change the sharingUrl to any of your OneDrive links. Wait for some seconds, then you get the output direct OneDrive download link in the output console.
Python is popular in many Data Science projects. So, you might want to generate a direct download link in Python directly before doing the data analysis with the dataset. If someone (or yourself) shares the OneDrive link, you may turn it to a downloadable link with the following Python script:
For example, you can follow the script below to generate the direct download link from our COVID-19 dataset example in excel format. URL: https://1drv.ms/x/s!AmLiprCs46qqhMgFb5pcJisePNNlXw?e=ZgPsGZ then import it to the Pandas dataframe:
import pandas as pd # Input any OneDrive URL onedrive_url = "https://1drv.ms/x/s!AmLiprCs46qqhMgFb5pcJisePNNlXw?e=ZgPsGZ"# Generate Direct Download URL from above Scriptdirect_download_url = create_onedrive_directdownload(onedrive_url)print(direct_download_url)# Load Dataset to the Dataframedf = pd.read_excel(direct_download_url)print(df.head())# Continue with your data analysis ...
This article introduces how to share your files through the OneDrive and turn it to the direct downloadable link with C# or Python. Please, note that you may use apply this logic and rewrite it in any other programming language as well.
I hope you like this article and found it useful for your daily work or projects. Feel free to leave me a message if you have any questions or comments.
About me & Check out all my blog contents: Link
Be Safe and Healthy! 💪
Thank you for Reading. 📚