skip to Main Content

I’m trying to upload a json file directly from my python script (VSC) to an Azure blob container.

Here is what I’ve tried:

account_url = "https://containerxyz.blob.core.windows.net"
default_credential = DefaultAzureCredential()
blob_service_client = BlobServiceClient(account_url, credential=default_credential)

container_name = 'https://containerxyz.blob.core.windows.net/a/b/raw/'

file = 'test.txt'
contents = 'test'
blob_client = blob_service_client.get_blob_client(container=container_name, blob=contents)
blob_client.upload_blob(name=file, data=contents, overwrite=True)

I don’t even get an error code, it just runs and never stops and I eventually interrupt the kernel after a couple minutes.

The same thing happens when I try it a bit differently:

data = 'test'
container_client = blob_service_client.get_container_client(container=container_name)
container_client.upload_blob(name="test.txt", data=data, overwrite=True)

I’ve tried following the Azure docs but they always use examples that take a local file and upload it to azure using "with open(…)" e.g: https://learn.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python

If I run everything before the upload_blob() function it runs without errors so I’m assuming the problem is there.

2

Answers


  1. I don’t even get an error code, it just runs and never stops and I eventually interrupt the kernel after a couple of minutes

    I agree with Gaurav Mantri’s comment, the issue may be using an incorrect storage account or container name.

    I tried in my environment with proper account url and container name it executed successfully.

    Code:

    from azure.identity import DefaultAzureCredential
    from azure.storage.blob import BlobServiceClient
    
    
    account_url = "https://<your-storage-account-name>.blob.core.windows.net"
    default_credential = DefaultAzureCredential()
    blob_service_client = BlobServiceClient(account_url, credential=default_credential)
    container_name = 'test1' #your-container-name
    file = 'test.txt'
    content=b"test"
    blob_client = blob_service_client.get_blob_client(container=container_name, blob=file)
    blob_client.upload_blob(data=content, overwrite=True)
    
    print(f"File {file} uploaded to blob {blob_client.blob_name} in container {blob_client.container_name}")
    

    Output:

    File test.txt uploaded to blob test.txt in container test1
    

    enter image description here

    Reference:

    Upload a blob with Python – Azure Storage | Microsoft Learn

    Login or Signup to reply.
  2. my account name is "containerxyz", in that account I have multiple
    directories and I want to upload the blob to "/a/b/raw". So the
    container name is "a/b/raw"? I tried this and got the same issue.

    In this case, the container name would be a. If you want to upload data in b/raw folder (which is virtual BTW), you would need to include this in the name of the blob.

    So your code would be something like:

    account_url = "https://containerxyz.blob.core.windows.net"
    default_credential = DefaultAzureCredential()
    blob_service_client = BlobServiceClient(account_url, credential=default_credential)
    
    container_name = 'a'
    
    file = 'b/raw/test.txt'
    contents = 'test'
    blob_client = blob_service_client.get_blob_client(container=container_name, blob=file)
    blob_client.upload_blob(name=file, data=contents, overwrite=True)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search