Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/aws/s3/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ node_modules
dist
target
.spin/
dist.js
build/
1 change: 1 addition & 0 deletions examples/aws/s3/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KNITWIT_SOURCE=./config/knitwit.json
22 changes: 7 additions & 15 deletions examples/aws/s3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ This example showcases how to connect to and send messages using Amazon S3 with

## Prerequisites
- `spin >=2.6.0`
-

## Install Dependencies
Install the necessary npm packages:

```bash
npm install
```

## Setup the Example

Expand All @@ -30,18 +22,15 @@ npm install

```typescript
const client = new S3Client({
region: "<>",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intended?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good catch - this changed in the example because I rewrote the example to use the router to both be able to list objects and also stream objects for which I hardcoded the region. I will update the readme to use the hardcoded version for now if that is acceptable.

region: "us-west-2",
credentials: {
accessKeyId: "<>>",
accessKeyId: "<>",
secretAccessKey: "<>",
sessionToken: "<>"
},
});

const params = {
Bucket: "<>"
};
```
*note*: The example assumes that the bucket is in the `us-west-2` region. If the object is in some other region modify the configuration of the client and also edit the `allowed_outbound_hosts` in the `spin.toml`.

## Building and Running the Example

Expand All @@ -50,4 +39,7 @@ spin build
spin up
```

Use e.g. `curl -v http://127.0.0.1:3000/` to test the endpoint.
### Testing the different endpoints

- `curl -v http://127.0.0.1:3000/list/<bucket name>` to list the objects stored in the given bucket.
- `curl -v http://127.0.0.1:3000/stream/<bucket name>/<object key>` to stream the object in the given bucket.
File renamed without changes.
5,302 changes: 5,302 additions & 0 deletions examples/aws/s3/package-lock.json

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions examples/aws/s3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"build": "npx webpack --mode=production && npx mkdirp target && npx j2w -i dist.js -n spin-http -o target/s3.wasm",
"build": "knitwit --out-dir build/wit/knitwit --out-world combined && npx webpack --mode=production && npx mkdirp dist && npx j2w -i build/bundle.js -d build/wit/knitwit -n combined -o dist/s3.wasm",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
Expand All @@ -15,10 +15,12 @@
"ts-loader": "^9.4.1",
"typescript": "^4.8.4",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0"
"webpack-cli": "^4.10.0",
"@fermyon/knitwit": "0.3.0"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.600.0",
"@fermyon/spin-sdk": "^2.0.0"
"@fermyon/spin-sdk": "^3.0.0",
"itty-router": "^5.0.18",
"@aws-sdk/client-s3": "^3.600.0"
}
}
8 changes: 4 additions & 4 deletions examples/aws/s3/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ route = "/..."
component = "s3"

[component.s3]
source = "target/s3.wasm"
source = "dist/s3.wasm"
exclude_files = ["**/node_modules"]
allowed_outbound_hosts = ["https://*:*"]
allowed_outbound_hosts = ["https://*.s3.us-west-2.amazonaws.com"]
[component.s3.build]
command = "npm run build"
watch = ["src/**/*.ts", "package.json"]
command = ["npm install", "npm run build"]
watch = ["src/**/*.ts"]
59 changes: 38 additions & 21 deletions examples/aws/s3/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,43 @@
import { ResponseBuilder } from '@fermyon/spin-sdk';
import { S3Client, ListObjectsV2Command } from '@aws-sdk/client-s3';
// https://itty.dev/itty-router/routers/autorouter
import { AutoRouter } from 'itty-router';
import { S3Client, ListObjectsV2Command, GetObjectCommand } from '@aws-sdk/client-s3';

const client = new S3Client({
region: '<>',
credentials: {
accessKeyId: '<>>',
secretAccessKey: '<>',
sessionToken: '<>',
},
region: 'us-west-2',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

credentials: {
accessKeyId: '<>',
secretAccessKey: '<>',
sessionToken: '<>',
},
});
let router = AutoRouter();

const params = {
Bucket: '<>',
};
// Route ordering matters, the first route that matches will be used
// Any route that does not return will be treated as a middleware
// Any unmatched route will return a 404
router
.get("/list/:bucket", async ({ bucket }) => {
let command = new ListObjectsV2Command({ Bucket: bucket });
try {
let data = await client.send(command);
return new Response(JSON.stringify(data.Contents, null, 2));
} catch (e: any) {
return new Response(`error : ${e.message}`, { status: 500 });
}
})
.get("/stream/:bucket/:file", async ({ bucket, file }) => {
let command = new GetObjectCommand({ Bucket: bucket, Key: file });
try {
const data = await client.send(command);
return new Response(data.Body as ReadableStream, {
status: 200,
});
} catch (e: any) {
return new Response(`error : ${e.message}`, { status: 500 });
}
})

export async function handler(_req: Request, res: ResponseBuilder) {
const command = new ListObjectsV2Command(params);
try {
let data = await client.send(command);
res.send(JSON.stringify(data.Contents, null, 2));
} catch (e: any) {
res.status(500);
res.send(`error : ${e.message}`);
}
}
//@ts-ignore
addEventListener('fetch', async (event: FetchEvent) => {
event.respondWith(router.fetch(event.request));
});
19 changes: 0 additions & 19 deletions examples/aws/s3/src/spin.ts

This file was deleted.

2 changes: 1 addition & 1 deletion examples/aws/s3/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"jsx": "react",
"skipLibCheck": true,
"lib": [
"ES2015",
"ES2020",
"WebWorker"
],
"allowJs": true,
Expand Down
56 changes: 29 additions & 27 deletions examples/aws/s3/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
const path = require('path');
const SpinSdkPlugin = require('@fermyon/spin-sdk/plugins/webpack');
const SpinSdkPlugin = require("@fermyon/spin-sdk/plugins/webpack")

module.exports = {
entry: './src/spin.ts',
experiments: {
outputModule: true,
},
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
entry: './src/index.ts',
experiments: {
outputModule: true,
},
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
output: {
path: path.resolve(__dirname, './build'),
filename: 'bundle.js',
module: true,
library: {
type: "module",
}
},
plugins: [
new SpinSdkPlugin()
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
output: {
path: path.resolve(__dirname, './'),
filename: 'dist.js',
module: true,
library: {
type: 'module',
optimization: {
minimize: false
},
},
plugins: [new SpinSdkPlugin()],
optimization: {
minimize: false,
},
};
2 changes: 1 addition & 1 deletion examples/aws/sqs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ node_modules
dist
target
.spin/
dist.js
build/
1 change: 1 addition & 0 deletions examples/aws/sqs/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KNITWIT_SOURCE=./config/knitwit.json
8 changes: 0 additions & 8 deletions examples/aws/sqs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ This example showcases how to connect to and send messages using Amazon SQS with

## Prerequisites
- `spin >=2.6.0`
-

## Install Dependencies
Install the necessary npm packages:

```bash
npm install
```

## Setup the Example

Expand Down
File renamed without changes.
Loading