'With help of Python, how can I create dynamic subfolder under Azure Blobstorage?

I am trying to create a dynamic folder (current date with mm-dd-yyyy)under the blob store. I've tried different resources including this one: How can I safely create a nested directory? and used these guides to attempt to create a dynamic folder, but fail with the following error:

Container_client2 = blob_service_client.create_container(container_name/folder) TypeError: unsupported operand type(s) for /: 'str' and 'str'

Here is the snapshot of my code:

import os

from pathlib import Path
from datetime import datetime
# Instantiate a new BlobServiceClient using a connection string
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient 
from azure.core.exceptions import ResourceNotFoundError, ResourceExistsError

today = datetime.now()

try:
    connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING')

    # Create the BlobServiceClient object which will be used to create a container client
    # Instantiate a BlobServiceClient using a connection string
    blob_service_client = BlobServiceClient.from_connection_string(connect_str)

    # Create a unique name for the container
    folder = today.strftime('%m-%d-%Y')
    container_name = '2021' 
    print('Folder Name ---> ' + folder)
   
    # Create the container
    Container_client1 = blob_service_client.get_container_client(container_name)
    Container_client2 = blob_service_client.create_container(container_name/str(folder))
    print ("get Container client---" + str(Container_client1))
    try:
        for blob in Container_client1.list_blobs():
            print("Found blob: ", blob.name)
    except ResourceNotFoundError:
        print("Container not found.")     
finally:
    print ("nothing")



Solution 1:[1]

Joel Cochran is correct, there are no subfolders in Azure Blob. The Blob service is based on a flat storage scheme, not a hierarchical scheme. However, you may specify a character or string delimiter within a blob name to create a virtual hierarchy. For example, the following list shows valid and unique blob names. Notice that a string can be valid as both a blob name and as a virtual directory name in the same container:

  • /a
  • /a.txt
  • /a/b
  • /a/b.txt

You can take advantage of the delimiter character when enumerating blobs.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ken W MSFT