-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3.ts
66 lines (57 loc) · 1.61 KB
/
s3.ts
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
import { Client } from "minio";
import createHttpError from "http-errors";
import { Context, Next } from "hono";
export const {
S3_REGION,
S3_ACCESS_KEY_ID = "",
S3_SECRET_ACCESS_KEY = "",
S3_ENDPOINT = "s3.amazonaws.com",
S3_PORT,
S3_BUCKET = "",
S3_USE_SSL,
S3_FILE_KEY = "",
} = process.env;
export const minioClient = new Client({
accessKey: S3_ACCESS_KEY_ID,
secretKey: S3_SECRET_ACCESS_KEY,
endPoint: S3_ENDPOINT,
port: Number(S3_PORT),
useSSL: !!S3_USE_SSL,
region: S3_REGION,
});
export const sendFormFromS3 = async (c: Context, key: any) => {
const stream = await minioClient.getObject(S3_BUCKET, key);
if (!stream) throw "No stream available";
const readableStream = new ReadableStream({
start(controller) {
stream.on("data", (chunk) => {
controller.enqueue(chunk);
});
stream.on("end", () => {
controller.close();
});
stream.on("error", (err) => {
controller.error(err);
});
},
});
return c.newResponse(readableStream, {
headers: {
"Content-Disposition": `attachment; filename=${encodeURIComponent(key)}`,
},
});
};
export const uploadMiddleware = async (c: Context, next: Next) => {
try {
let { form } = await c.req.parseBody();
if (!form || !(form instanceof File))
throw createHttpError(400, "File not provided");
const fileName = decodeURIComponent(form.name);
const arrayBuffer = await form.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
await minioClient.putObject(S3_BUCKET, fileName, buffer);
await next();
} catch (error) {
throw error;
}
};