使用 PYTHON 中的 SAS URI 从 AZURE BLOB CONTAINER 下载文件



我有 Azure 容器,我在其中保存一些文件。我需要使用 python 代码访问它们我在JAVA中做了同样的事情,但我无法在Python中复制它

这是相同的java代码。

CloudBlobContainer Con = new CloudBlobContainer("Some SAS URI");
CloudBlockBlob blob1 = Con.getBlockBlobReference(fileName);
blob1.downloadToFile(filePath+fileName+userName);

python 中没有等效的方法,可以看看 python 的 Container 类

应始终将BlockBlobService与 sas 令牌(如果您有 sas uri,则可以从中获取 sas 令牌(或帐户密钥一起使用,如下所示(如果使用 sas 令牌(:

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

如果您使用的是没有BlockBlobService的较新版本,则可以使用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())

最新更新