Skip to content

Commit

Permalink
Streamien yhdistely vain jos on useampi kuin 1 stream
Browse files Browse the repository at this point in the history
  • Loading branch information
augustk committed Jan 13, 2025
1 parent 51438cb commit 7468056
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions lampi-siirtaja-container/lampi-siirtaja/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { Upload } from '@aws-sdk/lib-storage';

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

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

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

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

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

const multiStream = new MultiStream(streams);

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

0 comments on commit 7468056

Please sign in to comment.