Michael Tito
Michael Tito

Reputation: 45

Upload files to a Container in Azure Blob Storage using Python

I want to upload a couple of files to a container. I have tried the approaches mentioned here but it did not work.

Here's what I have:

SAS URL: https://xxxx.blob.core.windows.net?sp=acw&st=2022-05-31T17:54:22Z&se=2022-06-05T10:59:59Z&sv=2020-08-04&sr=c&sig=H0g%2BY%2FOnNIaYNVTsX%2FP42buWaowxIlxQDJ0xqH0gvqQ%3D

Container Name: XYZ

I want to upload the files to the container root, without any subfolders.

Edit: My code so far:

sas = 'https://xxxx.blob.core.windows.net?sp=acw&st=2022-05-31T17:54:22Z&se=2022-06-05T10:59:59Z&sv=2020-08-04&sr=c&sig=H0g%2BY%2FOnNIaYNVTsX%2FP42buWaowxIlxQDJ0xqH0gvqQ%3D'

container = 'container_name'
sasUrlParts = urlparse(sas)
accountEndpoint = sasUrlParts.scheme + '://' + sasUrlParts.netloc
sasToken = sasUrlParts.query

blobSasUrl = accountEndpoint + '/' + container + '?' + sasToken

blobClient = BlobClient.from_blob_url(blobSasUrl)

with open('/file_to_be_uploaded.csv', 'rb') as f:
    blobClient.upload_blob(f)

which produces the following error in the line blobClient = BlobClient.from_blob_url(blobSasUrl):

ValueError: Invalid URL. Provide a blob_url with a valid blob and container name

Upvotes: 2

Views: 3720

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136336

I believe the reason you're getting the error is because your blobSasUrl does not include the name of the blob.

Please try by changing the following line of code:

blobSasUrl = accountEndpoint + '/' + container + '?' + sasToken

to

blobName = 'file_to_be_uploaded.csv'
blobSasUrl = accountEndpoint + '/' + container + '/' + blobName + '?' + sasToken

And you should not get the error you are getting.

Upvotes: 6

Related Questions