Skip to content

Commit 32061c4

Browse files
committed
feat: implement uploadByUrl function for S3 image uploads
1 parent 14a49af commit 32061c4

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { z } from "zod";
2+
import { handleActionException } from "./RepositoryException";
3+
import { PutObjectCommand } from "@aws-sdk/client-s3";
4+
import { env } from "@/env";
5+
import { s3Client } from "../s3.client";
6+
7+
const schema = z.object({
8+
url: z.string().url(),
9+
key: z.string(),
10+
});
11+
12+
export const uploadByUrl = async (_input: z.infer<typeof schema>) => {
13+
try {
14+
const input = await schema.parseAsync(_input);
15+
const api = await fetch(input.url);
16+
const body = await api.bytes();
17+
const command = new PutObjectCommand({
18+
Bucket: env.S3_BUCKET,
19+
Key: input.key,
20+
ContentType: "image/jpeg",
21+
Body: body,
22+
});
23+
await s3Client.send(command);
24+
} catch (error) {
25+
return handleActionException(error);
26+
}
27+
};

0 commit comments

Comments
 (0)