Skip to content

Commit

Permalink
PassThrough stream käyttöön
Browse files Browse the repository at this point in the history
  • Loading branch information
augustk committed Jan 13, 2025
1 parent 7468056 commit db9793e
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 deletions lampi-siirtaja-container/lampi-siirtaja/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { Upload } from '@aws-sdk/lib-storage';

import MultiStream from 'multistream';
import { Readable } from 'node:stream';
import { PassThrough, Readable } from 'node:stream';

const dbHost = process.env.POSTGRES_HOST;
const dbUsername = process.env.DB_USERNAME;
Expand Down Expand Up @@ -77,40 +77,30 @@ const copyFileToLampi = async (sourceKey: string, numberOfFiles: number): Promis

console.log(`${sourceKey} | tiedostojen määrä: ${numberOfFiles}`);

let bodyStream;
let contentLength = 0;
if(numberOfFiles > 1) {
const streams = [];
for(let i = 1; i <= numberOfFiles; i++) {
const partSourceKey = i === 1 ? sourceKey : `${sourceKey}_part${i}`;
console.log(`${sourceKey} | partSourceKey: ${partSourceKey}`);

const getObjectCommandOutput: GetObjectCommandOutput = await ovaraS3Client.send(
new GetObjectCommand({
Bucket: ovaraLampiSiirtajaBucket,
Key: partSourceKey,
}),
);
streams.push(getObjectCommandOutput.Body);
contentLength = contentLength + getObjectCommandOutput.ContentLength;
}
const streams = [];
for(let i = 1; i <= numberOfFiles; i++) {
const partSourceKey = i === 1 ? sourceKey : `${sourceKey}_part${i}`;
console.log(`${sourceKey} | partSourceKey: ${partSourceKey}`);

bodyStream = new MultiStream(streams);
} else {
const getObjectCommandOutput: GetObjectCommandOutput = await ovaraS3Client.send(
new GetObjectCommand({
Bucket: ovaraLampiSiirtajaBucket,
Key: sourceKey,
Key: partSourceKey,
}),
);
bodyStream = getObjectCommandOutput.Body;
contentLength = getObjectCommandOutput.ContentLength;
streams.push(getObjectCommandOutput.Body);
contentLength = contentLength + getObjectCommandOutput.ContentLength;
}

const bodyStream = new MultiStream(streams);
const passThrough = new PassThrough();
bodyStream.pipe(passThrough);

const target = {
Bucket: lampiS3Bucket,
Key: destinationKey,
Body: bodyStream,
Body: passThrough,
ContentLength: contentLength,
ContentType: 'text/csv'
}
Expand Down

0 comments on commit db9793e

Please sign in to comment.