-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from sor4chi/feat/signed-url
feat: signed url support for S3 / R2
- Loading branch information
Showing
14 changed files
with
944 additions
and
132 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
"@hono-storage/s3": patch | ||
--- | ||
|
||
feat: support `signedUrl` for s3 storage. | ||
|
||
```ts | ||
import { S3Client } from "@aws-sdk/client-s3"; | ||
import { HonoS3Storage } from "@hono-storage/s3"; | ||
import { Hono } from "hono"; | ||
|
||
const client = new S3Client({ | ||
region: "us-east-1", | ||
credentials: { | ||
accessKeyId: "...", | ||
secretAccessKey: "...", | ||
}, | ||
}); | ||
|
||
const storage = new HonoS3Storage({ | ||
bucket: "hono-storage", | ||
client, | ||
}); | ||
|
||
const app = new Hono(); | ||
|
||
app.post( | ||
"/", | ||
storage.single("image", { | ||
sign: { | ||
expiresIn: 60, | ||
}, | ||
}), | ||
(c) => { | ||
return c.json({ signedURL: c.var.signedURLs.image }); | ||
}, | ||
); | ||
``` |
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 @@ | ||
--- | ||
"@hono-storage/core": patch | ||
--- | ||
|
||
feat: add `field` property to `HonoStorageFile`(HSF). |
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,2 @@ | ||
node_modules | ||
.env |
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,8 @@ | ||
``` | ||
npm install | ||
npm run start | ||
``` | ||
|
||
``` | ||
open http://localhost:3000 | ||
``` |
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,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" | ||
} | ||
} |
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,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); |
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.