How to use or work with fileshare.aio library?

Vaibhav Jain 40 Reputation points
2023-09-04T12:12:35.8166667+00:00

I have been trying to implement the azure.storage.fileshare.aio Python SDK to upload files to my Azure File Share.
I tried using the asyncio class and its functions to implement the directory_client.upload_file() function, that seems to be not working as expected and the control directly jumps to the return statement.

I have provided the code snippet below. Please excuse any of the knowledge gap regarding async implementation, I have just started off with it.

def some_other_func():
	.....

	directory_client = get_directory_client_aio(base_url=file_dir_url)
	response = asyncio.run(save(directory_client, new_file_with_changes, tasks=tasks))
	
	.....

	return response


async def save(directory_client, new_file_with_changes, tasks : list):
    async with directory_client:
        tasks.append(asyncio.create_task(directory_client.upload_file(file_nam=new_file_with_changes.name,
 data=new_file_with_changes, 
length=new_file_with_changes.size)))
        asyncio.gather(*tasks)

    return Response.send_response(code=55)

It will be appreciated if I get some assistance, Do's and Don'ts on the same.
Thank you.

Azure Files
Azure Files
An Azure service that offers file shares in the cloud.
1,288 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bhanu Ejjagiri 261 Reputation points Microsoft Employee
    2023-09-06T15:01:53.16+00:00

    Hi @Vaibhav Jain

    Thanks for reaching Microsoft Q&A team.

    We have a limited support scope with Python and here are my recommendations at my best efforts.

    Please try this code using asyncio:

    async def save(directory_client, new_file_with_changes, tasks: list):
        try:
            async with directory_client:
                tasks.append(asyncio.create_task(directory_client.upload_file(file_name=new_file_with_changes.name,
                                                                            data=new_file_with_changes,
                                                                            length=new_file_with_changes.size)))
    
            await asyncio.gather(*tasks)
            return Response.send_response(code=55)
        except Exception as e:
            # Handle exceptions here, log them, and possibly return an error response
            return Response.send_response(code=500, message=f"Error uploading file: {str(e)}")
    
    

    With these changes, your code should work more effectively for uploading files to Azure File Share using asyncio. Make sure to handle exceptions appropriately for robust error handling.

    Please let us know if you have any further queries. I’m happy to assist you further.     

    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    Thanks!


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.