-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretrieve.js
More file actions
84 lines (75 loc) · 2.46 KB
/
retrieve.js
File metadata and controls
84 lines (75 loc) · 2.46 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { GetObjectCommand, NoSuchKey, S3Client } from '@aws-sdk/client-s3';
import { toBase64 } from '@smithy/util-base64';
// Create S3 client
const REGION = 'us-east-1';
const config = { region: REGION };
const client = new S3Client(config);
const BUCKET_NAME = 'case-consulting-mgmt-genbot-images';
/**
* Gets image from bucket.
*
* @param {string} imageFileName - The image file name.
* @returns {string} The image file converted to Base64-encoded string.
*/
export const getImage = async (imageFileName) => {
try {
// Get image file object from bucket
const getCommand = new GetObjectCommand({
Bucket: BUCKET_NAME,
Key: imageFileName
});
const response = await client.send(getCommand);
// Return image as Base64-encoded string
const imageBytes = await response.Body.transformToByteArray();
return toBase64(imageBytes);
} catch (error) {
if (error instanceof NoSuchKey) {
console.error('Image file not found.');
} else {
console.error(`Error while getting image from ${BUCKET_NAME}. ${error.name}: ${error.message}`);
}
throw error;
}
};
/**
* Gets specified image from bucket.
* Returns image as Base64-encoded string.
*
* @param {Object} event - Lambda Function URL request
* @see {@link https://docs.aws.amazon.com/lambda/latest/dg/urls-invocation.html}
*
* @param {Object} context - Lambda context object
* @see {@link https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html}
*
* @returns {Object} object - Lambda Function URL response
* @see {@link https://docs.aws.amazon.com/lambda/latest/dg/urls-invocation.html}
*/
export const handler = async (event, context) => {
let response;
try {
// Obtain image file name from request path (e.g., /abc.png)
const requestPath = event.requestContext.http.path;
const imageFileName = requestPath.replace(/^\//, '');
console.log(`Getting ${imageFileName} from ${BUCKET_NAME}`);
// Get image file, as Base64-encoded string
const base64Body = await getImage(imageFileName);
// Return image as Base64-encoded string
response = {
headers: {
'content-type': 'image/jpg'
},
statusCode: 200,
body: base64Body,
isBase64Encoded: true
};
} catch (error) {
// Redirect to 'Something went wrong' image
response = {
statusCode: 303,
headers: {
Location: 'https://media.giphy.com/media/l41JNsXAvFvoHvWJW/giphy.gif'
}
};
}
return response;
};