I'm trying to generate sas token for accessing Azure storage. While referencing to online samples, I came accross this sample code snippet:
from azure.storage.blob import BlockBlobService, BlobPermissions, generate_account_sas
However, it fails to find generate_account_sas module. I suspected it could be due to Azure versions, and tried pip uninstall azure, then pip install azure-storage-blob==12.9.0 azure-core==1.21.1. This allows me to access in a different manner though:
from azure.storage.blob._shared_access_signature import generate_account_sas
However, it would also require me to revise all those BlockBlobService related lines in my code as well.
My question is, how can I use generate_account_sas in the form of my original way, without uninstalling and re-installing those packages?
CodePudding user response:
As mentioned in this answer, there were breaking changes introduced to the azure-storage library since 0.37.0. According to the change log, not only has the namespaces been changed, but the library has also been split into 5 different packages:
- azure-storage-common
- azure-storage-blob
- azure-storage-file
- azure-storage-queue
- azure-storage-nspkg
I believe you were following the new guide which uses azure-storage-blob and were trying to use code for the old azure-storage library, thus the ImportError.
Since BlockBlobService is from the old azure-storage library, to continue using it, you need to pip uninstall azure-storage-blob if you have installed the new library by accident, and then pip install azure-storage to install the old library. So, As of my knowledge you need to uninstall and reinstall those packages is the only way.
