Skip to content

Commit

Permalink
Log progress of s3 download
Browse files Browse the repository at this point in the history
  • Loading branch information
philmcmahon committed Jan 9, 2025
1 parent 31c2a5b commit ed1ff8e
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions packages/backend-common/src/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,29 @@ export const downloadObject = async (
if (!data.Body) {
throw new Error(`Failed to retrieve object ${key} from bucket ${bucket}`);
}
await downloadS3Data(data.Body as Readable, destinationPath, key);
return {
await downloadS3Data(
data.Body as Readable,
destinationPath,
extension: data.Metadata?.['extension'],
};
key,
data.ContentLength,
);
return data.Metadata?.['extension'];
};

const downloadS3Data = async (
data: Readable,
destinationPath: string,
key: string,
contentLength?: number,
) => {
const stream = data.pipe(createWriteStream(destinationPath));

let downloadedBytes = 0;
data.on('data', (chunk) => {
downloadedBytes += chunk.length;
logger.info(
`Downloaded ${downloadedBytes} of ${contentLength} bytes so far for ${key}`,
);
});
await new Promise<void>((resolve, reject) => {
stream
.on('finish', () => {
Expand Down

0 comments on commit ed1ff8e

Please sign in to comment.