-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
96 lines (82 loc) · 2.44 KB
/
index.js
File metadata and controls
96 lines (82 loc) · 2.44 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
85
86
87
88
89
90
91
92
93
94
95
import { S3Client, CopyObjectCommand, HeadObjectCommand } from '@aws-sdk/client-s3';
const s3Client = new S3Client({ region: process.env.AWS_REGION });
const SIZES = {
MLS: {
folder: 'mls',
width: 2048
},
THUMB: {
folder: 'thumb',
width: 240
}
};
export const handler = async (event) => {
try {
const record = event.Records[0].s3;
const bucket = record.bucket.name;
const key = decodeURIComponent(record.object.key.replace(/\+/g, ' '));
// Validate key format: media/{propertyId}/original/{filename}
const keyParts = key.split('/');
if (keyParts.length !== 4 || keyParts[0] !== 'media' || keyParts[2] !== 'original') {
console.log(`Skipping invalid key format: ${key}`);
return {
statusCode: 200,
body: JSON.stringify({ message: 'Skipped - invalid key format' })
};
}
const propertyId = keyParts[1];
const filename = keyParts[3];
// Get original file metadata
const headCommand = new HeadObjectCommand({
Bucket: bucket,
Key: key
});
const { ContentType, Metadata } = await s3Client.send(headCommand);
// Create copies for each size
const results = await Promise.all(
Object.entries(SIZES).map(async ([size, config]) => {
const newKey = `media/${propertyId}/${config.folder}/${filename}`;
const copyCommand = new CopyObjectCommand({
Bucket: bucket,
Key: newKey,
CopySource: `${bucket}/${key}`,
ContentType,
Metadata: {
...Metadata,
processedAt: new Date().toISOString(),
processingSize: size,
originalKey: key,
targetWidth: config.width.toString()
},
MetadataDirective: 'REPLACE'
});
await s3Client.send(copyCommand);
return {
size,
key: newKey,
url: `https://${bucket}.s3.${process.env.AWS_REGION}.amazonaws.com/${newKey}`
};
})
);
console.log('Successfully processed:', {
original: key,
copies: results
});
return {
statusCode: 200,
body: JSON.stringify({
message: 'Processing completed successfully',
results
})
};
} catch (error) {
console.error('Lambda handler error:', error);
return {
statusCode: 500,
body: JSON.stringify({
error: 'Failed to process image',
details: error.message
})
};
}
};