I have a uploadFilesToAzure
method which works and the file is getting uploaded to my Azure’s container.
But I can’t seem to get the download to work. The azure link is correct but Azure says Resource not found.
def downloadFileFromAzure(azure_url):
# https%3A%2F%2FmyContainer.blob.core.windows.net%2Fattachments%2Fservice-orders%2F424%2Fattach-accounts%2Fdummy.txt
print(azure_url)
# Instantiate a BlobServiceClient using a connection string
blob_service_client = BlobServiceClient.from_connection_string(settings.AZURE_STORAGE_ACCOUNT_STRING)
# Name of the Azure Storage Container
container_name = settings.AZURE_STORAGE_CONTAINER_NAME
# Create a BlobServiceClient object
container_client = blob_service_client.get_container_client(container_name)
# The destination file name
decoded_url = unquote(azure_url)
parsed_url = urlparse(decoded_url)
blob_name = unquote(parsed_url.path.lstrip("/"))
blob_client = blob_service_client.get_blob_client(container_name, blob_name)
destination_dir = os.path.join(settings.MEDIA_ROOT, 'cache')
parsed_url = urlparse(unquote(azure_url))
file_name = unquote(parsed_url.path.split("/")[-1])
print(file_name)
with open(file=os.path.join(destination_dir, file_name), mode="wb") as sample_blob:
download_stream = blob_client.download_blob()
sample_blob.write(download_stream.readall())