@eyevinn/iaf-plugin-aws
Version:
Eyevinn Ingest Application Framework (IAF) plugin for upload and transcode in AWS
107 lines • 4.67 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.S3Uploader = void 0;
const lib_storage_1 = require("@aws-sdk/lib-storage");
const client_s3_1 = require("@aws-sdk/client-s3");
/**
* S3 Uploader class
* Holds the S3 client that data will be piped through for upload to ingest bucket
*/
class S3Uploader {
constructor(destination, outputDestination, awsRegion, outputFiles, logger, watcherTimeout) {
this.destination = destination;
this.outputDestination = outputDestination;
this.outputFiles = outputFiles;
this.region = awsRegion;
this.logger = logger;
if (!watcherTimeout) {
this.timeout = process.env.TIMEOUT ? parseInt(process.env.TIMEOUT) : 300;
}
else {
this.timeout = watcherTimeout;
}
}
/**
* Uploads a file to the destination bucket
* @param fileStream a Readable stream of the file to upload
* @param fileName the name of the uploaded file (this will be the S3 key)
* @returns status report from the AWS upload
*/
upload(fileStream, fileName) {
return __awaiter(this, void 0, void 0, function* () {
const config = { region: this.region };
const target = {
Bucket: this.destination,
Key: fileName,
Body: fileStream
};
try {
const parallelUploadsToS3 = new lib_storage_1.Upload({
client: new client_s3_1.S3(config) || new client_s3_1.S3Client(config),
params: target
});
parallelUploadsToS3.on("httpUploadProgress", (progress) => {
this.logger.info(`Upload progress for ${fileName}: ${(progress.loaded / progress.total) * 100} %`);
});
const data = yield parallelUploadsToS3.done();
return data;
}
catch (err) {
this.logger.error(`Failed to upload ${fileName}`);
throw err;
}
});
}
/**
* Watches for an object to be uploaded to S3
* @param fileName the name of the file to wait for
* @returns An object with the S3 paths to the files if they have been uploaded successfully.
* Else an object with the error message if the file has not been uploaded within the timeout period.
*/
watcher(fileName) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.outputFiles) {
this.logger.info(`No 'outputFiles' specified abort watcher`);
return null;
}
const config = { region: this.region };
const client = new client_s3_1.S3(config) || new client_s3_1.S3Client(config);
let outputsDest = {};
let outputError = {};
for (const file in this.outputFiles) {
const key = this.outputFiles[file];
const dest = `${this.outputDestination}/${fileName}/${key}`;
try {
this.logger.info(`Watching for: ${dest}`);
yield (0, client_s3_1.waitUntilObjectExists)({ client, maxWaitTime: this.timeout }, { Bucket: this.outputDestination, Key: `${fileName}/${key}` });
outputsDest[file] = `arn:aws:s3:::${dest}`;
}
catch (err) {
this.logger.error(`Watcher could not find: ${dest}`);
outputsDest[file] = null;
outputError[file] = err;
}
}
if (Object.keys(outputError).length > 0) {
return {
outputs: outputsDest,
outputError: outputError
};
}
else {
return outputsDest;
}
});
}
}
exports.S3Uploader = S3Uploader;
//# sourceMappingURL=s3Uploader.js.map