/
blob_devguide_create_sas.py
221 lines (178 loc) · 9.96 KB
/
blob_devguide_create_sas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import datetime
import os, uuid
from azure.identity import DefaultAzureCredential
from azure.storage.blob import (
BlobServiceClient,
ContainerClient,
BlobClient,
BlobSasPermissions,
ContainerSasPermissions,
ResourceTypes,
AccountSasPermissions,
UserDelegationKey,
generate_account_sas,
generate_container_sas,
generate_blob_sas
)
class SASSamples(object):
# <Snippet_create_account_sas>
def create_account_sas(self, account_name: str, account_key: str):
# Create an account SAS that's valid for one day
start_time = datetime.datetime.now(datetime.timezone.utc)
expiry_time = start_time + datetime.timedelta(days=1)
# Define the SAS token permissions
sas_permissions=AccountSasPermissions(read=True)
# Define the SAS token resource types
# For this example, we grant access to service-level APIs
sas_resource_types=ResourceTypes(service=True)
sas_token = generate_account_sas(
account_name=account_name,
account_key=account_key,
resource_types=sas_resource_types,
permission=sas_permissions,
expiry=expiry_time,
start=start_time
)
return sas_token
# </Snippet_create_account_sas>
def use_account_sas(self, blob_service_client: BlobServiceClient):
account_name = blob_service_client.account_name
account_key = blob_service_client.credential.account_key
sas_token = self.create_account_sas(account_name=account_name, account_key=account_key)
# <Snippet_use_account_sas>
# The SAS token string can be appended to the account URL with a ? delimiter
# or passed as the credential argument to the client constructor
account_sas_url = f"{blob_service_client.url}?{sas_token}"
# Create a BlobServiceClient object
blob_service_client_sas = BlobServiceClient(account_url=account_sas_url)
# </Snippet_use_account_sas>
# <Snippet_request_user_delegation_key>
def request_user_delegation_key(self, blob_service_client: BlobServiceClient) -> UserDelegationKey:
# Get a user delegation key that's valid for 1 day
delegation_key_start_time = datetime.datetime.now(datetime.timezone.utc)
delegation_key_expiry_time = delegation_key_start_time + datetime.timedelta(days=1)
user_delegation_key = blob_service_client.get_user_delegation_key(
key_start_time=delegation_key_start_time,
key_expiry_time=delegation_key_expiry_time
)
return user_delegation_key
# </Snippet_request_user_delegation_key>
# <Snippet_create_user_delegation_sas_blob>
def create_user_delegation_sas_blob(self, blob_client: BlobClient, user_delegation_key: UserDelegationKey):
# Create a SAS token that's valid for one day, as an example
start_time = datetime.datetime.now(datetime.timezone.utc)
expiry_time = start_time + datetime.timedelta(days=1)
sas_token = generate_blob_sas(
account_name=blob_client.account_name,
container_name=blob_client.container_name,
blob_name=blob_client.blob_name,
user_delegation_key=user_delegation_key,
permission=BlobSasPermissions(read=True),
expiry=expiry_time,
start=start_time
)
return sas_token
# </Snippet_create_user_delegation_sas_blob>
def use_user_delegation_sas_blob(self, blob_service_client: BlobServiceClient):
user_delegation_key = self.request_user_delegation_key(blob_service_client=blob_service_client)
blob_client = blob_service_client.get_blob_client(container="sample-container", blob="sample-blob.txt")
sas_token = self.create_user_delegation_sas_blob(blob_client=blob_client, user_delegation_key=user_delegation_key)
# <Snippet_use_user_delegation_sas_blob>
# The SAS token string can be appended to the resource URL with a ? delimiter
# or passed as the credential argument to the client constructor
sas_url = f"{blob_client.url}?{sas_token}"
# Create a BlobClient object with SAS authorization
blob_client_sas = BlobClient.from_blob_url(blob_url=sas_url)
# </Snippet_use_user_delegation_sas_blob>
# <Snippet_create_user_delegation_sas_container>
def create_user_delegation_sas_container(self, container_client: ContainerClient, user_delegation_key: UserDelegationKey):
# Create a SAS token that's valid for one day, as an example
start_time = datetime.datetime.now(datetime.timezone.utc)
expiry_time = start_time + datetime.timedelta(days=1)
sas_token = generate_container_sas(
account_name=container_client.account_name,
container_name=container_client.container_name,
user_delegation_key=user_delegation_key,
permission=ContainerSasPermissions(read=True),
expiry=expiry_time,
start=start_time
)
return sas_token
# </Snippet_create_user_delegation_sas_container>
def use_user_delegation_sas_container(self, blob_service_client: BlobServiceClient):
user_delegation_key = self.request_user_delegation_key(blob_service_client=blob_service_client)
container_client = blob_service_client.get_container_client(container="sample-container")
sas_token = self.create_user_delegation_sas_container(container_client=container_client, user_delegation_key=user_delegation_key)
# <Snippet_use_user_delegation_sas_container>
# The SAS token string can be appended to the resource URL with a ? delimiter
# or passed as the credential argument to the client constructor
sas_url = f"{container_client.url}?{sas_token}"
# Create a ContainerClient object with SAS authorization
container_client_sas = ContainerClient.from_container_url(container_url=sas_url)
# </Snippet_use_user_delegation_sas_container>
# <Snippet_create_service_sas_container>
def create_service_sas_container(self, container_client: ContainerClient, account_key: str):
# Create a SAS token that's valid for one day, as an example
start_time = datetime.datetime.now(datetime.timezone.utc)
expiry_time = start_time + datetime.timedelta(days=1)
sas_token = generate_container_sas(
account_name=container_client.account_name,
container_name=container_client.container_name,
account_key=account_key,
permission=ContainerSasPermissions(read=True),
expiry=expiry_time,
start=start_time
)
return sas_token
# </Snippet_create_service_sas_container>
def use_service_sas_container(self, blob_service_client: BlobServiceClient):
container_client = blob_service_client.get_container_client(container="sample-container")
# Assumes the service client object was created with a shared access key
sas_token = self.create_service_sas_container(container_client=container_client, account_key=blob_service_client.credential.account_key)
# <Snippet_use_service_sas_container>
# The SAS token string can be appended to the resource URL with a ? delimiter
# or passed as the credential argument to the client constructor
sas_url = f"{container_client.url}?{sas_token}"
# Create a ContainerClient object with SAS authorization
container_client_sas = ContainerClient.from_container_url(container_url=sas_url)
# </Snippet_use_service_sas_container>
# <Snippet_create_service_sas_blob>
def create_service_sas_blob(self, blob_client: BlobClient, account_key: str):
# Create a SAS token that's valid for one day, as an example
start_time = datetime.datetime.now(datetime.timezone.utc)
expiry_time = start_time + datetime.timedelta(days=1)
sas_token = generate_blob_sas(
account_name=blob_client.account_name,
container_name=blob_client.container_name,
blob_name=blob_client.blob_name,
account_key=account_key,
permission=BlobSasPermissions(read=True),
expiry=expiry_time,
start=start_time
)
return sas_token
# </Snippet_create_service_sas_blob>
def use_service_sas_blob(self, blob_service_client: BlobServiceClient):
blob_client = blob_service_client.get_blob_client(container="sample-container", blob="sample-blob.txt")
# Assumes the service client object was created with a shared access key
sas_token = self.create_service_sas_blob(blob_client=blob_client, account_key=blob_service_client.credential.account_key)
# <Snippet_use_service_sas_blob>
# The SAS token string can be appended to the resource URL with a ? delimiter
# or passed as the credential argument to the client constructor
sas_url = f"{blob_client.url}?{sas_token}"
# Create a BlobClient object with SAS authorization
blob_client_sas = BlobClient.from_blob_url(blob_url=sas_url)
# </Snippet_use_service_sas_blob>
if __name__ == '__main__':
sample = SASSamples()
# TODO: Replace <storage-account-name> with your actual storage account name
account_url = "https://<storage-account-name>.blob.core.windows.net"
blob_service_client = BlobServiceClient(account_url, credential=DefaultAzureCredential())
sample.use_user_delegation_sas_blob(blob_service_client=blob_service_client)
sample.use_user_delegation_sas_container(blob_service_client=blob_service_client)
account_url = "https://<storage-account-name>.blob.core.windows.net"
account_key = "<account-key>"
blob_service_client_account_key = BlobServiceClient(account_url, credential=account_key)
sample.use_service_sas_container(blob_service_client=blob_service_client_account_key)
sample.use_service_sas_blob(blob_service_client=blob_service_client_account_key)
sample.use_account_sas(blob_service_client=blob_service_client_account_key)