Skip to content

Commit a63ff66

Browse files
committed
feat: upgrade middleware to use AWS SDK v3
1 parent 201f5e2 commit a63ff66

File tree

4 files changed

+1235
-25
lines changed

4 files changed

+1235
-25
lines changed

packages/large-response-middleware/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
}
4141
],
4242
"devDependencies": {
43+
"@aws-sdk/client-s3": "3.782.0",
44+
"@aws-sdk/s3-request-presigner": "3.782.0",
4345
"@babel/cli": "^7.23.4",
4446
"@babel/core": "^7.25.2",
4547
"@babel/preset-env": "^7.25.4",
@@ -55,7 +57,6 @@
5557
"@types/aws-lambda": "^8.10.130",
5658
"@types/node": "^20.10.5",
5759
"aws-lambda": "^1.0.7",
58-
"aws-sdk": "^2.816.0",
5960
"cross-env": "^7.0.3",
6061
"npm-run-all": "^4.1.5",
6162
"rollup": "^4.9.1",
@@ -68,10 +69,11 @@
6869
"vitest": "3.0.5"
6970
},
7071
"peerDependencies": {
72+
"@aws-sdk/client-s3": "^3.x",
73+
"@aws-sdk/s3-request-presigner": "^3.x",
7174
"@dazn/lambda-powertools-logger": "^1.28.1",
7275
"@middy/core": "^2.5.7",
73-
"aws-lambda": "^1.0.7",
74-
"aws-sdk": "^2.816.0"
76+
"aws-lambda": "^1.0.7"
7577
},
7678
"dependencies": {
7779
"core-js": "^3.34.0",

packages/large-response-middleware/src/file-storage-service.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import path from 'node:path';
22

33
import { getS3Client } from './s3/s3-client';
44

5+
import { GetObjectCommand, PutObjectCommand } from '@aws-sdk/client-s3';
6+
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
57
import type { FileUploadContext } from '.';
68

79
/**
@@ -15,22 +17,27 @@ export const uploadFile = async (params: FileUploadContext) => {
1517
const date = getFormattedDate();
1618
const outputKey = `${namespace}/${date}/${encodeURIComponent(params.fileName)}`;
1719

18-
await client
19-
.putObject({
20+
await client.send(
21+
new PutObjectCommand({
2022
Bucket: params.bucket,
2123
Key: outputKey,
2224
ContentType: params.contentType || 'text/plain',
2325
Body: JSON.stringify(params.content || {}),
2426
ACL: 'private',
25-
})
26-
.promise();
27-
28-
const url = await client.getSignedUrl('getObject', {
29-
Expires: 3600,
30-
Bucket: params.bucket,
31-
Key: outputKey,
32-
ResponseContentDisposition: `inline;filename=${path.basename(outputKey)}`,
33-
});
27+
}),
28+
);
29+
30+
const url = await getSignedUrl(
31+
client,
32+
new GetObjectCommand({
33+
Bucket: params.bucket,
34+
Key: outputKey,
35+
ResponseContentDisposition: `inline;filename=${path.basename(outputKey)}`,
36+
}),
37+
{
38+
expiresIn: 3600,
39+
},
40+
);
3441

3542
return { url, filename: outputKey };
3643
};

packages/large-response-middleware/src/s3/s3-client.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1+
import { S3Client, type S3ClientConfig } from '@aws-sdk/client-s3';
12
import Log from '@dazn/lambda-powertools-logger';
2-
import { S3 } from 'aws-sdk';
33

44
const AWS_ENDPOINT =
55
process.env.AWS_ENDPOINT ||
66
(process.env.STAGE === 'local' || process.env.NODE_ENV === 'local' ? 'http://host.docker.internal:4566' : undefined);
77

8-
export const getS3Client = async (options?: S3.Types.ClientConfiguration) => {
8+
export const getS3Client = async (options?: S3ClientConfig) => {
99
try {
10-
const client = new S3({
10+
const client = new S3Client({
1111
endpoint: AWS_ENDPOINT,
12-
s3ForcePathStyle: Boolean(AWS_ENDPOINT),
13-
httpOptions: {
14-
timeout: 60_000,
15-
...options?.httpOptions,
16-
},
12+
forcePathStyle: Boolean(AWS_ENDPOINT),
1713
...options,
1814
});
1915

0 commit comments

Comments
 (0)