feat(techdocs): update aws-sdk to v3
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2020 Spotify AB
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import type { S3ClientConfig } from '@aws-sdk/client-s3';
|
||||
import fs from 'fs';
|
||||
|
||||
export class S3 {
|
||||
private readonly options;
|
||||
|
||||
constructor(options: S3ClientConfig) {
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
headObject({ Key }: { Key: string }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (fs.existsSync(Key)) {
|
||||
resolve('');
|
||||
} else {
|
||||
reject({ message: `The file ${Key} doest not exist.` });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
getObject({ Key }: { Key: string }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (fs.existsSync(Key)) {
|
||||
resolve({
|
||||
Body: Buffer.from(fs.readFileSync(Key)),
|
||||
});
|
||||
} else {
|
||||
reject({ message: `The file ${Key} doest not exist.` });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
headBucket() {
|
||||
return new Promise(resolve => {
|
||||
resolve('');
|
||||
});
|
||||
}
|
||||
|
||||
putObject({ Key }: { Key: string }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (
|
||||
fs.existsSync(Key) &&
|
||||
fs.readFileSync(Key).toString() === 'mock-error'
|
||||
) {
|
||||
reject('');
|
||||
} else {
|
||||
resolve('');
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,7 @@
|
||||
"url": "https://github.com/backstage/backstage/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"@aws-sdk/client-s3": "^3.0.0",
|
||||
"@backstage/backend-common": "^0.4.2",
|
||||
"@backstage/catalog-model": "^0.6.0",
|
||||
"@backstage/config": "^0.1.2",
|
||||
@@ -43,7 +44,6 @@
|
||||
"@google-cloud/storage": "^5.6.0",
|
||||
"@types/dockerode": "^3.2.1",
|
||||
"@types/express": "^4.17.6",
|
||||
"aws-sdk": "^2.817.0",
|
||||
"cross-fetch": "^3.0.6",
|
||||
"dockerode": "^3.2.1",
|
||||
"express": "^4.17.1",
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user