@@ -38,10 +38,12 @@ Before you get started, make sure to [configure your AWS credentials](/docs/iam-
38
38
Let's start by creating our app.
39
39
40
40
``` bash
41
- mkdir aws-hono && cd aws-hono
42
- npm init -y
41
+ npm create hono@latest aws-hono
42
+ cd aws-hono
43
43
```
44
44
45
+ We are picking the ** aws-lambda** template.
46
+
45
47
##### Init SST
46
48
47
49
Now let's initialize SST in our app.
@@ -61,7 +63,7 @@ Let's add a Hono API using an AWS Lambda. Update your `sst.config.ts`.
61
63
62
64
``` js title="sst.config.ts"
63
65
async run () {
64
- const hono = new sst.aws.Function (" Hono" , {
66
+ new sst.aws.Function (" Hono" , {
65
67
url: true ,
66
68
handler: " index.handler" ,
67
69
});
@@ -102,7 +104,7 @@ const bucket = new sst.aws.Bucket("MyBucket");
102
104
Now, link the bucket to the API.
103
105
104
106
``` ts title="sst.config.ts" {3}
105
- const hono = new sst .aws .Function (" Hono" , {
107
+ new sst .aws .Function (" Hono" , {
106
108
url: true ,
107
109
link: [bucket ],
108
110
handler: " index.handler" ,
@@ -113,20 +115,17 @@ const hono = new sst.aws.Function("Hono", {
113
115
114
116
### 4. Upload a file
115
117
116
- We want the ` / ` route of our API to generate a pre-signed URL to upload a file to our S3 Bucket. Create an ` index.ts ` file and add the following.
117
-
118
- ``` ts title="index.ts" {5}
119
- const app = new Hono ()
120
- .get (" /" , async (c ) => {
121
- const command = new PutObjectCommand ({
122
- Key: crypto .randomUUID (),
123
- Bucket: Resource .MyBucket .name ,
124
- });
118
+ We want the ` / ` route of our API to generate a pre-signed URL to upload a file to our S3 Bucket. Replace the _ Hello Hono_ route in ` src/index.ts ` .
125
119
126
- return c .text (await getSignedUrl (s3 , command ));
120
+ ``` ts title="src/index.ts" {4}
121
+ app .get (' /' , async (c ) => {
122
+ const command = new PutObjectCommand ({
123
+ Key: crypto .randomUUID (),
124
+ Bucket: Resource .MyBucket .name ,
127
125
});
128
126
129
- export const handler = handle (app );
127
+ return c .text (await getSignedUrl (s3 , command ));
128
+ });
130
129
```
131
130
132
131
:::tip
@@ -136,55 +135,50 @@ We are directly accessing our S3 bucket with `Resource.MyBucket.name`.
136
135
Install the npm packages.
137
136
138
137
``` bash
139
- npm install hono @aws-sdk/client-s3 @aws-sdk/s3-request-presigner
138
+ npm install @aws-sdk/client-s3 @aws-sdk/s3-request-presigner
140
139
```
141
140
142
- Then add the relevant imports.
141
+ Then add the relevant imports. We'll use the extra ones below.
143
142
144
- ``` ts title="index.ts"
145
- import { Resource } from " sst" ;
146
- import { getSignedUrl } from " @aws-sdk/s3-request-presigner" ;
143
+ ``` ts title="src/ index.ts"
144
+ import { Resource } from ' sst'
145
+ import { getSignedUrl } from ' @aws-sdk/s3-request-presigner'
147
146
import {
148
147
S3Client ,
149
148
GetObjectCommand ,
150
149
PutObjectCommand ,
151
150
ListObjectsV2Command ,
152
- } from " @aws-sdk/client-s3" ;
153
- import { Hono } from " hono" ;
154
- import { handle } from " hono/aws-lambda" ;
151
+ } from ' @aws-sdk/client-s3'
155
152
156
- const s3 = new S3Client ({} );
153
+ const s3 = new S3Client ();
157
154
```
158
155
159
-
160
156
---
161
157
162
158
### 5. Download a file
163
159
164
- We want the ` /latest ` route of our API to generate a pre-signed URL to download the last uploaded file in our S3 Bucket. Add this to your routes in ` index.ts ` .
160
+ We want the ` /latest ` route of our API to generate a pre-signed URL to download the last uploaded file in our S3 Bucket. Add this to your routes in ` src/ index.ts` .
165
161
166
- ``` ts title="index.ts"
167
- const app = new Hono ()
168
- // ...
169
- .get (" /latest" , async (c ) => {
170
- const objects = await s3 .send (
171
- new ListObjectsV2Command ({
172
- Bucket: Resource .MyBucket .name ,
173
- }),
174
- );
175
-
176
- const latestFile = objects .Contents ! .sort (
177
- (a , b ) =>
178
- (b .LastModified ?.getTime () ?? 0 ) - (a .LastModified ?.getTime () ?? 0 ),
179
- )[0 ];
180
-
181
- const command = new GetObjectCommand ({
182
- Key: latestFile .Key ,
162
+ ``` ts title="src/index.ts"
163
+ app .get (' /latest' , async (c ) => {
164
+ const objects = await s3 .send (
165
+ new ListObjectsV2Command ({
183
166
Bucket: Resource .MyBucket .name ,
184
- });
167
+ }),
168
+ );
169
+
170
+ const latestFile = objects .Contents ! .sort (
171
+ (a , b ) =>
172
+ (b .LastModified ?.getTime () ?? 0 ) - (a .LastModified ?.getTime () ?? 0 ),
173
+ )[0 ];
185
174
186
- return c .redirect (await getSignedUrl (s3 , command ));
175
+ const command = new GetObjectCommand ({
176
+ Key: latestFile .Key ,
177
+ Bucket: Resource .MyBucket .name ,
187
178
});
179
+
180
+ return c .redirect (await getSignedUrl (s3 , command ));
181
+ });
188
182
```
189
183
190
184
---
@@ -345,14 +339,14 @@ app.post('/', async (c) => {
345
339
Add the imports. We'll use the extra ones below.
346
340
347
341
``` tsx title="src/index.ts"
348
- import { Resource } from " sst" ;
342
+ import { Resource } from ' sst'
349
343
import {
350
344
S3Client ,
351
345
GetObjectCommand ,
352
346
ListObjectsV2Command ,
353
- } from " @aws-sdk/client-s3" ;
354
- import { Upload } from " @aws-sdk/lib-storage" ;
355
- import { getSignedUrl } from " @aws-sdk/s3-request-presigner" ;
347
+ } from ' @aws-sdk/client-s3'
348
+ import { Upload } from ' @aws-sdk/lib-storage'
349
+ import { getSignedUrl } from ' @aws-sdk/s3-request-presigner'
356
350
357
351
const s3 = new S3Client ();
358
352
```
0 commit comments