【问题标题】:Operation not permitted error when uploading a file DJango上传文件时操作不允许错误 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,但我们没有使用存储帐户,只是使用卷挂载,并且无论如何也失败了。

希望有人能帮帮我

【问题讨论】:

    标签: python django azure azure-storage persistent-volumes


    【解决方案1】:

    谢谢Elliot CastilloPamela 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 storagedjango file upload is not working in azure blob with new version

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      • 2019-05-03
      • 2013-08-15
      • 2017-09-21
      • 1970-01-01
      相关资源
      最近更新 更多