You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(tests): node examples use offline testing (#35)
* tests: update cors-node example to use serverless local testing
* tests: update typescript-with-node example to use serverless local testing
* tests: update node-upload-file-s3-multipart example to use serverless local testing
* tests: update postgre-sql-node example to use serverless local testing
* tests: update image-labelling-node example to use serverless local testing
* tests: update image-transform-node example to use serverless local testing
* style: always use sh to stay consistent for code in README (node examples only)
Copy file name to clipboardExpand all lines: functions/cors-node/README.md
+47-5Lines changed: 47 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,24 +4,66 @@ This function shows how to handle preflight CORS requests that will be sent by a
4
4
5
5
The example uses totally permissive, open CORS, you may want to modify this to make it more secure.
6
6
7
-
## Setup
7
+
## Requirements
8
+
9
+
This example assumes you are familiar with how serverless functions work. If needed, you can check [Scaleway's official documentation](https://www.scaleway.com/en/docs/serverless/functions/quickstart/)
10
+
11
+
This example uses the Scaleway Serverless Framework Plugin. Please set up your environment with the requirements stated in the [Scaleway Serverless Framework Plugin](https://github.com/scaleway/serverless-scaleway-functions) before trying out the example.
8
12
9
-
This example uses the [Scaleway Serverless Framework Plugin](https://github.com/scaleway/serverless-scaleway-functions).
13
+
Additionnaly it uses the [serverless-functions-node](https://github.com/scaleway/serverless-functions-node) library for local testing.
10
14
11
-
Once this is set up, you can run:
15
+
## Context
12
16
13
-
```console
17
+
The function of the example allows most CORS requests. For more documentation on how to control access for CORS requests, see [HTTP response headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#the_http_response_headers)
18
+
19
+
## Setup
20
+
21
+
Once your environment is set up, you can run:
22
+
23
+
```sh
14
24
npm install
15
25
26
+
NODE_ENV=test node handler.js
27
+
```
28
+
29
+
This will launch a local server, allowing you to test the function. For that, you can run in another terminal:
30
+
31
+
The result should be similar to:
32
+
33
+
```sh
34
+
HTTP/1.1 200 OK
35
+
access-control-allow-origin: *
36
+
access-control-allow-headers: *
37
+
access-control-allow-methods: *
38
+
access-control-expose-headers: *
39
+
content-type: text/plain
40
+
content-length: 35
41
+
Date: Mon, 17 Apr 2023 07:03:06 GMT
42
+
Connection: keep-alive
43
+
Keep-Alive: timeout=72
44
+
server: envoy
45
+
46
+
This is allowing most CORS requests
47
+
```
48
+
49
+
You can also check the result of your function in a browser. It should be "This is allowing most CORS requests".
50
+
51
+
## Deploy and run
52
+
53
+
Finally, if the test succeeded, you can deploy your function with:
54
+
55
+
```sh
16
56
serverless deploy
17
57
```
18
58
19
59
Then, from the given URL, you can run:
20
60
21
-
```console
61
+
```sh
22
62
# Options request
23
63
curl -i -X OPTIONS <function URL>
24
64
25
65
# Get request
26
66
curl -i -X GET <function URL>
27
67
```
68
+
69
+
When invoking this function, the output should be similar to the one obtained when testing locally.
This example shows how to label an RGB image in an S3 bucket using serverless functions.
4
+
3
5
## Requirements
4
6
5
7
This example assumes that you are familiar with:
6
-
* how serverless functions work. If needed, you can check [Scaleway official documentation](https://www.scaleway.com/en/docs/serverless/functions/quickstart/).
7
-
* how S3 object storage works, and especially how to create a bucket and upload files within a bucket. Please refer to scaleway's documentation [here](https://www.scaleway.com/en/docs/storage/object/quickstart/).
8
-
* how to create and get user API access and secret keys using IAM. Please refer to IAM documentation [here](https://www.scaleway.com/en/docs/identity-and-access-management/iam/concepts/).
8
+
9
+
* how serverless functions work. If needed, you can check [Scaleway official documentation](https://www.scaleway.com/en/docs/serverless/functions/quickstart/).
10
+
* how S3 object storage works, and especially how to create a bucket and upload files within a bucket. Please refer to scaleway's documentation [here](https://www.scaleway.com/en/docs/storage/object/quickstart/).
11
+
* how to create and get user API access and secret keys using IAM. Please refer to IAM documentation [here](https://www.scaleway.com/en/docs/identity-and-access-management/iam/concepts/).
9
12
10
13
This example uses the Scaleway Serverless Framework Plugin. Please set up your environment with the requirements stated in the [Scaleway Serverless Framework Plugin](https://github.com/scaleway/serverless-scaleway-functions) before trying out the example.
11
14
15
+
Additionnaly it uses the [serverless-functions-node](https://github.com/scaleway/serverless-functions-node) library for local testing.
12
16
13
17
## Context
14
18
15
19
This example shows how to label an RGB image in an S3 bucket using serverless functions. The example uses a pre-trained ready-to-use model from [TensorFlow.js](https://www.tensorflow.org/js/models). The model is called `mobilenet` and can be used on server or client side. It returns three labels of an image with their respective prediction probabilities (namely, logits). Check `mobilenet` Github repository [here](https://github.com/tensorflow/tfjs-models/tree/master/mobilenet).
16
20
17
-
18
21
## Description
19
22
20
23
The function gets an RGB image (jpg, jpeg, or png formats) from an S3 bucket, transforms it into a TensorFlow-compatible object (namely, a tensor), and then applys a pretrained `mobilenet` model to return the labels and their respective logits in a json format.
@@ -29,40 +32,40 @@ Create an S3 bucket and upload an RGB image (jpg, jpeg or png format) within thi
29
32
30
33
### Fill environment variables
31
34
32
-
Fill your environment variables within `serverless.yml` file:
35
+
Ensure to create a bucket and have the following secrets variables available in your environment (to be able to test locally) and within `serverless.yml` file (to be able to deploy):
33
36
34
-
```
37
+
```yml
35
38
secret:
36
39
USER_ACCESS_KEY: <bucket scw access key>
37
40
USER_SECRET_KEY: <bucket scw access key id>
38
-
S3_ENDPOINT_URL: http://s3.fr-par.scw.cloud
41
+
S3_ENDPOINT_URL: s3.fr-par.scw.cloud
39
42
```
40
43
41
44
### Install npm modules
42
45
43
46
Once your environment is set up, you can install `npm` dependencies from `package.json` file using:
44
47
45
-
```
48
+
```sh
46
49
npm install
47
50
```
48
51
49
-
### Deploy and call the function
52
+
### Test locally
50
53
51
-
Then deploy your function and get its URL using:
54
+
Once your environment is set up, you can test your function locally with:
52
55
53
-
```
54
-
serverless deploy
56
+
```sh
57
+
NODE_ENV=test node handler.js
55
58
```
56
59
57
-
From the given function URL, you can get image labels in a `json` format:
60
+
This will launch a local server, allowing you to test the function. For that, you can run in another terminal:
58
61
59
-
```
60
-
curl -X GET "<function URL>/?sourceBucket=<source bucket name>&sourceKey=<file name within bucket>" | jq
62
+
```sh
63
+
curl -X GET "http://localhost:8080?sourceBucket=<source bucket name>&sourceKey=<filename within bucket>"
61
64
```
62
65
63
-
The result should be similar to:
66
+
The output should be similar to:
64
67
65
-
```
68
+
```json
66
69
{
67
70
"labels": [
68
71
{
@@ -79,7 +82,22 @@ The result should be similar to:
79
82
}
80
83
]
81
84
}
82
-
83
85
```
84
86
85
87
You can also check the result of your function in a browser. It should return the same result.
88
+
89
+
### Deploy and call the function
90
+
91
+
Finally, if the test succeeded, you can deploy your function with:
92
+
93
+
```sh
94
+
serverless deploy
95
+
```
96
+
97
+
Then, from the given URL, you can run:
98
+
99
+
```sh
100
+
curl -X GET "<function url>?sourceBucket=<source bucket name>&sourceKey=<filename within bucket>"
101
+
```
102
+
103
+
When invoking this function, the output should be similar to the one obtained when testing locally.
0 commit comments