Skip to content

Commit

Permalink
fix: s3 link ttl (#2370)
Browse files Browse the repository at this point in the history
  • Loading branch information
hassiebp committed Jun 19, 2024
1 parent 2a30f54 commit 65b4429
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions .env.prod.example
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ LANGFUSE_CSP_ENFORCE_HTTPS="true"
# S3_SECRET_ACCESS_KEY=
# S3_BUCKET_NAME=
# S3_REGION=
# BATCH_EXPORT_DOWNLOAD_LINK_EXPIRATION_HOURS=

# Exports are streamed to S3 in pages to avoid memory issues
# The page size can be adjusted if needed to optimize performance
Expand Down
7 changes: 3 additions & 4 deletions packages/shared/src/server/services/S3StorageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type UploadFile = {
fileName: string;
fileType: string;
data: Readable | string;
expiresInMinutes?: number;
expiresInSeconds: number;
};

export class S3StorageService {
Expand Down Expand Up @@ -39,7 +39,7 @@ export class S3StorageService {
fileName,
fileType,
data,
expiresInMinutes,
expiresInSeconds,
}: UploadFile): Promise<{ signedUrl: string }> {
try {
await new Upload({
Expand All @@ -52,8 +52,7 @@ export class S3StorageService {
},
}).done();

const expiresIn = (expiresInMinutes ?? 60) * 60; // Convert minutes to seconds, default to 1 hour
const signedUrl = await this.getSignedUrl(fileName, expiresIn);
const signedUrl = await this.getSignedUrl(fileName, expiresInSeconds);

return { signedUrl };
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const generationsExportQuery = protectedProjectProcedure
fileName,
fileType: exportOptions[input.fileFormat].fileType,
data: fileStream,
expiresInSeconds: 60 * 60, // 1 hour
});

return {
Expand Down
8 changes: 4 additions & 4 deletions worker/src/features/batchExport/handleBatchExportJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ export const handleBatchExportJob = async (
const fileExtension =
exportOptions[jobDetails.format as BatchExportFileFormat].extension;
const fileName = `${fileDate}-lf-${tableName}-export-${projectId}.${fileExtension}`;
const expiresInSeconds =
env.BATCH_EXPORT_DOWNLOAD_LINK_EXPIRATION_HOURS * 3600;

const { signedUrl } = await new S3StorageService({
accessKeyId,
Expand All @@ -126,6 +128,7 @@ export const handleBatchExportJob = async (
fileType:
exportOptions[jobDetails.format as BatchExportFileFormat].fileType,
data: fileStream,
expiresInSeconds,
});

logger.info(`Batch export file uploaded to S3`);
Expand All @@ -140,10 +143,7 @@ export const handleBatchExportJob = async (
status: BatchExportStatus.COMPLETED,
url: signedUrl,
finishedAt: new Date(),
expiresAt: new Date(
Date.now() +
env.BATCH_EXPORT_DOWNLOAD_LINK_EXPIRATION_HOURS * 3600 * 1000
),
expiresAt: new Date(Date.now() + expiresInSeconds * 1000),
},
});

Expand Down

0 comments on commit 65b4429

Please sign in to comment.