0
# Service Operations
1
2
Account-level operations for managing file systems, user delegation keys, and service properties. The DataLakeServiceClient provides the entry point for accessing Data Lake Storage Gen2 resources and managing account-wide settings.
3
4
## Capabilities
5
6
### DataLakeServiceClient
7
8
Main service client for Data Lake Storage Gen2 operations at the account level. Manages file systems and provides factory methods for creating other client types.
9
10
```python { .api }
11
class DataLakeServiceClient:
12
"""
13
A client to interact with Azure Data Lake Storage Gen2 account.
14
15
Attributes:
16
url (str): The full endpoint URL to the datalake service endpoint
17
primary_endpoint (str): The full primary endpoint URL
18
primary_hostname (str): The hostname of the primary endpoint
19
"""
20
21
def __init__(
22
self,
23
account_url: str,
24
credential=None,
25
**kwargs
26
):
27
"""
28
Initialize the DataLakeServiceClient.
29
30
Args:
31
account_url (str): The URL to the DataLake storage account
32
credential: Authentication credential (account key, SAS token, etc.)
33
**kwargs: Additional client configuration options
34
"""
35
36
@classmethod
37
def from_connection_string(
38
cls,
39
conn_str: str,
40
credential=None,
41
**kwargs
42
) -> 'DataLakeServiceClient':
43
"""
44
Create DataLakeServiceClient from connection string.
45
46
Args:
47
conn_str (str): Connection string for the storage account
48
credential: Optional credential to override connection string auth
49
**kwargs: Additional client configuration options
50
51
Returns:
52
DataLakeServiceClient: The service client instance
53
"""
54
55
def close(self) -> None:
56
"""Close the sockets opened by the client."""
57
58
def __enter__(self) -> 'DataLakeServiceClient':
59
"""Context manager entry."""
60
61
def __exit__(self, *args) -> None:
62
"""Context manager exit."""
63
```
64
65
**Usage Examples:**
66
67
```python
68
from azure.storage.filedatalake import DataLakeServiceClient
69
70
# Using account key
71
service_client = DataLakeServiceClient(
72
account_url="https://mystorageaccount.dfs.core.windows.net",
73
credential="<account_key>"
74
)
75
76
# Using connection string
77
service_client = DataLakeServiceClient.from_connection_string(
78
"DefaultEndpointsProtocol=https;AccountName=mystorageaccount;AccountKey=<key>;EndpointSuffix=core.windows.net"
79
)
80
81
# Using Azure Identity (requires azure-identity package)
82
from azure.identity import DefaultAzureCredential
83
84
service_client = DataLakeServiceClient(
85
account_url="https://mystorageaccount.dfs.core.windows.net",
86
credential=DefaultAzureCredential()
87
)
88
```
89
90
### File System Management
91
92
Operations for creating, listing, and managing file systems within the storage account.
93
94
```python { .api }
95
def create_file_system(
96
self,
97
file_system: Union[FileSystemProperties, str],
98
metadata: Dict[str, str] = None,
99
public_access: PublicAccess = None,
100
**kwargs
101
) -> FileSystemClient:
102
"""
103
Create a new file system in the account.
104
105
Args:
106
file_system: Name of the file system or FileSystemProperties object
107
metadata (dict, optional): Metadata key-value pairs
108
public_access (PublicAccess, optional): Public access level
109
**kwargs: Additional options
110
111
Returns:
112
FileSystemClient: Client for the created file system
113
"""
114
115
def list_file_systems(
116
self,
117
name_starts_with: str = None,
118
include_metadata: bool = False,
119
**kwargs
120
) -> ItemPaged[FileSystemProperties]:
121
"""
122
List file systems in the account.
123
124
Args:
125
name_starts_with (str, optional): Filter by prefix
126
include_metadata (bool): Include metadata in results
127
**kwargs: Additional options
128
129
Returns:
130
ItemPaged[FileSystemProperties]: Paged list of file systems
131
"""
132
133
def delete_file_system(
134
self,
135
file_system: Union[FileSystemProperties, str],
136
**kwargs
137
) -> FileSystemClient:
138
"""
139
Delete a file system.
140
141
Args:
142
file_system: Name of the file system or FileSystemProperties object
143
**kwargs: Additional options
144
145
Returns:
146
FileSystemClient: Client for the deleted file system
147
"""
148
149
def undelete_file_system(
150
self,
151
name: str,
152
deleted_version: str,
153
**kwargs
154
) -> FileSystemClient:
155
"""
156
Restore a soft-deleted file system.
157
158
Args:
159
name (str): Name of the deleted file system
160
deleted_version (str): Version identifier of the deleted file system
161
**kwargs: Additional options
162
163
Returns:
164
FileSystemClient: Client for the restored file system
165
"""
166
167
def get_user_delegation_key(
168
self,
169
key_start_time: datetime,
170
key_expiry_time: datetime,
171
**kwargs
172
) -> UserDelegationKey:
173
"""
174
Get a user delegation key for creating user delegation SAS tokens.
175
176
Args:
177
key_start_time (datetime): Start time for the key validity
178
key_expiry_time (datetime): Expiry time for the key validity
179
**kwargs: Additional options
180
181
Returns:
182
UserDelegationKey: The user delegation key for SAS token creation
183
"""
184
```
185
186
### Client Factory Methods
187
188
Methods for creating specialized client instances for specific resources.
189
190
```python { .api }
191
def get_file_system_client(
192
self,
193
file_system: Union[FileSystemProperties, str]
194
) -> FileSystemClient:
195
"""
196
Get a FileSystemClient for a specific file system.
197
198
Args:
199
file_system: Name of the file system or FileSystemProperties object
200
201
Returns:
202
FileSystemClient: Client for the specified file system
203
"""
204
205
def get_directory_client(
206
self,
207
file_system: Union[FileSystemProperties, str],
208
directory: Union[DirectoryProperties, str]
209
) -> DataLakeDirectoryClient:
210
"""
211
Get a DataLakeDirectoryClient for a specific directory.
212
213
Args:
214
file_system: Name of the file system or FileSystemProperties object
215
directory: Name of the directory or DirectoryProperties object
216
217
Returns:
218
DataLakeDirectoryClient: Client for the specified directory
219
"""
220
221
def get_file_client(
222
self,
223
file_system: Union[FileSystemProperties, str],
224
file_path: Union[FileProperties, str]
225
) -> DataLakeFileClient:
226
"""
227
Get a DataLakeFileClient for a specific file.
228
229
Args:
230
file_system: Name of the file system or FileSystemProperties object
231
file_path: Path to the file or FileProperties object
232
233
Returns:
234
DataLakeFileClient: Client for the specified file
235
"""
236
```
237
238
### Service Properties and Delegation Keys
239
240
Operations for managing account-level service properties and obtaining user delegation keys for SAS token generation.
241
242
```python { .api }
243
def get_service_properties(self, **kwargs) -> Dict[str, Any]:
244
"""
245
Get storage account service properties.
246
247
Args:
248
**kwargs: Additional options
249
250
Returns:
251
dict: Service properties including CORS, logging, metrics settings
252
"""
253
254
def set_service_properties(self, **kwargs) -> None:
255
"""
256
Set storage account service properties.
257
258
Args:
259
analytics_logging (AnalyticsLogging, optional): Logging configuration
260
hour_metrics (Metrics, optional): Hour metrics configuration
261
minute_metrics (Metrics, optional): Minute metrics configuration
262
cors (List[CorsRule], optional): CORS rules
263
target_version (str, optional): Target service version
264
delete_retention_policy (RetentionPolicy, optional): Delete retention policy
265
static_website (StaticWebsite, optional): Static website configuration
266
**kwargs: Additional options
267
"""
268
269
```
270
271
**Usage Examples:**
272
273
```python
274
from datetime import datetime, timedelta
275
276
# List all file systems
277
file_systems = service_client.list_file_systems(include_metadata=True)
278
for fs in file_systems:
279
print(f"File System: {fs.name}, Modified: {fs.last_modified}")
280
281
# Create a file system
282
fs_client = service_client.create_file_system(
283
"myfilesystem",
284
metadata={"project": "data-analysis", "env": "prod"}
285
)
286
287
# Get a user delegation key for SAS token generation
288
start_time = datetime.utcnow()
289
expiry_time = start_time + timedelta(hours=1)
290
user_delegation_key = service_client.get_user_delegation_key(
291
key_start_time=start_time,
292
key_expiry_time=expiry_time
293
)
294
```