You are implementing a secure file-sharing module in a Python application using the `azure-storage-blob` (v12) SDK. A client application needs temporary, read-only access to a specific report blob named `q4_report.pdf` located in a container named `reports`. To adhere to the principle of least privilege, you must generate a Shared Access Signature (SAS) token that restricts access to only this single blob, allowing only read operations, and expiring in one hour. Which code segment should you use to generate the SAS token?
- from datetime import datetime, timedelta
from azure.storage.blob import generate_blob_sas, BlobSasPermissions
sas_token = generate_blob_sas(
account_name="mystorage",
container_name="reports",
blob_name="q4_report.pdf",
account_key="mykey",
permission=BlobSasPermissions(read=True),
expiry=datetime.utcnow() + timedelta(hours=1)
)Answer - Bfrom datetime import datetime, timedelta
from azure.storage.blob import generate_container_sas, ContainerSasPermissions
sas_token = generate_container_sas(
account_name="mystorage",
container_name="reports",
account_key="mykey",
permission=ContainerSasPermissions(read=True),
expiry=datetime.utcnow() + timedelta(hours=1)
) - Cfrom datetime import datetime, timedelta
from azure.storage.blob import generate_blob_sas, BlobSasPermissions
sas_token = generate_blob_sas(
account_name="mystorage",
container_name="reports",
blob_name="q4_report.pdf",
account_key="mykey",
permission=BlobSasPermissions(read=True, write=True, delete=True),
expiry=datetime.utcnow() + timedelta(hours=1)
) - Dfrom datetime import datetime, timedelta
from azure.storage.blob import generate_account_sas, AccountSasPermissions, ResourceTypes
sas_token = generate_account_sas(
account_name="mystorage",
account_key="mykey",
resource_types=ResourceTypes(object=True),
permission=AccountSasPermissions(read=True),
expiry=datetime.utcnow() + timedelta(hours=1)
)
Answer
The correct code segment uses `generate_blob_sas` from the `azure.storage.blob` package, targeting the specific blob name 'q4_report.pdf' inside the container 'reports', and specifies `BlobSasPermissions(read=True)` to ensure read-only access, adhering to the principle of least privilege.
The correct segment calls `generate_blob_sas` specifying the single target blob `q4_report.pdf` within the container `reports` and configuring `BlobSasPermissions(read=True)`. This ensures that access is locked down specifically to the requested blob with read-only permissions for one hour.
Step-by-Step Solution
Key Concept
Generating Shared Access Signatures (SAS) with least privilege scopes and permissions using the Azure Storage Blobs SDK.