@m11s-io/s3-upload-proxy 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +57 -0
  2. package/package.json +37 -0
package/dist/index.js ADDED
@@ -0,0 +1,57 @@
1
+ #!/usr/bin/env node
2
+ #!/usr/bin/env node
3
+
4
+ // src/index.ts
5
+ import express from "express";
6
+ import multer from "multer";
7
+ import cors from "cors";
8
+ import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
9
+ var {
10
+ S3_ENDPOINT,
11
+ S3_ACCESS_KEY,
12
+ S3_SECRET_KEY,
13
+ S3_BUCKET,
14
+ S3_PUBLIC_URL,
15
+ S3_KEY_PREFIX = "",
16
+ S3_REGION = "us-east-1",
17
+ PORT = "8082"
18
+ } = process.env;
19
+ if (!S3_ENDPOINT || !S3_ACCESS_KEY || !S3_SECRET_KEY || !S3_BUCKET || !S3_PUBLIC_URL) {
20
+ console.error("Missing required env: S3_ENDPOINT, S3_ACCESS_KEY, S3_SECRET_KEY, S3_BUCKET, S3_PUBLIC_URL");
21
+ process.exit(1);
22
+ }
23
+ var s3 = new S3Client({
24
+ endpoint: S3_ENDPOINT,
25
+ region: S3_REGION,
26
+ credentials: { accessKeyId: S3_ACCESS_KEY, secretAccessKey: S3_SECRET_KEY },
27
+ forcePathStyle: true
28
+ });
29
+ var app = express();
30
+ app.use(cors());
31
+ var upload = multer({ storage: multer.memoryStorage() });
32
+ app.post("/upload/:tenant?", upload.single("file"), async (req, res) => {
33
+ if (!req.file) {
34
+ res.status(400).json({ error: "No file provided" });
35
+ return;
36
+ }
37
+ const { originalname, buffer, mimetype } = req.file;
38
+ const tenant = req.params.tenant;
39
+ const prefix = [S3_KEY_PREFIX, tenant].filter(Boolean).join("/");
40
+ const key = `${prefix ? prefix + "/" : ""}${Date.now()}-${originalname}`;
41
+ try {
42
+ await s3.send(new PutObjectCommand({
43
+ Bucket: S3_BUCKET,
44
+ Key: key,
45
+ Body: buffer,
46
+ ContentType: mimetype
47
+ }));
48
+ res.json({ url: `${S3_PUBLIC_URL}/${key}` });
49
+ } catch (err) {
50
+ console.error("Upload error:", err.message);
51
+ res.status(500).json({ error: err.message });
52
+ }
53
+ });
54
+ app.listen(
55
+ Number(PORT),
56
+ () => console.log(`s3-upload-proxy listening on port ${PORT}`)
57
+ );
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@m11s-io/s3-upload-proxy",
3
+ "version": "0.1.0",
4
+ "description": "HTTP-to-S3 upload proxy — accepts multipart file uploads and stores them in any S3-compatible store",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "bin": {
8
+ "s3-upload-proxy": "dist/index.js"
9
+ },
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "scripts": {
14
+ "build": "tsup",
15
+ "dev": "tsup --watch"
16
+ },
17
+ "keywords": [
18
+ "s3",
19
+ "minio",
20
+ "upload",
21
+ "proxy"
22
+ ],
23
+ "license": "MIT",
24
+ "dependencies": {
25
+ "@aws-sdk/client-s3": "^3",
26
+ "cors": "^2",
27
+ "express": "^5",
28
+ "multer": "^1"
29
+ },
30
+ "devDependencies": {
31
+ "@types/cors": "^2",
32
+ "@types/express": "^5",
33
+ "@types/multer": "^1",
34
+ "tsup": "^8",
35
+ "typescript": "^5"
36
+ }
37
+ }