Skip to content

Commit

Permalink
feat(example): add signedURL example
Browse files Browse the repository at this point in the history
  • Loading branch information
sor4chi committed Dec 9, 2023
1 parent ad5332b commit 113df71
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/s3-sign/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
8 changes: 8 additions & 0 deletions examples/s3-sign/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
```
npm install
npm run start
```

```
open http://localhost:3000
```
18 changes: 18 additions & 0 deletions examples/s3-sign/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@hono-storage/s3-sign-example",
"private": true,
"version": "0.0.0",
"scripts": {
"start": "tsx src/index.ts"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.428.0",
"@hono-storage/s3": "workspace:*",
"@hono/node-server": "^1.1.0",
"dotenv": "^16.3.1",
"hono": "^3.8.1"
},
"devDependencies": {
"tsx": "^3.12.2"
}
}
60 changes: 60 additions & 0 deletions examples/s3-sign/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { S3Client } from "@aws-sdk/client-s3";
import { serve } from "@hono/node-server";
import { HonoS3Storage } from "@hono-storage/s3";
import { config } from "dotenv";
import { Hono } from "hono";
import { html } from "hono/html";

config();

const app = new Hono();

if (!process.env.AWS_ACCESS_KEY_ID || !process.env.AWS_SECRET_ACCESS_KEY) {
throw new Error("AWS credentials not found");
}

const client = new S3Client({
region: "us-east-1",
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
},
});

const storage = new HonoS3Storage({
key: (_, file) =>
`${file.originalname}-${new Date().getTime()}.${file.extension}`,
bucket: "hono-storage",
client,
});

let signedURL = "";

app.post(
"/",
storage.single("image", {
sign: {
expiresIn: 60,
},
}),
(c) => {
signedURL = c.var.signedURLs.image || "";
return c.html(html`
<a href="/">Back</a>
<p>Image uploaded successfully</p>
<code>${signedURL}</code>
`);
},
);

app.get("/", (c) =>
c.html(html`
<form action="/" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<button type="submit">Submit</button>
</form>
<img src="${signedURL}" />
`),
);

serve(app);

0 comments on commit 113df71

Please sign in to comment.