每个 Fabric 项目都可以访问 OneLake 存储,后者为存储与项目关联的文件提供了安全且可扩展的方式。 本指南介绍如何使用FabricPlatformAPIClient和底层的OneLakeClient将文件上传到您的Fabric项目。
了解 Fabric 中的物品存储
Fabric 中的每个项在 OneLake 中都有自己的专用存储区域。 此存储组织到文件夹中,主文件夹为:
- 文件 - 用于存储常规文件和文档
- 表 - 用于存储表数据
先决条件
将文件上传到您的项目之前,您需要:
- 有效的 Fabric 工作区
- 您要上传文件的现有项目
- 写入项的适当权限
创建 FabricPlatformAPIClient
首先,创建实例 FabricPlatformAPIClient :
import { getWorkloadClient } from "../controller/WorkloadClient";
import { FabricPlatformAPIClient } from "../clients/FabricPlatformAPIClient";
// Create client using the current user's context
const fabricClient = FabricPlatformAPIClient.create(getWorkloadClient());
示例:将文本文件上传到项目
下面介绍如何使用 OneLake 客户端将文本文件上传到项目:
async function uploadTextFileToItem(
workspaceId: string,
itemId: string,
fileName: string,
content: string
) {
try {
// Get the FabricPlatformAPIClient
const fabricClient = FabricPlatformAPIClient.create(getWorkloadClient());
// Access the OneLake client
const oneLakeClient = fabricClient.oneLake;
// Generate the file path in OneLake for this item
// This follows the pattern: workspaceId/itemId/Files/fileName
const filePath = oneLakeClient.constructor.getFilePath(workspaceId, itemId, fileName);
// Write the text content to the file
await oneLakeClient.writeFileAsText(filePath, content);
console.log(`Successfully uploaded ${fileName} to item ${itemId}`);
return true;
} catch (error) {
console.error("Error uploading file to item:", error);
throw error;
}
}
示例:将二进制文件上传到项
对于图像或 PDF 等二进制文件,首先需要将文件转换为 base64:
async function uploadBinaryFileToItem(
workspaceId: string,
itemId: string,
fileName: string,
fileData: ArrayBuffer // Binary file data
) {
try {
const fabricClient = FabricPlatformAPIClient.create(getWorkloadClient());
const oneLakeClient = fabricClient.oneLake;
// Convert binary data to base64
const base64Content = arrayBufferToBase64(fileData);
// Generate the file path
const filePath = oneLakeClient.constructor.getFilePath(workspaceId, itemId, fileName);
// Write the binary content to the file
await oneLakeClient.writeFileAsBase64(filePath, base64Content);
console.log(`Successfully uploaded binary file ${fileName} to item ${itemId}`);
return true;
} catch (error) {
console.error("Error uploading binary file to item:", error);
throw error;
}
}
// Helper function to convert ArrayBuffer to base64
function arrayBufferToBase64(buffer: ArrayBuffer): string {
let binary = '';
const bytes = new Uint8Array(buffer);
const len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
}
示例:从浏览器上传文件
如果要生成 Web 界面,可以使用此函数处理文件输入中的文件上传:
async function handleFileUpload(
workspaceId: string,
itemId: string,
fileInputElement: HTMLInputElement
) {
if (!fileInputElement.files || fileInputElement.files.length === 0) {
console.warn("No file selected");
return false;
}
const file = fileInputElement.files[0];
const fileName = file.name;
try {
// Read the file as ArrayBuffer
const fileBuffer = await readFileAsArrayBuffer(file);
// Upload based on file type
if (file.type.startsWith('text/')) {
// For text files, convert to string and upload as text
const textDecoder = new TextDecoder();
const textContent = textDecoder.decode(fileBuffer);
return await uploadTextFileToItem(workspaceId, itemId, fileName, textContent);
} else {
// For binary files, upload as base64
return await uploadBinaryFileToItem(workspaceId, itemId, fileName, fileBuffer);
}
} catch (error) {
console.error("Error processing file upload:", error);
throw error;
}
}
// Helper function to read file as ArrayBuffer
function readFileAsArrayBuffer(file: File): Promise<ArrayBuffer> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result as ArrayBuffer);
reader.onerror = reject;
reader.readAsArrayBuffer(file);
});
}
管理项目中的文件
上传文件后,还可以:
检查文件是否存在
async function checkFileExists(workspaceId: string, itemId: string, fileName: string) {
const fabricClient = FabricPlatformAPIClient.create(getWorkloadClient());
const filePath = fabricClient.oneLake.constructor.getFilePath(workspaceId, itemId, fileName);
return await fabricClient.oneLake.checkIfFileExists(filePath);
}
读取文件内容
async function readTextFile(workspaceId: string, itemId: string, fileName: string) {
const fabricClient = FabricPlatformAPIClient.create(getWorkloadClient());
const filePath = fabricClient.oneLake.constructor.getFilePath(workspaceId, itemId, fileName);
return await fabricClient.oneLake.readFileAsText(filePath);
}
删除文件
async function deleteFile(workspaceId: string, itemId: string, fileName: string) {
const fabricClient = FabricPlatformAPIClient.create(getWorkloadClient());
const filePath = fabricClient.oneLake.constructor.getFilePath(workspaceId, itemId, fileName);
await fabricClient.oneLake.deleteFile(filePath);
console.log(`File ${fileName} deleted successfully`);
}
文件上传的最佳做法
- 使用适当的文件格式:考虑文件的目的,并使用广受支持的格式。
- 优雅地处理错误:确保始终包括对网络问题或权限问题的错误处理。
- 验证文件大小:大型文件上传和处理可能需要更长的时间。
- 检查权限:在尝试上传之前,请确保用户具有适当的权限。
- 使用文件前缀或文件夹:对于包含许多文件的复杂项目,请考虑在子文件夹中组织它们。
使用 OneLakeClientItemWrapper
为了简化对项文件的访问,可以使用 OneLakeClientItemWrapper:
async function uploadFileWithItemWrapper(item, fileName, content) {
const fabricClient = FabricPlatformAPIClient.create(getWorkloadClient());
// Create a wrapper for simpler access to this specific item
const itemWrapper = fabricClient.oneLake.createItemWrapper({
workspaceId: item.workspaceId,
itemId: item.id
});
// Upload directly to the item (no need to specify paths)
await itemWrapper.writeFileAsText(fileName, content);
// Read the file back
const fileContent = await itemWrapper.readFileAsText(fileName);
console.log(`File uploaded and read back: ${fileContent.substring(0, 50)}...`);
}
此包装器通过自动处理完整路径构造来简化文件作。