import { S3 } from '@aws-sdk/client-s3'; import * as chalk from 'chalk'; import * as crypto from 'crypto'; import {AppContext} from './AppContext'; import {runtimeConfig} from './Config'; import {Rivendell} from './Rivendell'; export class AppUploader { constructor(private context: AppContext, private packageData: Buffer) { } public async upload(): Promise { console.log(chalk.gray('Uploading package...')); const {vendor, appId, version} = this.context; const md5 = crypto.createHash('md5').update(this.packageData).digest('hex'); const bucket = runtimeConfig().uploadBucket; const s3Key = `${vendor}/${appId}@${version}-${md5}.zip`; const credentials = await Rivendell.uploadCredentials(vendor!, appId!, version!, md5); await new S3({ region: runtimeConfig().awsRegion, credentials: { accessKeyId: credentials.accessKeyId, secretAccessKey: credentials.secretAccessKey, sessionToken: credentials.sessionToken } }).putObject( { Bucket: bucket, Key: s3Key, Body: this.packageData }); return `s3://${bucket}/${s3Key}`; } }