Skip to content

Commit

Permalink
Improve s3 download logs
Browse files Browse the repository at this point in the history
  • Loading branch information
philmcmahon committed Jan 10, 2025
1 parent 505d7b2 commit 105b12c
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/backend-common/src/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ export const downloadObject = async (
return data.Metadata?.['extension'];
};

const bytesToMB = (bytes: number) => bytes / 1024 / 1024;

const downloadS3Data = async (
data: Readable,
destinationPath: string,
Expand All @@ -172,9 +174,10 @@ const downloadS3Data = async (
) => {
let downloadedBytes = 0;
let lastLoggedPercentage = 0;
const contentLengthMb = contentLength && bytesToMB(contentLength);
data.on('data', (chunk) => {
downloadedBytes += chunk.length;
if (contentLength) {
if (contentLength && contentLengthMb) {
const percentage = Math.floor((downloadedBytes / contentLength) * 100);
if (
downloadedBytes > 0 &&
Expand All @@ -183,7 +186,7 @@ const downloadS3Data = async (
) {
lastLoggedPercentage = percentage;
logger.info(
`Downloaded ${downloadedBytes} of ${contentLength} bytes so far ${contentLength ? `(${percentage}%)` : ''} for ${key}`,
`Downloaded ${bytesToMB(downloadedBytes)} of ${contentLengthMb} MB so far (${percentage}%) for ${key}`,
);
}
}
Expand All @@ -193,7 +196,6 @@ const downloadS3Data = async (
data
.pipe(stream)
.on('finish', () => {
logger.debug('stream pipe done');
resolve();
})
.on('error', (error) => {
Expand Down

0 comments on commit 105b12c

Please sign in to comment.