Azure Storage - FileNotFoundError: [Errno 2] No such file or directory

I am using django-storage to handle media files on Azure Storage. I am using Docker to deploy an app to Web App for Containers. When I am trying to run my celery task it gives me an error:

File "/code/myapp_settings/celery.py", line 59, in test_task
spec.loader.exec_module(spider_module)
File "<frozen importlib._bootstrap_external>", line 839, in exec_module
File "<frozen importlib._bootstrap_external>", line 975, in get_code
File "<frozen importlib._bootstrap_external>", line 1032, in get_data
FileNotFoundError: [Errno 2] No such file or directory: 'https://myapp.blob.core.windows.net/media/python/test.py'

I checked Azure Storage and file with such name and path exists. I can access it from Azure and from URL normally without any issues. I can also upload and download file from admin panel.

Below is my celery task that I am trying to run:

@shared_task(name="test_task")
def test_task(myapp_id):

    myapp = myapp.objects.get(id=myapp_id)
    python_config_file = myapp.python_config_file

    module_path = os.path.join(MEDIA_URL, f"{python_config_file}")

    spec = importlib.util.spec_from_file_location("myapp", module_path)
    myapp_module = importlib.util.module_from_spec(spec)
    sys.modules["myapp"] = myapp_module
    spec.loader.exec_module(myapp_module)
    asyncio.run(myapp_module.run(
        task_id = test_task.request.id
    ))
    return f"[myapp: {myapp_id}, task_id: {test_task.request.id}]"

Below are my media variables that I have in settings.py

DEFAULT_FILE_STORAGE = 'azure.storage_production.AzureMediaStorage'
MEDIA_LOCATION = "media"
AZURE_ACCOUNT_NAME = os.environ.get('AZURE_ACCOUNT_NAME')
AZURE_CUSTOM_DOMAIN = f'{AZURE_ACCOUNT_NAME}.blob.core.windows.net'
MEDIA_URL = f'https://{AZURE_CUSTOM_DOMAIN}/{MEDIA_LOCATION}/'

If anyone can tell me what am I doing wrong I'd be grateful.

Back to Top