Skip to content

Latest commit

 

History

History
69 lines (44 loc) · 2.28 KB

File metadata and controls

69 lines (44 loc) · 2.28 KB

Node function to read files and upload to S3

This example shows how to upload a file to an S3 bucket.

Requirements

This example assumes that you are familiar with some products of Scaleway's ecosystem:

  • how serverless functions work. If needed, you can check Scaleway official documentation.
  • how Object Storage works. Please refer to scaleway's documentation here for more information.

This example uses the Scaleway Serverless Framework Plugin. Please set up your environment with the requirements stated in the Scaleway Serverless Framework Plugin before trying out the example.

Additionnaly it uses the serverless-functions-node library for local testing.

Context

This example shows how to upload a file to an S3 bucket using serverless function. It also shows how you can test your function locally before deploying.

This function does the following steps:

  • Read a file from HTTP request
  • Upload file to S3 bucket
  • Test locally

Setup

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):

S3_REGION = # Default: fr-par
SCW_ACCESS_KEY =
SCW_SECRET_KEY =
BUCKET_NAME =

Once your environment is set up, you can test your function locally with:

NODE_ENV=test node handler.js

This will launch a local server, allowing you to test the function. For that, you can run in another terminal (replace README.md with the file you want to upload):

curl -X POST http://localhost:8080 -H "content-type: multipart/form-data" -F "data=@README.md"

The output should be similar to:

Successfully uploaded README.md to <bucket name>

Deploy and run

Finally, if the test succeeded, you can deploy your function with:

serverless deploy

Then, from the given URL, you can run:

curl -X POST <function URL> -H "Content-Type: multipart/form-data"  -F "data=@README.md"

When invoking this function, the output should be similar to the one obtained when testing locally.