Skip to content

Instantly share code, notes, and snippets.

@rndazurescript
Last active May 2, 2020 17:02
Show Gist options
  • Save rndazurescript/4066b61483a108af0825a38f84ae5f89 to your computer and use it in GitHub Desktop.
Save rndazurescript/4066b61483a108af0825a38f84ae5f89 to your computer and use it in GitHub Desktop.
Play with Azure Storage
from azure.storage.common.cloudstorageaccount import CloudStorageAccount
from azure.storage.common.models import AccessPolicy
from azure.storage.blob import BlockBlobService, PageBlobService, AppendBlobService
from azure.storage.models import CorsRule, Logging, Metrics, RetentionPolicy, ResourceTypes, AccountPermissions
from azure.storage.blob.models import BlobBlock, ContainerPermissions, ContentSettings
from datetime import datetime, timedelta
import time
import json
settings= {}
with open('./settings.json') as f:
settings = json.load(f)
account_name = settings["STORAGE_ACCOUNT_NAME"]
account_key = settings["STORAGE_ACCOUNT_KEY"]
account = CloudStorageAccount(account_name, account_key)
blobService = account.create_block_blob_service()
container_name = "testattach"
# Note that the name has date in it so that if I have to delete
# and create a new one, I won't accidentally enable old access tokens
policyId = "2020-05-02-readlist-access"
# Set access policy on container
access_policy = AccessPolicy(permission=ContainerPermissions(read=True, list=True),
expiry=datetime.utcnow() + timedelta(hours=10))
identifiers = {policyId: access_policy}
acl = blobService.set_container_acl(container_name, identifiers)
# Wait 30 seconds for acl to propagate
time.sleep(30)
# Indicates to use the access policy set on the container
token = blobService.generate_container_shared_access_signature(
container_name,
id=policyId
)
print("Token: {}".format(token))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment