DJANGO-使用Progress Bar上传到云(Azure Blob存储)到云

发布于 2025-02-10 01:56:58 字数 1112 浏览 4 评论 0原文

我正在关注 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱给你人给你 2025-02-17 01:56:58

当一个人将文件上传到特定位置时,为了跟踪上传的当前状态,要么在Python对象周围添加包装器,也可以在上传的地方添加包装器,以提供用于监视的回调。

由于Azure库无法提供该回调,因此可以为对象创建包装器或使用已有的包装器。

Alastair McCormack 命名 tqdm 用包装器可以使用。

AS George John Shows ,一个人可以做这样的事情

size = os.stat(fname).st_size
with tqdm.wrapattr(open(fname, 'rb'), "read", total=size) as data:
    blob_client.upload_blob(data)

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

size = os.stat(fname).st_size
with tqdm.wrapattr(open(fname, 'rb'), "read", total=size) as data:
    blob_client.upload_blob(data)
扛刀软妹 2025-02-17 01:56:58

我可以建议尝试本地存储文件的工作,然后上传到Azure。

不确定它是否有效,但至少您可以尝试一下,并确定是否有帮助:

class UploadFile(models.Model):
    title = models.CharField(max_length=50)
    file = models.FileField(upload_to='files/media/pre', null=True, blank=False)
    remote_file = models.FileField(storage=AMS, null=True, blank=True, default=None)

    def save(self, *args, **kwargs):
        if self.file:
            self.remote_file = self.file
            super().save(*args, **kwargs)  # in theory - this should trigger upload of remote_file
            self.file = None 
        super().save(*args, **kwargs)
        

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:

class UploadFile(models.Model):
    title = models.CharField(max_length=50)
    file = models.FileField(upload_to='files/media/pre', null=True, blank=False)
    remote_file = models.FileField(storage=AMS, null=True, blank=True, default=None)

    def save(self, *args, **kwargs):
        if self.file:
            self.remote_file = self.file
            super().save(*args, **kwargs)  # in theory - this should trigger upload of remote_file
            self.file = None 
        super().save(*args, **kwargs)
        
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文