我建议你在 Django 中使用Azure Storage System。
请按照此tutorial 在您的项目中配置您的 Azure 存储帐户。
# 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>/'
您可以通过here 和here 获取更多详细信息。
更新答案:
实际上,我昨天提供的 Django-Azure-Storage 本质上是对 azure storage SDK 的适配器调用。您在回复中提到的media container实际上是不需要配置的,因为您只是指Azure Storage。
根据你的需要,使用 Azure Storage Python SDK 即可。
请按照以下步骤操作。
Step1:将要下载的blob名称绑定到hyperlink,并将blob name作为参数传递给后端用户点击。
Step2:获取 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:在浏览器中通过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>"'
您也可以参考以下主题:
1.Stream file from remote url to Django view response
2.how to stream file to client in django
希望对你有帮助。