Amit vishnoi
Amit vishnoi

Reputation: 1

Download file from AZURE BLOB CONTAINER using SAS URI in PYTHON

I have Azure container where i keep some files. I need to access them using python code I did same thing in JAVA but i am unable to replicate it in Python

//This is java code for same.

CloudBlobContainer Con = new CloudBlobContainer("Some SAS URI");

CloudBlockBlob blob1 = Con.getBlockBlobReference(fileName);

blob1.downloadToFile(filePath+fileName+userName);

Upvotes: 0

Views: 5249

Answers (2)

anon
anon

Reputation:

If you are using newer version that does not have BlockBlobService, you can use BlobClient:

from azure.storage.blob import BlobClient

blob_client = BlobClient.from_blob_url(sas_url)
with open(file=blob_client.blob_name, mode="wb") as blob_file:
    download_stream = blob_client.download_blob()
    blob_file.write(download_stream.readall())

Upvotes: 0

Ivan Glasenberg
Ivan Glasenberg

Reputation: 29940

There is no equivalent method in python, you can take a look at the Container class of python

You should always use BlockBlobService with sas token(if you have a sas uri, you can get sas token from it) or account key, like below if you use sas token:

from azure.storage.blob import BlockBlobService

blobservice = BlockBlobService("storage_account",sas_token="?sv=2018-03-28&ss=bfqt&srt=sco&sp=rwdlacup&se=2019-04-24T10:01:58Z&st=2019-04-23T02:01:58Z&spr=https&sig=xxxxxxxxx")
blobservice.get_blob_to_path("container_name","blob_name","local_file_path")

Upvotes: 3

Related Questions