in src/handlers/shared.ts [29:95]
export async function ProcessUploadToSourceBucket(event: S3CreateEvent) {
const STATE_MACHINE_ARN = process.env.STATE_MACHINE_ARN!;
interface Manifest {
Video: string;
Image: string;
Metadata: string;
}
for (const record of event.Records) {
const pathParts = record.s3.object.key.split("/");
const folder = pathParts.slice(0, pathParts.length - 1).join("/");
const manifestPath = `${folder}/manifest.json`;
const objects = await S3.listObjects({
Bucket: record.s3.bucket.name,
Prefix: folder
}).promise();
const keysInFolder = objects.Contents!.map((content) => content.Key);
if (keysInFolder.indexOf(manifestPath) < 0) {
console.log("Manifest not found.");
return;
}
const manifestObj = await S3.getObject({
Bucket: record.s3.bucket.name,
Key: manifestPath
}).promise();
const manifest: Manifest = JSON.parse(manifestObj.Body!.toString());
const folderContainsVideo = keysInFolder.indexOf(`${folder}/${manifest.Video}`) > -1;
const folderContainsImage = keysInFolder.indexOf(`${folder}/${manifest.Image}`) > -1;
const folderContainsMetadata = keysInFolder.indexOf(`${folder}/${manifest.Metadata}`) > -1;
if (folderContainsVideo && folderContainsImage && folderContainsMetadata) {
console.log("Manifest and files found. Starting State Machine");
const stepFunctionExecutionResult = await StepFunctions.startExecution({
input: JSON.stringify({
AssetId: folder,
Image: {
bucketName: record.s3.bucket.name,
objectKey: `${folder}/${manifest.Image}`
},
Metadata: {
bucketName: record.s3.bucket.name,
objectKey: `${folder}/${manifest.Metadata}`
},
Video: {
bucketName: record.s3.bucket.name,
objectKey: `${folder}/${manifest.Video}`
},
}),
name: `S3UploadTriggeredExecution${Date.now()}`,
stateMachineArn: STATE_MACHINE_ARN
}).promise();
console.log(stepFunctionExecutionResult);
} else {
console.log("Files required by Manifest are missing");
}
}
return;
}