feat(techdocs): update aws-sdk to v3

This commit is contained in:
Remi
2021-01-07 01:20:57 +01:00
parent ab11e705c9
commit 7f34d22bdf
5 changed files with 744 additions and 38 deletions
@@ -187,7 +187,7 @@ describe('AwsS3Publish', () => {
.catch(error =>
expect(error).toEqual(
new Error(
`TechDocs metadata fetch failed, The file ${entityRootDir}/techdocs_metadata.json doest not exist !`,
`TechDocs metadata fetch failed, The file ${entityRootDir}/techdocs_metadata.json doest not exist.`,
),
),
);
@@ -15,13 +15,12 @@
*/
import path from 'path';
import express from 'express';
import aws from 'aws-sdk';
import { PutObjectCommandOutput, S3 } from '@aws-sdk/client-s3';
import { Logger } from 'winston';
import { Entity, EntityName } from '@backstage/catalog-model';
import { Config } from '@backstage/config';
import { getHeadersForFileExtension, getFileTreeRecursively } from './helpers';
import { PublisherBase, PublishRequest } from './types';
import { ManagedUpload } from 'aws-sdk/clients/s3';
import fs from 'fs-extra';
export class AwsS3Publish implements PublisherBase {
@@ -47,7 +46,7 @@ export class AwsS3Publish implements PublisherBase {
);
}
const storageClient = new aws.S3({
const storageClient = new S3({
credentials: { accessKeyId, secretAccessKey },
...(region && { region }),
});
@@ -79,7 +78,7 @@ export class AwsS3Publish implements PublisherBase {
}
constructor(
private readonly storageClient: aws.S3,
private readonly storageClient: S3,
private readonly bucketName: string,
private readonly logger: Logger,
) {
@@ -98,7 +97,7 @@ export class AwsS3Publish implements PublisherBase {
// So collecting path of only the files is good enough.
const allFilesToUpload = await getFileTreeRecursively(directory);
const uploadPromises: Array<Promise<ManagedUpload.SendData>> = [];
const uploadPromises: Array<Promise<PutObjectCommandOutput>> = [];
for (const filePath of allFilesToUpload) {
// Remove the absolute path prefix of the source directory
@@ -116,7 +115,7 @@ export class AwsS3Publish implements PublisherBase {
Body: fileContent,
};
uploadPromises.push(this.storageClient.upload(params).promise());
uploadPromises.push(this.storageClient.putObject(params));
}
await Promise.all(uploadPromises);
this.logger.info(
@@ -135,25 +134,25 @@ export class AwsS3Publish implements PublisherBase {
return await new Promise<string>((resolve, reject) => {
const entityRootDir = `${entityName.namespace}/${entityName.kind}/${entityName.name}`;
const fileStreamChunks: Array<any> = [];
this.storageClient
.getObject({
Bucket: this.bucketName,
Key: `${entityRootDir}/techdocs_metadata.json`,
})
.createReadStream()
.on('error', err => {
.then(file => {
const techdocsMetadataJson = file?.Body?.toString();
if (!techdocsMetadataJson) {
throw new Error(
`Unable to parse the techdocs metadata file ${entityRootDir}/techdocs_metadata.json.`,
);
}
resolve(techdocsMetadataJson);
})
.catch(err => {
this.logger.error(err.message);
reject(new Error(err.message));
})
.on('data', chunk => {
fileStreamChunks.push(chunk);
})
.on('end', () => {
const techdocsMetadataJson = Buffer.concat(
fileStreamChunks,
).toString();
resolve(techdocsMetadataJson);
});
});
} catch (e) {
@@ -174,19 +173,14 @@ export class AwsS3Publish implements PublisherBase {
const fileExtension = path.extname(filePath);
const responseHeaders = getHeadersForFileExtension(fileExtension);
const fileStreamChunks: Array<any> = [];
this.storageClient
.getObject({ Bucket: this.bucketName, Key: filePath })
.createReadStream()
.on('error', err => {
this.logger.warn(err.message);
res.status(404).send(err.message);
})
.on('data', chunk => {
fileStreamChunks.push(chunk);
})
.on('end', () => {
const fileContent = Buffer.concat(fileStreamChunks);
.then(object => {
const fileContent = object?.Body?.toString();
if (!fileContent) {
throw new Error(`Unable to parse the file ${filePath}.`);
}
// Inject response headers
for (const [headerKey, headerValue] of Object.entries(
responseHeaders,
@@ -195,6 +189,10 @@ export class AwsS3Publish implements PublisherBase {
}
res.send(fileContent);
})
.catch(err => {
this.logger.warn(err.message);
res.status(404).send(err.message);
});
};
}
@@ -206,12 +204,10 @@ export class AwsS3Publish implements PublisherBase {
async hasDocsBeenGenerated(entity: Entity): Promise<boolean> {
try {
const entityRootDir = `${entity.metadata.namespace}/${entity.kind}/${entity.metadata.name}`;
await this.storageClient
.headObject({
Bucket: this.bucketName,
Key: `${entityRootDir}/index.html`,
})
.promise();
await this.storageClient.headObject({
Bucket: this.bucketName,
Key: `${entityRootDir}/index.html`,
});
return Promise.resolve(true);
} catch (e) {
return Promise.resolve(false);