Skip to content

Commit c8ee2e9

Browse files
committed
Convert to command style
1 parent dbacbf8 commit c8ee2e9

File tree

4 files changed

+90
-63
lines changed

4 files changed

+90
-63
lines changed

backend/convertImage.ts

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
import { S3 } from '@aws-sdk/client-s3';
1+
import {
2+
DeleteObjectsCommand,
3+
GetObjectCommand,
4+
PutObjectCommand,
5+
S3Client,
6+
} from '@aws-sdk/client-s3';
7+
import { S3Event } from 'aws-lambda';
28
import sharp from 'sharp';
39

410
import * as ImageAdjustUtils from './src/image-processing/ImageAdjustUtils';
511
import * as LaserdiscUtils from './src/image-processing/LaserdiscUtils';
612

7-
const s3 = new S3();
13+
const s3 = new S3Client();
814

915
type Template = { prefix: string; suffix: string };
1016

@@ -31,7 +37,7 @@ const INPUT_PREFIX = 'originals/';
3137
const getRootKey = (srcKey: string): string =>
3238
srcKey.substring(INPUT_PREFIX.length).replace(/\.\w+$/, '');
3339

34-
export const handler = async (event): Promise<unknown> => {
40+
export const handler = async (event: S3Event): Promise<unknown> => {
3541
const srcBucket = event.Records[0].s3.bucket.name;
3642
const srcKey = event.Records[0].s3.object.key;
3743

@@ -42,10 +48,12 @@ export const handler = async (event): Promise<unknown> => {
4248

4349
const rootKey = getRootKey(srcKey);
4450

45-
const inputObject = await s3.getObject({
46-
Bucket: srcBucket,
47-
Key: srcKey,
48-
});
51+
const inputObject = await s3.send(
52+
new GetObjectCommand({
53+
Bucket: srcBucket,
54+
Key: srcKey,
55+
})
56+
);
4957

5058
if (!inputObject.Body) {
5159
throw new Error('Source object not found');
@@ -83,13 +91,15 @@ export const handler = async (event): Promise<unknown> => {
8391
})
8492
.toBuffer()
8593
.then((outputBuffer) =>
86-
s3.putObject({
87-
Body: outputBuffer,
88-
Bucket: srcBucket,
89-
Key: makeFilename(FILENAMES.jpeg, rootKey),
90-
ACL: 'public-read',
91-
ContentType: 'image/jpeg',
92-
})
94+
s3.send(
95+
new PutObjectCommand({
96+
Body: outputBuffer,
97+
Bucket: srcBucket,
98+
Key: makeFilename(FILENAMES.jpeg, rootKey),
99+
ACL: 'public-read',
100+
ContentType: 'image/jpeg',
101+
})
102+
)
93103
),
94104

95105
sharp(inputBuffer)
@@ -100,13 +110,15 @@ export const handler = async (event): Promise<unknown> => {
100110
})
101111
.toBuffer()
102112
.then((outputBuffer) =>
103-
s3.putObject({
104-
Body: outputBuffer,
105-
Bucket: srcBucket,
106-
Key: makeFilename(FILENAMES.jpeg720, rootKey),
107-
ACL: 'public-read',
108-
ContentType: 'image/jpeg',
109-
})
113+
s3.send(
114+
new PutObjectCommand({
115+
Body: outputBuffer,
116+
Bucket: srcBucket,
117+
Key: makeFilename(FILENAMES.jpeg720, rootKey),
118+
ACL: 'public-read',
119+
ContentType: 'image/jpeg',
120+
})
121+
)
110122
),
111123

112124
sharp(inputBuffer)
@@ -116,18 +128,20 @@ export const handler = async (event): Promise<unknown> => {
116128
})
117129
.toBuffer()
118130
.then((outputBuffer) =>
119-
s3.putObject({
120-
Body: outputBuffer,
121-
Bucket: srcBucket,
122-
Key: makeFilename(FILENAMES.jpeg420, rootKey),
123-
ACL: 'public-read',
124-
ContentType: 'image/jpeg',
125-
})
131+
s3.send(
132+
new PutObjectCommand({
133+
Body: outputBuffer,
134+
Bucket: srcBucket,
135+
Key: makeFilename(FILENAMES.jpeg420, rootKey),
136+
ACL: 'public-read',
137+
ContentType: 'image/jpeg',
138+
})
139+
)
126140
),
127141
]);
128142
};
129143

