上传文件时操作不允许错误 DJango

Posted

技术标签:

【中文标题】上传文件时操作不允许错误 DJango【英文标题】:Operation not permitted error when uploading a file DJango 【发布时间】:2021-11-04 17:13:16 【问题描述】:

我有一个基于 Django REST 的 API 在 Azure Web 服务上正常工作,但在尝试将文件上传到容器的装载卷时失败。我正在通过 POST 请求执行上传过程,并且在我的本地环境中它可以正常工作。

我用来上传文件到数据库的模型是这样的:

from app.controllers.users.models import User


class QueueJob(models.Model):

    class Tasks(models.TextChoices):
        COLLABORATORS_UPDATE = 'CB_UPDATE'
        WITHDRAWALS_UPDATE = 'WD_UPDATE'
        EXPATS_UPDATE = 'EP_UPDATE'

    class Statuses(models.TextChoices):
        PENDING = 'PENDING'
        PROCESSING = 'PROCESSING'
        DONE = 'DONE'
        FAILED = 'FAILED'

    user = models.ForeignKey(User, on_delete=models.CASCADE)
    task = models.CharField(max_length=20, choices=Tasks.choices, blank=False, null=False)
    file = models.FileField(upload_to=r'reports/', null=False)
    extra_data = models.CharField(max_length=1024, blank=False, null=True)
    execution_comment = models.CharField(max_length=255, blank=False, null=True)
    status = models.CharField(max_length=20, choices=Statuses.choices, null=False, default=Statuses.PENDING)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        verbose_name = 'Queue Job'
        verbose_name_plural = 'Queue Jobs'
        ordering = ['created_at']


class QueueJobSerializer(ModelSerializer):
    class Meta:
        model = QueueJob
        exclude = ['id', ]

奇怪的是文件实际上加载了,我可以通过卷访问它 as you can see here 但在创建数据库记录时运行 save() 方法仍然失败,序列化程序返回下一个错误:

[Errno 1] 不允许操作:'/app/storage/public/reports/report_UhrYWD6.xlsb'

我已经尝试为 azure 安装 django-storages,但我们没有使用存储帐户,只是使用卷挂载,并且无论如何也失败了。

希望有人能帮帮我

【问题讨论】:

【参考方案1】:

谢谢Elliot Castillo 和Pamela Peng。发布您的建议作为答案,以帮助其他社区成员。

在您的settings.py 中进行更改,如下所示:

mysite/settings.py

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, '<directory that houses the static files>/static'),
]

AZURE_ACCOUNT_NAME = '<azure container name>'
AZURE_ACCOUNT_KEY = '<azure account key for this container>'
AZURE_CUSTOM_DOMAIN = f'AZURE_ACCOUNT_NAME.blob.core.windows.net'
AZURE_LOCATION = '<blob container name>'
AZURE_CONTAINER = '<blob container name>'

STATIC_LOCATION = 'static'
STATIC_URL = f'https://AZURE_CUSTOM_DOMAIN/STATIC_LOCATION/'

STATICFILES_STORAGE = 'storages.backends.azure_storage.AzureStorage'
DEFAULT_FILE_STORAGE = 'mysite.custom_azure.AzureMediaStorage'

你也可以试试Azure Storage Blobs client library for Python:

from azure.storage.blob import BlobClient

blob = BlobClient.from_connection_string(conn_str="<connection_string>", container_name="my_container", blob_name="my_blob")

with open("file.csv", "rb") as data:
    blob.upload_blob(data)

您可以参考Django Azure upload file to blob storage和django file upload is not working in azure blob with new version

【讨论】:

以上是关于上传文件时操作不允许错误 DJango的主要内容,如果未能解决你的问题,请参考以下文章

Flask 文件上传返回错误 405 方法不允许

405 上传文件时方法不允许

BlueImp 文件上传“错误:方法不允许” - Laravel 4 路由问题

上传文件时Django的权限被拒绝错误

WordPress不允许上传该类型的文件,请选择其它文件

Django-Photologue:上传 jpg 文件时出现错误 500,但上传 png 文件有效