I am new to azure-storage and django My dev environment software configuration is as- django 1.10 , python 2.7.6, azure-storage-blob 0.37. I am using django application as webservice api and frontend is built using angular-2 and HTML
I am using azure blob storage for storing files with any type.
azure blob Container where which I am saving files have private access only.I am able to upload file successfully.
The problem is while downloading file- what I am trying to achieve is-
When someone clicks on hyperlink on page request will go to django view with blob name. then I can get blob using container name and blob name
block_blob_service._get_blob(container,blob_name)
I want to return that blob as downloadable file in django response.
Can you suggest be solution or better approach where I can achieve this.
Thanks in advance.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With