0

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")

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Sunny
  • 1
  • 2
  • 1
    You can't create them using "create_container" because a) they are not containers and b) folders are not real in Blob Storage (see https://stackoverflow.com/questions/65912473/how-to-create-directories-in-azure-storage-container-without-creating-extra-file/65923129#65923129 for more). You can create (the illusion of) them by prepending your blob name with the desired folder value. – Joel Cochran Mar 01 '22 at 19:00

1 Answers1

0

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.

Ken W - Zero Networks
  • 3,533
  • 1
  • 13
  • 18
  • Yes, you both are right. That is I tried with ```Container_client2 = blob_service_client.create_container(container_name/str(folder)) ``` – Sunny Mar 01 '22 at 20:49
  • The container name can't have '/' in it, it's the blob you name 'bar/file.txt' in the container named 'foo'. That would then make it look like the file 'file.txt' was sitting in folder bar which is sitting in folder foo. Containers names can only be letters, numbers, and the hyphen. – Ken W - Zero Networks Mar 01 '22 at 20:58
  • thank you. I will try that. thanks for sharing your knowledge. – Sunny Mar 01 '22 at 22:40