This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
336 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
# sst | ||
.sst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
FROM node:18-bullseye-slim | ||
|
||
WORKDIR /app/ | ||
|
||
COPY package.json /app | ||
RUN npm install | ||
|
||
COPY index.mjs /app | ||
|
||
ENTRYPOINT ["node", "index.mjs"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import multer from "multer"; | ||
import express from "express"; | ||
import { Resource } from "sst"; | ||
import { Upload } from "@aws-sdk/lib-storage"; | ||
import { getSignedUrl } from "@aws-sdk/s3-request-presigner"; | ||
import { | ||
S3Client, | ||
GetObjectCommand, | ||
ListObjectsV2Command, | ||
} from "@aws-sdk/client-s3"; | ||
|
||
const PORT = 80; | ||
|
||
const app = express(); | ||
const s3 = new S3Client({}); | ||
const upload = multer({ storage: multer.memoryStorage() }); | ||
|
||
app.get("/", async (req, res) => { | ||
res.send("Hello World!"); | ||
}); | ||
|
||
app.post("/", upload.single("file"), async (req, res) => { | ||
const file = req.file; | ||
const params = { | ||
Bucket: Resource.MyBucket.name, | ||
Key: file.originalname, | ||
Body: file.buffer, | ||
}; | ||
|
||
const upload = new Upload({ | ||
params, | ||
client: s3, | ||
}); | ||
|
||
await upload.done(); | ||
|
||
res.status(200).send("File uploaded successfully."); | ||
}); | ||
|
||
app.get("/latest", async (req, res) => { | ||
const objects = await s3.send( | ||
new ListObjectsV2Command({ | ||
Bucket: Resource.MyBucket.name, | ||
}), | ||
); | ||
|
||
const latestFile = objects.Contents.sort( | ||
(a, b) => b.LastModified - a.LastModified, | ||
)[0]; | ||
|
||
const command = new GetObjectCommand({ | ||
Key: latestFile.Key, | ||
Bucket: Resource.MyBucket.name, | ||
}); | ||
const url = await getSignedUrl(s3, command); | ||
|
||
res.redirect(url); | ||
}); | ||
|
||
app.listen(PORT, () => { | ||
console.log(`Server is running on http://localhost:${PORT}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "aws-express", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@aws-sdk/client-s3": "^3.556.0", | ||
"@aws-sdk/lib-storage": "^3.556.0", | ||
"@aws-sdk/s3-request-presigner": "^3.556.0", | ||
"express": "^4.19.2", | ||
"multer": "^1.4.5-lts.1", | ||
"sst": "latest" | ||
}, | ||
"devDependencies": { | ||
"@types/aws-lambda": "8.10.137" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* This file is auto-generated by SST. Do not edit. */ | ||
/* tslint:disable */ | ||
/* eslint-disable */ | ||
import "sst" | ||
export {} | ||
declare module "sst" { | ||
export interface Resource { | ||
"MyBucket": { | ||
"name": string | ||
"type": "sst.aws.Bucket" | ||
} | ||
"MyService": { | ||
"service": string | ||
"type": "sst.aws.Service" | ||
"url": string | ||
} | ||
"MyVpc": { | ||
"type": "sst.aws.Vpc" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/// <reference path="./.sst/platform/config.d.ts" /> | ||
|
||
/** | ||
* ## AWS Express file upload | ||
* | ||
* Deploys an Express app to AWS. | ||
* | ||
* You can get started by running. | ||
* | ||
* ```bash | ||
* mkdir aws-express && cd aws-express | ||
* npm init -y | ||
* npm install express | ||
* npx sst@latest init | ||
* ``` | ||
* | ||
* Now you can add a service. | ||
* | ||
* ```ts title="sst.config.ts" | ||
* cluster.addService("MyService", { | ||
* public: { | ||
* ports: [{ listen: "80/http", forward: "3000/http" }], | ||
* }, | ||
* dev: { | ||
* command: "node --watch index.mjs", | ||
* }, | ||
* }); | ||
* ``` | ||
* | ||
* Start your app locally. | ||
* | ||
* ```bash | ||
* npx sst dev | ||
* ``` | ||
* | ||
* This example lets you upload a file to S3 and then download it. | ||
* | ||
* ```bash | ||
* curl --F [email protected] http://localhost:3000/ | ||
* curl http://localhost:3000/latest | ||
* ``` | ||
* | ||
* Finally, you can deploy it using `npx sst deploy --stage production`. | ||
*/ | ||
export default $config({ | ||
app(input) { | ||
return { | ||
name: "aws-express", | ||
removal: input?.stage === "production" ? "retain" : "remove", | ||
home: "aws", | ||
}; | ||
}, | ||
async run() { | ||
const bucket = new sst.aws.Bucket("MyBucket"); | ||
const vpc = new sst.aws.Vpc("MyVpc"); | ||
|
||
const cluster = new sst.aws.Cluster("MyCluster", { vpc }); | ||
cluster.addService("MyService", { | ||
public: { | ||
ports: [{ listen: "80/http" }], | ||
}, | ||
dev: { | ||
command: "node --watch index.mjs", | ||
}, | ||
link: [bucket], | ||
}); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
node_modules | ||
|
||
|
||
# sst | ||
.sst |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.