Fix generatte_download_link() -- Use BlockBlobService and BlobPermissions

This commit is contained in:
leigh-mil 2020-02-06 16:35:10 -05:00
parent e744fddbf9
commit 39f8e4ce9c

View File

@ -43,10 +43,12 @@ class AzureFileService(FileService):
from azure.storage.common import CloudStorageAccount from azure.storage.common import CloudStorageAccount
from azure.storage.blob import BlobSasPermissions from azure.storage.blob import BlobSasPermissions
from azure.storage.blob.models import BlobPermissions
from azure.storage.blob.blockblobservice import BlockBlobService from azure.storage.blob.blockblobservice import BlockBlobService
self.CloudStorageAccount = CloudStorageAccount self.CloudStorageAccount = CloudStorageAccount
self.BlobSasPermissions = BlobSasPermissions self.BlobSasPermissions = BlobSasPermissions
self.BlobPermissions = BlobPermissions
self.BlockBlobService = BlockBlobService self.BlockBlobService = BlockBlobService
def get_token(self): def get_token(self):
@ -72,20 +74,22 @@ class AzureFileService(FileService):
return ({"token": sas_token}, object_name) return ({"token": sas_token}, object_name)
def generate_download_link(self, object_name, filename): def generate_download_link(self, object_name, filename):
account = self.CloudStorageAccount( block_blob_service = self.BlockBlobService(
account_name=self.account_name, account_key=self.storage_key account_name=self.account_name, account_key=self.storage_key
) )
bbs = account.create_block_blob_service() sas_token = block_blob_service.generate_blob_shared_access_signature(
sas_token = bbs.generate_blob_shared_access_signature( container_name=self.container_name,
self.container_name, blob_name=object_name,
object_name, permission=self.BlobPermissions(read=True),
permission=self.BlobSasPermissions(read=True),
expiry=datetime.utcnow() + self.timeout, expiry=datetime.utcnow() + self.timeout,
content_disposition=f"attachment; filename={filename}", content_disposition=f"attachment; filename={filename}",
protocol="https", protocol="https",
) )
return bbs.make_blob_url( return block_blob_service.make_blob_url(
self.container_name, object_name, protocol="https", sas_token=sas_token container_name=self.container_name,
blob_name=object_name,
protocol="https",
sas_token=sas_token,
) )
def download_task_order(self, object_name): def download_task_order(self, object_name):