I suggest you using Azure Storage System
in Django.
Please follow this tutorial to configure your azure storage account in your project.
# Replace <...> appropriately with your information
# AzureStorage Settings
AZURE_STORAGE_ACCOUNT = "<account_name>"
AZURE_STORAGE_KEY = "<account_key>"
AZURE_STORAGE_CONTAINER = "<default_storage_container>" # statics will use this container
# Static Settings
STATICFILES_STORAGE = "<my_project>.storage.AzureStorage"
STATIC_URL = "http://<storage account>.blob.core.windows.net/<default_storage_container>/"
# Media Settings
MEDIA_URL = 'http://storage.pepperdeck.com/<media_container>/'
You could get more details here and here.
Update Answer:
Actually,The Django-Azure-Storage
I provided yesterday is essentially a adapter call for azure storage SDK
. The media container
you mentioned in your reply is actually don't need to be configured , because you only refer to azure storage.
According to you needs , just use Azure Storage Python SDK.
Please follow the steps below.
Step1: Bind the name of your blob which you want to download to your hyperlink
, and pass the blob name
as a parameter to the backend when the user clicks.
Step2: Get blob url.
def GetBlobUrl():
blobService = BlockBlobService(account_name=accountName, account_key=accountKey)
sas_token = blobService.generate_container_shared_access_signature(containerName,ContainerPermissions.READ, datetime.utcnow() + timedelta(hours=1))
# print url
return 'https://' + <your_account_name> + '.blob.core.windows.net/' + <your_container_name> + '/<your_blob_name>?' + sas_token
Step3: Download a file in a browser via StreamingHttpResponse
.
import requests
from django.http import StreamingHttpResponse
def stream_file(request, *args, **kwargs):
file_url = "<blob url you get in the Previous step >"
r = requests.get(file_url, stream=True)
resp = StreamingHttpResponse(streaming_content=r)
resp['Content-Disposition'] = 'attachment; filename="<your blob name>"'
You could also refer to the threads below:
1.Stream file from remote url to Django view response
2.how to stream file to client in django
Hope it helps you.