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
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
andcontainer name
it executed successfully.Code:
Output:
Reference:
Upload a blob with Python – Azure Storage | Microsoft Learn
In this case, the container name would be
a
. If you want to upload data inb/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: