Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

perf: use uploadStream for Readable uploads to azure blob storage (#10730)

Steffen Schmitz14bdb59edd6d
diff --git a/packages/shared/src/server/services/StorageService.ts b/packages/shared/src/server/services/StorageService.ts
index cbbebf01f1..0c6057decc 100644
--- a/packages/shared/src/server/services/StorageService.ts
+++ b/packages/shared/src/server/services/StorageService.ts
@@ -198,36 +198,24 @@ class AzureBlobStorageService implements StorageService {
}
public async uploadFile(params: UploadFile): Promise<void> {
- const { fileName, data } = params;
+ const { fileName, fileType, data, partSize } = params;
try {
await this.createContainerIfNotExists();
const blockBlobClient = this.client.getBlockBlobClient(fileName);
if (typeof data === "string") {
- await blockBlobClient.upload(data, data.length);
+ await blockBlobClient.upload(data, data.length, {
+ blobHTTPHeaders: { blobContentType: fileType },
+ });
} else if (data instanceof Readable) {
- const blockIds = [];
- for await (const chunk of data) {
- // Azure requires block IDs to be base64 strings of the same length
- // Use a fixed format with padded index to ensure consistent length
- const blockIdStr: string = `block-${blockIds.length.toString().padStart(10, "0")}`;
- const blockId = Buffer.from(blockIdStr).toString("base64");
-
- const bufferChunk = Buffer.isBuffer(chunk)
- ? chunk
- : Buffer.from(chunk);
-
- await blockBlobClient.stageBlock(
- blockId,
- bufferChunk,
- bufferChunk.length,
- );
- blockIds.push(blockId);
- }
- if (blockIds.length > 0) {
- await blockBlobClient.commitBlockList(blockIds);
- }
+ // bufferSize controls the block size (default 8MB supports ~800GB files)
+ const bufferSize = partSize ?? 8 * 1024 * 1024; // Default 8MB per block
+ const maxConcurrency = 5; // Default value
+
+ await blockBlobClient.uploadStream(data, bufferSize, maxConcurrency, {
+ blobHTTPHeaders: { blobContentType: fileType },
+ });
} else {
throw new Error("Unsupported data type. Must be Readable or string.");
}
diff --git a/worker/src/__tests__/storageservice.test.ts b/worker/src/__tests__/storageservice.test.ts
index 0b4a3a6bd7..f27206e88d 100644
--- a/worker/src/__tests__/storageservice.test.ts
+++ b/worker/src/__tests__/storageservice.test.ts
@@ -6,6 +6,8 @@ import {
StorageServiceFactory,
} from "@langfuse/shared/src/server";
+const { Readable } = require("stream");
+
describe("StorageService", () => {
let storageService: StorageService;
let storageServiceWithExternalEndpoint: StorageService;
@@ -100,6 +102,31 @@ describe("StorageService", () => {
expect(fileNames).not.toContain(fileName1);
});
+ test("uploadFile should successfully process a Readable entity", async () => {
+ // Setup
+ const fileName = `${randomUUID()}.txt`;
+ const fileType = "text/plain";
+ const data = "Hello, world!";
+ const expiresInSeconds = 3600;
+
+ // Upload a file
+ await storageService.uploadFile({
+ fileName,
+ fileType,
+ data: Readable.from(data),
+ });
+
+ // When
+ const signedUrl = await storageService.getSignedUrl(
+ fileName,
+ expiresInSeconds,
+ );
+
+ // Then
+ expect(signedUrl).toContain(env.LANGFUSE_S3_EVENT_UPLOAD_ENDPOINT);
+ expect(signedUrl).not.toContain("external-endpoint.example.com");
+ });
+
test("getSignedUrl should return URL with internal endpoint when no external endpoint is configured", async () => {
// Setup
const fileName = `${randomUUID()}.txt`;