0

I have a API based on Django REST working properly over Azure Web Service but it fails when trying to upload a file into the container's mount volume. I´m doing the upload process by a POST request and in my local environment it's works with no trouble.

The model I used to upload the file to the DB is this:

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', ]

The weird thing is that the file actualy loads up and I can access to it over the volume as you can see here but still failing when it runs the save() method when creating the DB record with the serializer returning the next error:

[Errno 1] Operation not permitted: '/app/storage/public/reports/report_UhrYWD6.xlsb'

I have tried installing django-storages for azure, but we are not using a Storage Account, just the volume mount, and also failed anyway.

I hope someone could help me

Fertex
  • 3
  • 1

1 Answers1

0

Thank you Elliot Castillo and Pamela Peng. Posting your suggestions as an answer to help other community members.

Make changes in your settings.py as per below:

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'

You can also try using 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)

You can refer to Django Azure upload file to blob storage and django file upload is not working in azure blob with new version

DeepDave-MT
  • 1,861
  • 1
  • 5
  • 18