Skip to content

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

feat: support external S3 endpoint for batch exports (#6055)

Steffen Schmitz2e84feeab77a
diff --git a/packages/shared/src/server/services/StorageService.ts b/packages/shared/src/server/services/StorageService.ts
index 78f7d4a93b..f55c1895b2 100644
--- a/packages/shared/src/server/services/StorageService.ts
+++ b/packages/shared/src/server/services/StorageService.ts
@@ -51,11 +51,22 @@ export interface StorageService {
}
export class StorageServiceFactory {
+ /**
+ * Get an instance of the StorageService
+ * @param params.accessKeyId - Access key ID
+ * @param params.secretAccessKey - Secret access key
+ * @param params.bucketName - Bucket name to store files
+ * @param params.endpoint - Endpoint - Endpoint to an S3 compatible API (or Azure Blob Storage)
+ * @param params.externalEndpoint - External endpoint to replace the internal endpoint in the signed URL.
+ * @param params.region - Region in which the bucket resides
+ * @param params.forcePathStyle - Add bucket name into the path instead of the domain name. Mainly used for MinIO.
+ */
public static getInstance(params: {
accessKeyId: string | undefined;
secretAccessKey: string | undefined;
bucketName: string;
endpoint: string | undefined;
+ externalEndpoint?: string | undefined;
region: string | undefined;
forcePathStyle: boolean;
}): StorageService {
@@ -69,22 +80,25 @@ export class StorageServiceFactory {
class AzureBlobStorageService implements StorageService {
private client: ContainerClient;
private container: string;
+ private externalEndpoint: string | undefined;
constructor(params: {
accessKeyId: string | undefined;
secretAccessKey: string | undefined;
bucketName: string;
endpoint: string | undefined;
+ externalEndpoint?: string | undefined;
region: string | undefined;
forcePathStyle: boolean;
}) {
- const { accessKeyId, secretAccessKey, endpoint } = params;
+ const { accessKeyId, secretAccessKey, endpoint, externalEndpoint } = params;
if (!accessKeyId || !secretAccessKey || !endpoint) {
throw new Error(
`Endpoint, account and account key must be configured to use Azure Blob Storage`,
);
}
+ this.externalEndpoint = externalEndpoint;
const sharedKeyCredential = new StorageSharedKeyCredential(
accessKeyId,
secretAccessKey,
@@ -258,13 +272,20 @@ class AzureBlobStorageService implements StorageService {
await this.createContainerIfNotExists();
const blockBlobClient = this.client.getBlockBlobClient(fileName);
- return blockBlobClient.generateSasUrl({
+ let url = await blockBlobClient.generateSasUrl({
permissions: BlobSASPermissions.parse("r"),
expiresOn: new Date(Date.now() + ttlSeconds * 1000),
contentDisposition: asAttachment
? `attachment; filename="${fileName}"`
: undefined,
});
+
+ // Replace internal endpoint with external endpoint if configured
+ if (this.externalEndpoint && url.includes(this.client.url)) {
+ url = url.replace(this.client.url, this.externalEndpoint);
+ }
+
+ return url;
} catch (err) {
logger.error(
`Failed to generate presigned URL for Azure Blob Storage ${fileName}`,
@@ -286,11 +307,18 @@ class AzureBlobStorageService implements StorageService {
await this.createContainerIfNotExists();
const blockBlobClient = this.client.getBlockBlobClient(path);
- return blockBlobClient.generateSasUrl({
+ let url = await blockBlobClient.generateSasUrl({
permissions: BlobSASPermissions.parse("w"),
expiresOn: new Date(Date.now() + ttlSeconds * 1000),
contentType: contentType,
});
+
+ // Replace internal endpoint with external endpoint if configured
+ if (this.externalEndpoint && url.includes(this.client.url)) {
+ url = url.replace(this.client.url, this.externalEndpoint);
+ }
+
+ return url;
} catch (err) {
logger.error(
`Failed to generate presigned upload URL for Azure Blob Storage ${path}`,
@@ -306,12 +334,15 @@ class AzureBlobStorageService implements StorageService {
class S3StorageService implements StorageService {
private client: S3Client;
private bucketName: string;
+ private endpoint: string | undefined;
+ private externalEndpoint: string | undefined;
constructor(params: {
accessKeyId: string | undefined;
secretAccessKey: string | undefined;
bucketName: string;
endpoint: string | undefined;
+ externalEndpoint?: string | undefined;
region: string | undefined;
forcePathStyle: boolean;
}) {
@@ -337,6 +368,8 @@ class S3StorageService implements StorageService {
},
});
this.bucketName = params.bucketName;
+ this.endpoint = params.endpoint;
+ this.externalEndpoint = params.externalEndpoint;
}
public async uploadFile({
@@ -425,7 +458,7 @@ class S3StorageService implements StorageService {
asAttachment: boolean = true,
): Promise<string> {
try {
- return await getSignedUrl(
+ let url = await getSignedUrl(
this.client,
new GetObjectCommand({
Bucket: this.bucketName,
@@ -436,6 +469,17 @@ class S3StorageService implements StorageService {
}),
{ expiresIn: ttlSeconds },
);
+
+ // Replace internal endpoint with external endpoint if configured
+ if (
+ this.externalEndpoint &&
+ this.endpoint &&
+ url.includes(this.endpoint)
+ ) {
+ url = url.replace(this.endpoint, this.externalEndpoint);
+ }
+
+ return url;
} catch (err) {
logger.error(`Failed to generate presigned URL for ${fileName}`, err);
throw Error("Failed to generate signed URL");
@@ -482,7 +526,7 @@ class S3StorageService implements StorageService {
}): Promise<string> {
const { path, ttlSeconds, contentType, contentLength, sha256Hash } = params;
- return await getSignedUrl(
+ let url = await getSignedUrl(
this.client,
new PutObjectCommand({
Bucket: this.bucketName,
@@ -497,5 +541,12 @@ class S3StorageService implements StorageService {
unhoistableHeaders: new Set(["x-amz-checksum-sha256"]),
},
);
+
+ // Replace internal endpoint with external endpoint if configured
+ if (this.externalEndpoint && this.endpoint && url.includes(this.endpoint)) {
+ url = url.replace(this.endpoint, this.externalEndpoint);
+ }
+
+ return url;
}
}
diff --git a/web/src/env.mjs b/web/src/env.mjs
index af150fae3f..d973967eeb 100644
--- a/web/src/env.mjs
+++ b/web/src/env.mjs
@@ -170,6 +170,7 @@ export const env = createEnv({
LANGFUSE_S3_BATCH_EXPORT_PREFIX: z.string().default(""),
LANGFUSE_S3_BATCH_EXPORT_REGION: z.string().optional(),
LANGFUSE_S3_BATCH_EXPORT_ENDPOINT: z.string().optional(),
+ LANGFUSE_S3_BATCH_EXPORT_EXTERNAL_ENDPOINT: z.string().optional(),
LANGFUSE_S3_BATCH_EXPORT_ACCESS_KEY_ID: z.string().optional(),
LANGFUSE_S3_BATCH_EXPORT_SECRET_ACCESS_KEY: z.string().optional(),
LANGFUSE_S3_BATCH_EXPORT_FORCE_PATH_STYLE: z
@@ -437,7 +438,8 @@ export const env = createEnv({
AUTH_CUSTOM_ID_TOKEN: process.env.AUTH_CUSTOM_ID_TOKEN,
AUTH_WORKOS_CLIENT_ID: process.env.AUTH_WORKOS_CLIENT_ID,
AUTH_WORKOS_CLIENT_SECRET: process.env.AUTH_WORKOS_CLIENT_SECRET,
- AUTH_WORKOS_ALLOW_ACCOUNT_LINKING: process.env.AUTH_WORKOS_ALLOW_ACCOUNT_LINKING,
+ AUTH_WORKOS_ALLOW_ACCOUNT_LINKING:
+ process.env.AUTH_WORKOS_ALLOW_ACCOUNT_LINKING,
AUTH_WORKOS_ORGANIZATION_ID: process.env.AUTH_WORKOS_ORGANIZATION_ID,
AUTH_WORKOS_CONNECTION_ID: process.env.AUTH_WORKOS_CONNECTION_ID,
AUTH_IGNORE_ACCOUNT_FIELDS: process.env.AUTH_IGNORE_ACCOUNT_FIELDS,
@@ -467,6 +469,8 @@ export const env = createEnv({
process.env.LANGFUSE_S3_BATCH_EXPORT_REGION,
LANGFUSE_S3_BATCH_EXPORT_ENDPOINT:
process.env.LANGFUSE_S3_BATCH_EXPORT_ENDPOINT,
+ LANGFUSE_S3_BATCH_EXPORT_EXTERNAL_ENDPOINT:
+ process.env.LANGFUSE_S3_BATCH_EXPORT_EXTERNAL_ENDPOINT,
LANGFUSE_S3_BATCH_EXPORT_ACCESS_KEY_ID:
process.env.LANGFUSE_S3_BATCH_EXPORT_ACCESS_KEY_ID,
LANGFUSE_S3_BATCH_EXPORT_SECRET_ACCESS_KEY:
diff --git a/worker/src/__tests__/storageservice.test.ts b/worker/src/__tests__/storageservice.test.ts
index c6c2dc2f12..8c798b7de0 100644
--- a/worker/src/__tests__/storageservice.test.ts
+++ b/worker/src/__tests__/storageservice.test.ts
@@ -8,7 +8,9 @@ import {
describe("StorageService", () => {
let storageService: StorageService;
+ let storageServiceWithExternalEndpoint: StorageService;
const baseUrl = `${env.LANGFUSE_S3_EVENT_UPLOAD_ENDPOINT}/${env.LANGFUSE_S3_EVENT_UPLOAD_BUCKET}`;
+ const externalEndpoint = "https://external-endpoint.example.com";
beforeAll(() => {
storageService = StorageServiceFactory.getInstance({
@@ -19,6 +21,16 @@ describe("StorageService", () => {
region: env.LANGFUSE_S3_EVENT_UPLOAD_REGION,
forcePathStyle: env.LANGFUSE_S3_EVENT_UPLOAD_FORCE_PATH_STYLE === "true",
});
+
+ storageServiceWithExternalEndpoint = StorageServiceFactory.getInstance({
+ accessKeyId: env.LANGFUSE_S3_EVENT_UPLOAD_ACCESS_KEY_ID,
+ secretAccessKey: env.LANGFUSE_S3_EVENT_UPLOAD_SECRET_ACCESS_KEY,
+ bucketName: env.LANGFUSE_S3_EVENT_UPLOAD_BUCKET,
+ endpoint: env.LANGFUSE_S3_EVENT_UPLOAD_ENDPOINT,
+ externalEndpoint,
+ region: env.LANGFUSE_S3_EVENT_UPLOAD_REGION,
+ forcePathStyle: env.LANGFUSE_S3_EVENT_UPLOAD_FORCE_PATH_STYLE === "true",
+ });
});
test("uploadFile should upload a file and return a signed URL", async () => {
@@ -87,4 +99,127 @@ describe("StorageService", () => {
const fileNames = files.map((f) => f.file);
expect(fileNames).not.toContain(fileName1);
});
+
+ test("getSignedUrl should return URL with internal endpoint when no external endpoint is configured", 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,
+ expiresInSeconds,
+ });
+
+ // 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 external endpoint when configured", async () => {
+ // Setup
+ const fileName = `${randomUUID()}.txt`;
+ const fileType = "text/plain";
+ const data = "Hello, world!";
+ const expiresInSeconds = 3600;
+
+ // Upload a file
+ await storageServiceWithExternalEndpoint.uploadFile({
+ fileName,
+ fileType,
+ data,
+ expiresInSeconds,
+ });
+
+ // When
+ const signedUrl = await storageServiceWithExternalEndpoint.getSignedUrl(
+ fileName,
+ expiresInSeconds,
+ );
+
+ // Then
+ expect(signedUrl).toContain("external-endpoint.example.com");
+ expect(signedUrl).not.toContain(env.LANGFUSE_S3_EVENT_UPLOAD_ENDPOINT);
+ });
+
+ test("getSignedUploadUrl should return URL with internal endpoint when no external endpoint is configured", async () => {
+ // Setup
+ const path = `${randomUUID()}.txt`;
+ const ttlSeconds = 3600;
+ const sha256Hash = "dummy-hash";
+ const contentType = "text/plain";
+ const contentLength = 100;
+
+ // When
+ const signedUrl = await storageService.getSignedUploadUrl({
+ path,
+ ttlSeconds,
+ sha256Hash,
+ contentType,
+ contentLength,
+ });
+
+ // Then
+ expect(signedUrl).toContain(env.LANGFUSE_S3_EVENT_UPLOAD_ENDPOINT);
+ expect(signedUrl).not.toContain("external-endpoint.example.com");
+ });
+
+ test("getSignedUploadUrl should return URL with external endpoint when configured", async () => {
+ // Setup
+ const path = `${randomUUID()}.txt`;
+ const ttlSeconds = 3600;
+ const sha256Hash = "dummy-hash";
+ const contentType = "text/plain";
+ const contentLength = 100;
+
+ // When
+ const signedUrl =
+ await storageServiceWithExternalEndpoint.getSignedUploadUrl({
+ path,
+ ttlSeconds,
+ sha256Hash,
+ contentType,
+ contentLength,
+ });
+
+ // Then
+ expect(signedUrl).toContain("external-endpoint.example.com");
+ expect(signedUrl).not.toContain(env.LANGFUSE_S3_EVENT_UPLOAD_ENDPOINT);
+ });
+
+ test("uploadFile should return signed URL with external endpoint when configured", async () => {
+ // Setup
+ const fileName = `${randomUUID()}.txt`;
+ const fileType = "text/plain";
+ const data = "Hello, external world!";
+ const expiresInSeconds = 3600;
+
+ // When
+ const result = await storageServiceWithExternalEndpoint.uploadFile({
+ fileName,
+ fileType,
+ data,
+ expiresInSeconds,
+ });
+
+ // Then
+ expect(result.signedUrl).toContain("external-endpoint.example.com");
+ expect(result.signedUrl).not.toContain(
+ env.LANGFUSE_S3_EVENT_UPLOAD_ENDPOINT,
+ );
+
+ // Verify the file was uploaded correctly
+ const file = await storageService.download(fileName);
+ expect(file).toBe(data);
+ });
});
diff --git a/worker/src/env.ts b/worker/src/env.ts
index 7b430e6b3c..1e6c9e4311 100644
--- a/worker/src/env.ts
+++ b/worker/src/env.ts
@@ -22,6 +22,7 @@ const EnvSchema = z.object({
LANGFUSE_S3_BATCH_EXPORT_PREFIX: z.string().default(""),
LANGFUSE_S3_BATCH_EXPORT_REGION: z.string().optional(),
LANGFUSE_S3_BATCH_EXPORT_ENDPOINT: z.string().optional(),
+ LANGFUSE_S3_BATCH_EXPORT_EXTERNAL_ENDPOINT: z.string().optional(),
LANGFUSE_S3_BATCH_EXPORT_ACCESS_KEY_ID: z.string().optional(),
LANGFUSE_S3_BATCH_EXPORT_SECRET_ACCESS_KEY: z.string().optional(),
LANGFUSE_S3_BATCH_EXPORT_FORCE_PATH_STYLE: z
diff --git a/worker/src/features/batchExport/handleBatchExportJob.ts b/worker/src/features/batchExport/handleBatchExportJob.ts
index bb1cab0372..12ebd3204b 100644
--- a/worker/src/features/batchExport/handleBatchExportJob.ts
+++ b/worker/src/features/batchExport/handleBatchExportJob.ts
@@ -501,6 +501,7 @@ export const handleBatchExportJob = async (
accessKeyId: env.LANGFUSE_S3_BATCH_EXPORT_ACCESS_KEY_ID,
secretAccessKey: env.LANGFUSE_S3_BATCH_EXPORT_SECRET_ACCESS_KEY,
endpoint: env.LANGFUSE_S3_BATCH_EXPORT_ENDPOINT,
+ externalEndpoint: env.LANGFUSE_S3_BATCH_EXPORT_EXTERNAL_ENDPOINT,
region: env.LANGFUSE_S3_BATCH_EXPORT_REGION,
forcePathStyle: env.LANGFUSE_S3_BATCH_EXPORT_FORCE_PATH_STYLE === "true",
}).uploadFile({