-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
48 lines (40 loc) · 1.63 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import {Callback, Context, Handler} from 'aws-lambda';
import {SsmHandler} from './src/utils/ssm-handler';
import {CloudfrontSignedHandler} from "./src/utils/cloudfront-signed-handler";
const cfDomainUrl = process.env.CLOUDFRONT_DOMAIN || "";
const keyPairId = process.env.KEY_PAIR_ID || "";
const ssmParameterKey = process.env.CLOUDFRONT_KEYPAIR_PATH || "";
const handler: Handler = async (event: any, context: Context, callback: Callback) => {
console.debug('event', event)
console.debug('context', context)
console.debug('cfDomainUrl', cfDomainUrl)
console.debug('keyPairId', keyPairId)
try {
if (!event.hasOwnProperty('s3ObjectPath')) {
throw new Error("S3 Bucket s3ObjectPath property not found in the event object.");
}
const {s3ObjectPath, expireDays} = event;
const privateKey = await SsmHandler.getInstance().getParameter(ssmParameterKey);
const {
signedUrl,
dateLessThan
} = CloudfrontSignedHandler.getInstance(cfDomainUrl, keyPairId, privateKey).getSignedUrl(s3ObjectPath, parseInt(expireDays) || 7)
console.info(`
s3ObjectPath: ${s3ObjectPath}
keyPairId: ${keyPairId}
signedUrl: ${signedUrl}
expireDay: ${dateLessThan}
`);
return {
statusCode: 200,
body: JSON.stringify({signedUrl: signedUrl, expireDay: dateLessThan}, null, 2),
};
} catch (error) {
console.error("An error occurred:", error);
return {
statusCode: 400,
body: JSON.stringify({message: error})
};
}
};
export {handler};