DJANGO-使用Progress Bar上传到云(Azure Blob存储)到云
我正在关注 this 当我使用Ajax在Django上载文件时,添加进度栏的教程。 当我使用upload_to
选项将文件上传到文件夹时,一切正常。 但是,当我使用存储
选项将文件上传到Azure时 - 它不起作用。 即当这是我的模型时:
class UploadFile(models.Model):
title = models.CharField(max_length=50)
file=models.FileField(upload_to='files/media/pre')
它的工作原理是完美的,但是当这是我的模型时:
from myAzure import AzureMediaStorage as AMS
class UploadFile(models.Model):
title = models.CharField(max_length=50)
file = models.FileField(storage=AMS)
它被卡住而不是进步。 (AMS在Morazure.py中定义):
from storages.backends.azure_storage import AzureStorage
class AzureMediaStorage(AzureStorage):
account_name = '<myAccountName>'
account_key = '<myAccountKey>'
azure_container = 'media'
expiration_secs = None
我该如何使其起作用?
编辑: 如果不清楚:
- 我的问题不是上传到Azure,而是要显示进度栏。
- 由于安全原因,我不想从浏览器上传文件,使用CORS和SAS,而是从我的后端上传。
I'm following this tutorial to add a progress bar when I'm uploading a file in Django, using ajax.
When I'm uploading the file to a folder using the upload_to
option everything works fine.
But when I'm uploading the file to Azure using the storage
option - It doesn't work.
i.e. when this is my model:
class UploadFile(models.Model):
title = models.CharField(max_length=50)
file=models.FileField(upload_to='files/media/pre')
It works perfect, but when this is my model:
from myAzure import AzureMediaStorage as AMS
class UploadFile(models.Model):
title = models.CharField(max_length=50)
file = models.FileField(storage=AMS)
It gets stuck and not progressing.
(AMS is defined in myAzure.py by):
from storages.backends.azure_storage import AzureStorage
class AzureMediaStorage(AzureStorage):
account_name = '<myAccountName>'
account_key = '<myAccountKey>'
azure_container = 'media'
expiration_secs = None
How can I make it work?
EDIT:
If it was not clear:
- my problem is not to upload to Azure, but to show progress bar.
- From security reasons I do not want to upload the file from the browser and to use CORS and SAS but from my backend.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当一个人将文件上传到特定位置时,为了跟踪上传的当前状态,要么在Python对象周围添加包装器,也可以在上传的地方添加包装器,以提供用于监视的回调。
由于Azure库无法提供该回调,因此可以为对象创建包装器或使用已有的包装器。
Alastair McCormack 命名 tqdm 用包装器可以使用。
AS George John Shows ,一个人可以做这样的事情
When one is uploading a file to a specific place, in order to track the current state of the upload either one adds a wrapper around the Python object or the place where one is uploading to provides a callback for monitoring.
Since the Azure library doesn't provide that callback, one can create a wrapper for the object or use an already existing one.
There's a library suggested by Alastair McCormack named tqdm with such wrapper that one can use.
As George John shows, one can do something like this
我可以建议尝试本地存储文件的工作,然后上传到Azure。
不确定它是否有效,但至少您可以尝试一下,并确定是否有帮助:
I can suggest to try the work-around of storing file locally and then upload to Azure.
Not sure if it will work but at least you may give it a try and tell if helps: