How to upload file to virtual folder in blob container?

I’m currently working on a function to upload a file to a virtual folder that is in my blob container. E.g. I want to upload the file into "container1\folder" rather than "container1".

So far, I am able to upload the file directly into the container with the following code:

filename = "test.txt"
file_path = "C:\Users\____\Desktop\test.txt"

blob_client = blob_service_client.get_blob_client(container='container1', blob=filename)
with open(file_path, "rb") as data:
  blob_client.upload_blob(data)

Any tips or suggestions on how to upload the file into a virtual folder would be much appreciated.

>Solution :

You can just prepend the path to the filename

var file_name = "file.txt";

var folder_name = "folder1";

var blobpath = f"{folder_name}/{file_name}";

file_path = "C:\Users\____\Desktop\test.txt"

blob_client = blob_service_client.get_blob_client(container='container1', blob=blob_path)
with open(file_path, "rb") as data:
  blob_client.upload_blob(data)

Leave a Reply