azure-storage-file-datalake-py
microsoft/skills
Gerencie o Azure Data Lake Storage Gen2 com o SDK do Python para sistemas de arquivos hierárquicos, análise de big data e operações de arquivos/diretórios.
...Expandir tudoSDK do Azure Data Lake Storage Gen2 para Python
Sistema de arquivos hierárquico para cargas de trabalho de análise de big data.
Instalação
pip install azure-storage-file-datalake azure-identity
Variáveis de Ambiente
AZURE_STORAGE_ACCOUNT_URL=https://<account>.dfs.core.windows.net # Obrigatório para todos os métodos de autenticação
AZURE_TOKEN_CREDENTIALS=prod # Obrigatório apenas se DefaultAzureCredential for usado em produção
</account>Autenticação e Ciclo de Vida
🔑 Duas regras se aplicam a todas as amostras de código abaixo:
- Prefira
DefaultAzureCredential. Ele funciona localmente (Azure CLI / VS Code / Developer CLI) e no Azure (identidade gerenciada, identidade de carga de trabalho) sem alteração de código. Evite strings de conexão, chaves de conta/API — elas contornam a auditoria e a rotação do Entra.
- Desenvolvimento local:
DefaultAzureCredentialfunciona conforme o padrão.- Produção: defina
AZURE_TOKEN_CREDENTIALS=prod(ouAZURE_TOKEN_CREDENTIALS=<specific_credential></specific_credential>) para restringir a cadeia de credenciais a credenciais seguras para produção.- Envolva cada cliente em um gerenciador de contexto para que transportes HTTP, soquetes e caches de token sejam liberados de forma determinística:
- Síncrono:
with <client>(...) as client:</client>- Assíncrono:
async with <client>(...) as client:</client>easync with DefaultAzureCredential() as credential:(deazure.identity.aio)Os trechos de código podem abreviar essa configuração, mas o código de produção deve sempre seguir ambas as regras.
from azure.identity import DefaultAzureCredential, ManagedIdentityCredential
from azure.storage.filedatalake import DataLakeServiceClient
# Desenvolvimento local: DefaultAzureCredential. Produção: defina AZURE_TOKEN_CREDENTIALS=prod ou AZURE_TOKEN_CREDENTIALS=<specific_credential>
credential = DefaultAzureCredential(require_envvar=True)
# Ou use uma credencial específica diretamente em produção:
# Consulte https://learn.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python#credential-classes
# credential = ManagedIdentityCredential()
account_url = "https://<account>.dfs.core.windows.net"
with DataLakeServiceClient(account_url=account_url, credential=credential) as service_client:
# Use service_client aqui (consulte as seções seguintes para operações)
...
</account></specific_credential>Hierarquia de Clientes
| Cliente | Finalidade |
|---|---|
| `DataLakeServiceClient` | Operações em nível de conta |
| `FileSystemClient` | Operações de contêiner (sistema de arquivos) |
| `DataLakeDirectoryClient` | Operações de diretório |
| `DataLakeFileClient` | Operações de arquivo |
Operações do Sistema de Arquivos
# Criar sistema de arquivos (contêiner)
file_system_client = service_client.create_file_system("myfilesystem")
# Obter existente
file_system_client = service_client.get_file_system_client("myfilesystem")
# Excluir
service_client.delete_file_system("myfilesystem")
# Listar sistemas de arquivos
for fs in service_client.list_file_systems():
print(fs.name)
Operações de Diretório
file_system_client = service_client.get_file_system_client("myfilesystem")
# Criar diretório
directory_client = file_system_client.create_directory("mydir")
# Criar diretórios aninhados
directory_client = file_system_client.create_directory("path/to/nested/dir")
# Obter cliente de diretório
directory_client = file_system_client.get_directory_client("mydir")
# Excluir diretório
directory_client.delete_directory()
# Renomear/mover diretório
directory_client.rename_directory(new_name="myfilesystem/newname")
Operações de Arquivo
Carregar Arquivo
# Obter cliente de arquivo
file_client = file_system_client.get_file_client("path/to/file.txt")
# Carregar de arquivo local
with open("local-file.txt", "rb") as data:
file_client.upload_data(data, overwrite=True)
# Carregar bytes
file_client.upload_data(b"Hello, Data Lake!", overwrite=True)
# Anexar dados (para arquivos grandes)
file_client.append_data(data=b"chunk1", offset=0, length=6)
file_client.append_data(data=b"chunk2", offset=6, length=6)
file_client.flush_data(12) # Confirmar os dados
Baixar Arquivo
file_client = file_system_client.get_file_client("path/to/file.txt")
# Baixar todo o conteúdo
download = file_client.download_file()
content = download.readall()
# Baixar para arquivo
with open("downloaded.txt", "wb") as f:
download = file_client.download_file()
download.readinto(f)
# Baixar intervalo
download = file_client.download_file(offset=0, length=100)
Excluir Arquivo
file_client.delete_file()
Listar Conteúdos
# Listar caminhos (arquivos e diretórios)
for path in file_system_client.get_paths():
print(f"{'DIR' if path.is_directory else 'FILE'}: {path.name}")
# Listar caminhos no diretório
for path in file_system_client.get_paths(path="mydir"):
print(path.name)
# Listagem recursiva
for path in file_system_client.get_paths(path="mydir", recursive=True):
print(path.name)
Propriedades de Arquivo/Diretório
# Obter propriedades
properties = file_client.get_file_properties()
print(f"Tamanho: {properties.size}")
print(f"Última modificação: {properties.last_modified}")
# Definir metadados
file_client.set_metadata(metadata={"processed": "true"})
Controle de Acesso (ACL)
# Obter ACL
acl = directory_client.get_access_control()
print(f"Proprietário: {acl['owner']}")
print(f"Permissões: {acl['permissions']}")
# Definir ACL
directory_client.set_access_control(
owner="user-id",
permissions="rwxr-x---"
)
# Atualizar entradas de ACL
from azure.storage.filedatalake import AccessControlChangeResult
directory_client.update_access_control_recursive(
acl="user:user-id:rwx"
)
Cliente Assíncrono
from azure.storage.filedatalake.aio import DataLakeServiceClient
from azure.identity.aio import DefaultAzureCredential
async def datalake_operations():
async with DefaultAzureCredential() as credential:
async with DataLakeServiceClient(
account_url="https://<account>.dfs.core.windows.net",
credential=credential
) as service_client:
file_system_client = service_client.get_file_system_client("myfilesystem")
file_client = file_system_client.get_file_client("test.txt")
await file_client.upload_data(b"async content", overwrite=True)
download = await file_client.download_file()
content = await download.readall()
import asyncio
asyncio.run(datalake_operations())
</account>Melhores Práticas
- Escolha síncrono OU assíncrono e mantenha a consistência. Não misture clientes síncronos
azure.storage.filedatalakecom clientes assíncronosazure.storage.filedatalake.aiono mesmo caminho de chamada. Escolha um modo por módulo. - Sempre use gerenciadores de contexto para clientes e credenciais assíncronas. Envolva cada cliente em
with DataLakeServiceClient(...) as client:(síncrono) ouasync with DataLakeServiceClient(...) as client:(assíncrono). ParaDefaultAzureCredentialassíncrono deazure.identity.aio, também useasync with credential:para garantir que tokens e transportes sejam limpos. - Use
DefaultAzureCredentialpara autenticação portátil entre desenvolvimento local e Azure (evite strings de conexão/chaves de API quando possível). - Use namespace hierárquico para semântica de sistema de arquivos
- Use
append_data+flush_datapara uploads de arquivos grandes - Defina ACLs em nível de diretório e herde para os filhos
- Use cliente assíncrono para cenários de alta taxa de transferência
- Use
get_pathscomrecursive=Truepara listagem completa de diretórios - Defina metadados para atributos personalizados de arquivo
- Considere a API Blob para casos de uso simples de armazenamento de objetos
---
name: azure-storage-file-datalake-py
description: Manage Azure Data Lake Storage Gen2 with Python SDK for hierarchical file systems, big data analytics, and file/directory operations.
license: MIT
---
# Azure Data Lake Storage Gen2 SDK for Python
Hierarchical file system for big data analytics workloads.
## Installation
```bash
pip install azure-storage-file-datalake azure-identity
```
## Environment Variables
```bash
AZURE_STORAGE_ACCOUNT_URL=https://<account>.dfs.core.windows.net # Required for all auth methods
AZURE_TOKEN_CREDENTIALS=prod # Required only if DefaultAzureCredential is used in production
```
## Authentication & Lifecycle
> **🔑 Two rules apply to every code sample below:**
>
> 1. **Prefer `DefaultAzureCredential`.** It works locally (Azure CLI / VS Code / Developer CLI) and in Azure (managed identity, workload identity) with no code change. Avoid connection strings, account/API keys — they bypass Entra audit and rotation.
> - Local dev: `DefaultAzureCredential` works as-is.
> - Production: set `AZURE_TOKEN_CREDENTIALS=prod` (or `AZURE_TOKEN_CREDENTIALS=<specific_credential>`) to constrain the credential chain to production-safe credentials.
> 2. **Wrap every client in a context manager** so HTTP transports, sockets, and token caches are released deterministically:
> - Sync: `with <Client>(...) as client:`
> - Async: `async with <Client>(...) as client:` **and** `async with DefaultAzureCredential() as credential:` (from `azure.identity.aio`)
>
> Snippets may abbreviate this setup, but production code should always follow both rules.
```python
from azure.identity import DefaultAzureCredential, ManagedIdentityCredential
from azure.storage.filedatalake import DataLakeServiceClient
# Local dev: DefaultAzureCredential. Production: set AZURE_TOKEN_CREDENTIALS=prod or AZURE_TOKEN_CREDENTIALS=<specific_credential>
credential = DefaultAzureCredential(require_envvar=True)
# Or use a specific credential directly in production:
# See https://learn.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python#credential-classes
# credential = ManagedIdentityCredential()
account_url = "https://<account>.dfs.core.windows.net"
with DataLakeServiceClient(account_url=account_url, credential=credential) as service_client:
# Use service_client here (see following sections for operations)
...
```
## Client Hierarchy
| Client | Purpose |
|--------|---------|
| `DataLakeServiceClient` | Account-level operations |
| `FileSystemClient` | Container (file system) operations |
| `DataLakeDirectoryClient` | Directory operations |
| `DataLakeFileClient` | File operations |
## File System Operations
```python
# Create file system (container)
file_system_client = service_client.create_file_system("myfilesystem")
# Get existing
file_system_client = service_client.get_file_system_client("myfilesystem")
# Delete
service_client.delete_file_system("myfilesystem")
# List file systems
for fs in service_client.list_file_systems():
print(fs.name)
```
## Directory Operations
```python
file_system_client = service_client.get_file_system_client("myfilesystem")
# Create directory
directory_client = file_system_client.create_directory("mydir")
# Create nested directories
directory_client = file_system_client.create_directory("path/to/nested/dir")
# Get directory client
directory_client = file_system_client.get_directory_client("mydir")
# Delete directory
directory_client.delete_directory()
# Rename/move directory
directory_client.rename_directory(new_name="myfilesystem/newname")
```
## File Operations
### Upload File
```python
# Get file client
file_client = file_system_client.get_file_client("path/to/file.txt")
# Upload from local file
with open("local-file.txt", "rb") as data:
file_client.upload_data(data, overwrite=True)
# Upload bytes
file_client.upload_data(b"Hello, Data Lake!", overwrite=True)
# Append data (for large files)
file_client.append_data(data=b"chunk1", offset=0, length=6)
file_client.append_data(data=b"chunk2", offset=6, length=6)
file_client.flush_data(12) # Commit the data
```
### Download File
```python
file_client = file_system_client.get_file_client("path/to/file.txt")
# Download all content
download = file_client.download_file()
content = download.readall()
# Download to file
with open("downloaded.txt", "wb") as f:
download = file_client.download_file()
download.readinto(f)
# Download range
download = file_client.download_file(offset=0, length=100)
```
### Delete File
```python
file_client.delete_file()
```
## List Contents
```python
# List paths (files and directories)
for path in file_system_client.get_paths():
print(f"{'DIR' if path.is_directory else 'FILE'}: {path.name}")
# List paths in directory
for path in file_system_client.get_paths(path="mydir"):
print(path.name)
# Recursive listing
for path in file_system_client.get_paths(path="mydir", recursive=True):
print(path.name)
```
## File/Directory Properties
```python
# Get properties
properties = file_client.get_file_properties()
print(f"Size: {properties.size}")
print(f"Last modified: {properties.last_modified}")
# Set metadata
file_client.set_metadata(metadata={"processed": "true"})
```
## Access Control (ACL)
```python
# Get ACL
acl = directory_client.get_access_control()
print(f"Owner: {acl['owner']}")
print(f"Permissions: {acl['permissions']}")
# Set ACL
directory_client.set_access_control(
owner="user-id",
permissions="rwxr-x---"
)
# Update ACL entries
from azure.storage.filedatalake import AccessControlChangeResult
directory_client.update_access_control_recursive(
acl="user:user-id:rwx"
)
```
## Async Client
```python
from azure.storage.filedatalake.aio import DataLakeServiceClient
from azure.identity.aio import DefaultAzureCredential
async def datalake_operations():
async with DefaultAzureCredential() as credential:
async with DataLakeServiceClient(
account_url="https://<account>.dfs.core.windows.net",
credential=credential
) as service_client:
file_system_client = service_client.get_file_system_client("myfilesystem")
file_client = file_system_client.get_file_client("test.txt")
await file_client.upload_data(b"async content", overwrite=True)
download = await file_client.download_file()
content = await download.readall()
import asyncio
asyncio.run(datalake_operations())
```
## Best Practices
1. **Pick sync OR async and stay consistent.** Do not mix `azure.storage.filedatalake` sync clients with `azure.storage.filedatalake.aio` async clients in the same call path. Choose one mode per module.
2. **Always use context managers for clients and async credentials.** Wrap every client in `with DataLakeServiceClient(...) as client:` (sync) or `async with DataLakeServiceClient(...) as client:` (async). For async `DefaultAzureCredential` from `azure.identity.aio`, also use `async with credential:` so tokens and transports are cleaned up.
3. **Use `DefaultAzureCredential`** for portable auth across local dev and Azure (avoid connection strings / API keys when possible).
4. **Use hierarchical namespace** for file system semantics
5. **Use `append_data` + `flush_data`** for large file uploads
6. **Set ACLs at directory level** and inherit to children
7. **Use async client** for high-throughput scenarios
8. **Use `get_paths` with `recursive=True`** for full directory listing
9. **Set metadata** for custom file attributes
10. **Consider Blob API** for simple object storage use cases
Todos os arquivos
1 arquivosInstalar azure-storage-file-datalake-py
Baixe e extraia os arquivos de habilidade para o diretório .claude/skills/.
Baixar ZIPClone o repositório e copie os arquivos da habilidade para o seu projeto.
git clone https://github.com/microsoft/skills/tree/main/.github/plugins/azure-sdk-python/skills/azure-storage-file-datalake-py # Copy SKILL.md to your .claude/skills/ directory
Copiar





Lar
