Upload File to Azure Blob Storage in Django/Python

Vivek Yadav
3 min readFeb 25, 2021

Simple tutorial using Python 3.x and Django

This is a simple article to demonstrate file upload in Django/Python to Azure Blob Storage. This tutorial follows in Python 3.x, so make sure you have Python 3.x installed on your development machine. Let’s get started.

Following are the steps:

  1. Bootstrap a simple Django Project.
  2. Obtain access keys to your Azure Storage.
  3. Install Azure Storage Python module.
  4. Upload the file.

Bonus: Make the file open by browser.

Bootstrap a simple Django project

Let’s first create a simple Django project which we will be using to upload file either via frontend or Postman.

First install Django using following command:

pip install django

Upon successful install, you can bootstrap a new project using following command:

django-admin startproject myuploader

I have the project the name of “myuploader”. You can name it whatever you want.

After creating the project, let’s create an app to handle the uploading of the files.

cd myuploader
python manage.py startapp uploader

This will create a new Django app inside your project. Now, to configure your project to use this app, go to myuploader/settings.py and add “uploader” to the installed apps list.

Now edit the urls.py file inside myuploader directory to include following line of code inside the urlpatterns list.

from django.urls import include, pathurlpatterns = [
path('uploader/', include('uploader.urls')),
]

We now need to add a urls.py file to uploader app. So, create a new file in uploader directory named ‘urls.py’ and add following code to it

from django.urls import include, path
from uploader.views import Uploader
urlpatterns = [
path('upload/', Uploader.as_view()),
]

Obtain access keys to your Azure Storage.

If you haven’t already created a storage account and container, you can do that by following the steps mentioned here:

Install Azure Storage Python module.

Let’s now install azure storage python client.

pip install azure-storage-blob==2.1.0

Upload the file.

Now let’s create an API to handle the file upload.

django.views import View
django.http import JsonResponse
import uuid
from azure.storage.blob import BlockBlobService
class Uploader(View):
def post(self, request):
try:
file = request.FILES['file']
filename = file.name
file_upload_name = str(uuid.uuid4()) + file.name
blob_service_client = BlockBlobService(account_name = 'accountname', account_key='accountkey')
blob_service_client.create_blob_from_bytes( container_name = 'container-name', blob_name = file_upload_name, blob = file.read())
return JsonResponse( { "status": "success", "uploaded_file_name": file_upload_name}, status=201)

Now, you can go to Azure portal and search file with the upload name and you confirm that the file was uploaded successfully.

BONUS:

If you try to open the blob url in browser, the file will start to download. Even if you uploaded an image, at this point, it won’t be previewed in browser. That happens because the browser doesn’t understand the file as we did not set any content type of the file. Setting the content type will tell the browser what type of file is that and browser will try to open it first if it can (it will download the file if not able to open it).

Setting the content type is useful when you are uploading an image, video or PDF document. So, let’s do that.

Edit the above code to adjust for the following code:

from azure.storage.blob.models import ContentSettingsblob_service_client.create_blob_from_bytes( container_name = 'container-name', blob_name = file_upload_name, blob = file.read(), content_settings = ContentSettings(content_type='video/mp4', content_disposition='inline')

So, now we have added content_type as ‘video/mp4’ and content_disposition as ‘inline’. This tells browser that this is the a mp4 video file and try to play it in the browser.

You can learn more about content type at MDN.

For PDF, you would set content_type as ‘application/pdf’. For popular types, you can see the below list:

So, now you can dynamically upload files to Azure Storage with each incoming request.

Hope this helped. Feel free to ask any question or let me know if it helped you. Push that clap button. Thank you for your time.

--

--

Vivek Yadav

Lead Software Engineer, Curious about Space and Future, Changing world one byte at a time