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
25 changed files
with
594 additions
and
150 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,7 @@ | |
* This example lets you upload a file to S3 and then download it. | ||
* | ||
* ```bash | ||
* curl --F [email protected] http://localhost:3000/ | ||
* curl -F [email protected] http://localhost:3000/ | ||
* curl http://localhost:3000/latest | ||
* ``` | ||
* | ||
|
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,5 @@ | ||
node_modules | ||
.git | ||
.gitignore | ||
README.md | ||
Dockerfile* |
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,178 @@ | ||
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore | ||
|
||
# Logs | ||
|
||
logs | ||
_.log | ||
npm-debug.log_ | ||
yarn-debug.log* | ||
yarn-error.log* | ||
lerna-debug.log* | ||
.pnpm-debug.log* | ||
|
||
# Caches | ||
|
||
.cache | ||
|
||
# Diagnostic reports (https://nodejs.org/api/report.html) | ||
|
||
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json | ||
|
||
# Runtime data | ||
|
||
pids | ||
_.pid | ||
_.seed | ||
*.pid.lock | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
|
||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
|
||
coverage | ||
*.lcov | ||
|
||
# nyc test coverage | ||
|
||
.nyc_output | ||
|
||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) | ||
|
||
.grunt | ||
|
||
# Bower dependency directory (https://bower.io/) | ||
|
||
bower_components | ||
|
||
# node-waf configuration | ||
|
||
.lock-wscript | ||
|
||
# Compiled binary addons (https://nodejs.org/api/addons.html) | ||
|
||
build/Release | ||
|
||
# Dependency directories | ||
|
||
node_modules/ | ||
jspm_packages/ | ||
|
||
# Snowpack dependency directory (https://snowpack.dev/) | ||
|
||
web_modules/ | ||
|
||
# TypeScript cache | ||
|
||
*.tsbuildinfo | ||
|
||
# Optional npm cache directory | ||
|
||
.npm | ||
|
||
# Optional eslint cache | ||
|
||
.eslintcache | ||
|
||
# Optional stylelint cache | ||
|
||
.stylelintcache | ||
|
||
# Microbundle cache | ||
|
||
.rpt2_cache/ | ||
.rts2_cache_cjs/ | ||
.rts2_cache_es/ | ||
.rts2_cache_umd/ | ||
|
||
# Optional REPL history | ||
|
||
.node_repl_history | ||
|
||
# Output of 'npm pack' | ||
|
||
*.tgz | ||
|
||
# Yarn Integrity file | ||
|
||
.yarn-integrity | ||
|
||
# dotenv environment variable files | ||
|
||
.env | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
.env.local | ||
|
||
# parcel-bundler cache (https://parceljs.org/) | ||
|
||
.parcel-cache | ||
|
||
# Next.js build output | ||
|
||
.next | ||
out | ||
|
||
# Nuxt.js build / generate output | ||
|
||
.nuxt | ||
dist | ||
|
||
# Gatsby files | ||
|
||
# Comment in the public line in if your project uses Gatsby and not Next.js | ||
|
||
# https://nextjs.org/blog/next-9-1#public-directory-support | ||
|
||
# public | ||
|
||
# vuepress build output | ||
|
||
.vuepress/dist | ||
|
||
# vuepress v2.x temp and cache directory | ||
|
||
.temp | ||
|
||
# Docusaurus cache and generated files | ||
|
||
.docusaurus | ||
|
||
# Serverless directories | ||
|
||
.serverless/ | ||
|
||
# FuseBox cache | ||
|
||
.fusebox/ | ||
|
||
# DynamoDB Local files | ||
|
||
.dynamodb/ | ||
|
||
# TernJS port file | ||
|
||
.tern-port | ||
|
||
# Stores VSCode versions used for testing VSCode extensions | ||
|
||
.vscode-test | ||
|
||
# yarn v2 | ||
|
||
.yarn/cache | ||
.yarn/unplugged | ||
.yarn/build-state.yml | ||
.yarn/install-state.gz | ||
.pnp.* | ||
|
||
# IntelliJ based IDEs | ||
.idea | ||
|
||
# Finder (MacOS) folder config | ||
.DS_Store | ||
|
||
# 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,40 @@ | ||
# https://bun.sh/guides/ecosystem/docker | ||
|
||
# use the official Bun image | ||
# see all versions at https://hub.docker.com/r/oven/bun/tags | ||
FROM oven/bun:1 AS base | ||
WORKDIR /usr/src/app | ||
|
||
# install dependencies into temp directory | ||
# this will cache them and speed up future builds | ||
FROM base AS install | ||
RUN mkdir -p /temp/dev | ||
COPY package.json bun.lockb /temp/dev/ | ||
RUN cd /temp/dev && bun install --frozen-lockfile | ||
|
||
# install with --production (exclude devDependencies) | ||
RUN mkdir -p /temp/prod | ||
COPY package.json bun.lockb /temp/prod/ | ||
RUN cd /temp/prod && bun install --frozen-lockfile --production | ||
|
||
# copy node_modules from temp directory | ||
# then copy all (non-ignored) project files into the image | ||
FROM base AS prerelease | ||
COPY --from=install /temp/dev/node_modules node_modules | ||
COPY . . | ||
|
||
# [optional] tests & build | ||
ENV NODE_ENV=production | ||
# RUN bun test | ||
RUN bun run build | ||
|
||
# copy production dependencies and source code into final image | ||
FROM base AS release | ||
COPY --from=install /temp/prod/node_modules node_modules | ||
COPY --from=prerelease /usr/src/app/index.ts . | ||
COPY --from=prerelease /usr/src/app/package.json . | ||
|
||
# run the app | ||
USER bun | ||
EXPOSE 3000/tcp | ||
ENTRYPOINT [ "bun", "run", "index.ts" ] |
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,15 @@ | ||
# aws-bun | ||
|
||
To install dependencies: | ||
|
||
```bash | ||
bun install | ||
``` | ||
|
||
To run: | ||
|
||
```bash | ||
bun run index.ts | ||
``` | ||
|
||
This project was created using `bun init` in bun v1.1.29. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime. |
Binary file not shown.
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,58 @@ | ||
import { Resource } from "sst"; | ||
import { | ||
S3Client, | ||
GetObjectCommand, | ||
ListObjectsV2Command, | ||
} from "@aws-sdk/client-s3"; | ||
import { Upload } from "@aws-sdk/lib-storage"; | ||
import { getSignedUrl } from "@aws-sdk/s3-request-presigner"; | ||
|
||
const s3 = new S3Client(); | ||
|
||
const server = Bun.serve({ | ||
async fetch(req) { | ||
const url = new URL(req.url); | ||
|
||
if (url.pathname === "/" && req.method === "GET") { | ||
return new Response("Hello World!"); | ||
} | ||
|
||
if (url.pathname === "/" && req.method === "POST") { | ||
const formData = await req.formData(); | ||
const file = formData.get("file")! as File; | ||
const params = { | ||
Bucket: Resource.MyBucket.name, | ||
Key: file.name, | ||
Body: file, | ||
}; | ||
const upload = new Upload({ | ||
params, | ||
client: s3, | ||
}); | ||
await upload.done(); | ||
|
||
return new Response("File uploaded successfully."); | ||
} | ||
|
||
if (url.pathname === "/latest" && req.method === "GET") { | ||
const objects = await s3.send( | ||
new ListObjectsV2Command({ | ||
Bucket: Resource.MyBucket.name, | ||
}), | ||
); | ||
const latestFile = objects.Contents!.sort( | ||
(a, b) => | ||
(b.LastModified?.getTime() ?? 0) - (a.LastModified?.getTime() ?? 0), | ||
)[0]; | ||
const command = new GetObjectCommand({ | ||
Key: latestFile.Key, | ||
Bucket: Resource.MyBucket.name, | ||
}); | ||
return Response.redirect(await getSignedUrl(s3, command)); | ||
} | ||
|
||
return new Response("404!"); | ||
}, | ||
}); | ||
|
||
console.log(`Listening on ${server.url}`); |
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,22 @@ | ||
{ | ||
"name": "aws-bun-file-upload", | ||
"module": "index.ts", | ||
"type": "module", | ||
"scripts": { | ||
"dev": "bun run --watch index.ts", | ||
"build": "bun build --target bun index.ts" | ||
}, | ||
"devDependencies": { | ||
"@types/aws-lambda": "8.10.145", | ||
"@types/bun": "latest" | ||
}, | ||
"peerDependencies": { | ||
"typescript": "^5.0.0" | ||
}, | ||
"dependencies": { | ||
"@aws-sdk/client-s3": "^3.658.1", | ||
"@aws-sdk/lib-storage": "^3.658.1", | ||
"@aws-sdk/s3-request-presigner": "^3.658.1", | ||
"sst": "latest" | ||
} | ||
} |
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" | ||
} | ||
} | ||
} |
Oops, something went wrong.