130-
export const deletionHandler = async (event): Promise<unknown> => {
144+
export const deletionHandler = async (event: S3Event): Promise<unknown> => {
131145
const srcBucket = event.Records[0].s3.bucket.name;
132146
const srcKey = event.Records[0].s3.object.key;
133147

@@ -138,14 +152,16 @@ export const deletionHandler = async (event): Promise<unknown> => {
138152

139153
const rootKey = getRootKey(srcKey);
140154

141-
return s3.deleteObjects({
142-
Bucket: srcBucket,
143-
Delete: {
144-
Objects: Object.values(FILENAMES)
145-
.map((template) => makeFilename(template, rootKey))
146-
.map((key) => ({
147-
Key: key,
148-
})),
149-
},
150-
});
155+
return s3.send(
156+
new DeleteObjectsCommand({
157+
Bucket: srcBucket,
158+
Delete: {
159+
Objects: Object.values(FILENAMES)
160+
.map((template) => makeFilename(template, rootKey))
161+
.map((key) => ({
162+
Key: key,
163+
})),
164+
},
165+
})
166+
);
151167
};

backend/package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"license": "ISC",
2121
"devDependencies": {
2222
"@aws-sdk/client-s3": "^3.741.0",
23-
"@types/aws-lambda": "^8.10.125",
23+
"@types/aws-lambda": "^8.10.147",
2424
"@types/cookie-parser": "^1.4.3",
2525
"@types/cors": "^2.8.17",
2626
"@types/eslint": "^8.44.6",

backend/src/business/color/ColorService.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
import { S3 } from '@aws-sdk/client-s3';
1+
import {
2+
GetObjectCommand,
3+
HeadObjectCommand,
4+
PutObjectCommand,
5+
S3Client,
6+
} from '@aws-sdk/client-s3';
27
import { NotFound } from 'http-errors';
38
import * as LedgerService from '../ledger/LedgerService';
49
import isProduction from '../utils/isProduction';
510
import { colorizeImageWithAutoPromptBase64 } from '../utils/paletteApi';
611

7-
const s3 = new S3();
12+
const s3 = new S3Client();
813

914
// A lot of engineering went into this prompt
1015
// One word can make a huge difference
@@ -18,10 +23,12 @@ async function createColorVersion(
1823
): Promise<void> {
1924
const resolution = useTestingResolution ? 'watermarked-sd' : 'sd';
2025

21-
const s3Response = await s3.getObject({
22-
Bucket: 'fourties-photos',
23-
Key: sourceKey,
24-
});
26+
const s3Response = await s3.send(
27+
new GetObjectCommand({
28+
Bucket: 'fourties-photos',
29+
Key: sourceKey,
30+
})
31+
);
2532

2633
if (!s3Response.Body) {
2734
throw new NotFound('Original image not found');
@@ -42,12 +49,14 @@ async function createColorVersion(
4249

4350
const colorizedImage = Buffer.from(colorizedImageBase64, 'base64');
4451

45-
await s3.putObject({
46-
Bucket: 'fourties-photos',
47-
Key: destinationKey,
48-
Body: colorizedImage,
49-
ContentType: 'image/jpeg',
50-
});
52+
await s3.send(
53+
new PutObjectCommand({
54+
Bucket: 'fourties-photos',
55+
Key: destinationKey,
56+
Body: colorizedImage,
57+
ContentType: 'image/jpeg',
58+
})
59+
);
5160
}
5261

5362
/**
@@ -66,10 +75,12 @@ export async function getColorizedImage(
6675

6776
// Check if image already exists
6877
const headObjectResponse = await s3
69-
.headObject({
70-
Bucket: 'fourties-photos',
71-
Key: destinationKey,
72-
})
78+
.send(
79+
new HeadObjectCommand({
80+
Bucket: 'fourties-photos',
81+
Key: destinationKey,
82+
})
83+
)
7384
.catch(() => null);
7485

7586
if (!headObjectResponse) {

0 commit comments

Comments
 (0)