Python Microsoft Fabric - OneLake
OneLake files read and written through the data plane, plus shortcuts for cross-workspace data.
OneLake is Fabric's unified data lake - every workspace has a filesystem in it, and every lakehouse stores its files and tables there. The connection gives you two ways in - the data plane methods read and write files directly, and shortcuts make data from other locations appear inside an item without copying it. You create a Fabric connection in the Dashboard and both are available to your services.
The data plane methods speak the same protocol as ADLS Gen2 and use their own storage-scoped token, which the connection acquires and refreshes automatically alongside the API token.
Listing files
conn.onelake_list lists the paths of a workspace's filesystem, optionally under a specific directory. Paths follow the ItemName.ItemType/Files/... layout.
# -*- coding: utf-8 -*-
# Zato
from zato.server.service import Service
class ListSalesFiles(Service):
input = 'workspace_id'
def handle(self):
# Get the connection by its Dashboard name
conn = self.microsoft.fabric['My Fabric']
# List the files of a lakehouse directory
response = conn.onelake_list(self.request.input.workspace_id, 'Sales data.Lakehouse/Files/sales')
files = []
for path in response['paths']:
files.append({
'name': path['name'],
'is_directory': path['isDirectory'],
})
self.response.payload = {'files': files}
Reading a file
conn.onelake_read returns a file's contents as bytes.
# -*- coding: utf-8 -*-
# Zato
from zato.server.service import Service
class ReadDailySales(Service):
input = 'workspace_id'
def handle(self):
conn = self.microsoft.fabric['My Fabric']
# Read the file
data = conn.onelake_read(self.request.input.workspace_id, 'Sales data.Lakehouse/Files/sales/daily.csv')
# It arrives as bytes
text = data.decode('utf-8')
self.response.payload = {'size': len(data), 'first_line': text.splitlines()[0]}
Writing a file
conn.onelake_write creates or overwrites a file - the create, append and flush steps of the underlying protocol are handled for you.
# -*- coding: utf-8 -*-
# Zato
from zato.server.service import Service
class WriteExportFile(Service):
input = 'workspace_id'
def handle(self):
conn = self.microsoft.fabric['My Fabric']
# The data to write
data = 'order_id,amount\nORD-001,250.00\n'.encode('utf-8')
# Write it to the lakehouse
conn.onelake_write(self.request.input.workspace_id, 'Sales data.Lakehouse/Files/exports/orders.csv', data)
self.response.payload = {'status': 'written', 'bytes': len(data)}
Deleting a file
conn.onelake_delete removes a file - for instance, cleaning up processed input files.
# -*- coding: utf-8 -*-
# Zato
from zato.server.service import Service
class DeleteProcessedFile(Service):
input = 'workspace_id', 'file_path'
def handle(self):
conn = self.microsoft.fabric['My Fabric']
conn.onelake_delete(self.request.input.workspace_id, self.request.input.file_path)
self.response.payload = {'status': 'deleted'}
Listing shortcuts
Shortcuts make data from another workspace - or from external storage like ADLS or S3 - appear inside an item without copying it. conn.list_shortcuts returns the ones an item has.
# -*- coding: utf-8 -*-
# Zato
from zato.server.service import Service
class ListLakehouseShortcuts(Service):
input = 'workspace_id', 'lakehouse_id'
def handle(self):
conn = self.microsoft.fabric['My Fabric']
response = conn.list_shortcuts(self.request.input.workspace_id, self.request.input.lakehouse_id)
shortcuts = [shortcut['name'] for shortcut in response['value']]
self.response.payload = {'shortcuts': shortcuts}
Creating a shortcut
conn.create_shortcut links data in - here, a table from another workspace's lakehouse becomes readable in this one, which is the standard cross-workspace access pattern.
# -*- coding: utf-8 -*-
# Zato
from zato.server.service import Service
class LinkFinanceData(Service):
input = 'workspace_id', 'lakehouse_id', 'source_workspace_id', 'source_item_id'
def handle(self):
conn = self.microsoft.fabric['My Fabric']
# Point the shortcut at the source lakehouse's table
shortcut = {
'name': 'finance-transactions',
'path': 'Tables',
'target': {
'oneLake': {
'workspaceId': self.request.input.source_workspace_id,
'itemId': self.request.input.source_item_id,
'path': 'Tables/transactions',
}
}
}
conn.create_shortcut(self.request.input.workspace_id, self.request.input.lakehouse_id, shortcut)
self.response.payload = {'status': 'created'}
Deleting a shortcut
conn.delete_shortcut removes the link - the underlying data stays where it always was.
# -*- coding: utf-8 -*-
# Zato
from zato.server.service import Service
class UnlinkFinanceData(Service):
input = 'workspace_id', 'lakehouse_id'
def handle(self):
conn = self.microsoft.fabric['My Fabric']
conn.delete_shortcut(
self.request.input.workspace_id,
self.request.input.lakehouse_id,
'Tables',
'finance-transactions',
)
self.response.payload = {'status': 'deleted'}