How to use AWS SDK for JavaScript v3 with Scaleway

The other day I wanted to update my old scripts. Scripts that were AWS SDK for JavaScript v2 based. The problem is that v2 is going to sunset and you have to migrate your code from v2 to v3. Naively I started to dug into the AWS SDK for JavaScript v3 and started changing my helpers, and it was no longer working.. I was getting unknown errors, I did not know how to shape my s3 client. After some struggle I managed to make it work, so I wrote a little gist that will showcase that for you and I believe it a good starting point to play with the AWS SDK for JavaScript v3.

AWS SDK for JavaScript v3
API Reference

Basic operations for a good start

What you'll need:

  • A scaleway account
  • Bucket credentials

If you want to deal with the programatic part:

  • Node.js LTS
  • TypeScript
  1. If you do not have an account yet, register here:
    https://console.scaleway.com/register/
  2. Once verified, signed in create a bucket in a region of your choice:
    https://console.scaleway.com/object-storage/buckets
Bucket Page

3. Go to Identity and Access Management (IAM) at top right corner on your profil> API Keys and generate your credentials, do not forget to set key specific for Object Storage management.

Generate an API Key dialog

Keep note of your  accessKeyId and secretAccessKey safely

Last step, just go back to your listed bucket and go to your bucket settings, where you'll need your region, bucketName and endpoint.

From here we are good to go 🚀

⚠️ Everything is detailled within the gist, but for example from the endpoint you'll get from the scaleway website you'll have to remove the bucketName part in it when you put that endpoint value in your client's config. Actually the sdk append that value automatically for you and in order to match the endpoint you should not provide it. So most of the time your endpoint will look like this:

https://s3.<YOUR_REGION>.scw.cloud instead of https://<YOUR_BUCKET_NAME>.s3.<YOUR_REGION>.scw.cloud

Here the gist that showcase this:

You'll see how you can:

  • Check if a file exist into a bucket HeadCommand
  • Upload a file to a bucket
  • Generate a signed url from an existing file for download

Then you can run that script that way

npx ts-node index.ts

or 

npx ts-node head.ts | pino-pretty (if pino-pretty is installed globally)

package.json

{
  "name": "head",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "@aws-sdk/client-s3": "^3.400.0",
    "@aws-sdk/lib-storage": "^3.400.0",
    "@aws-sdk/s3-request-presigner": "^3.400.0",
    "pino": "^8.15.0"
  }
}

required:

@aws-sdk/client-s3
AWS SDK for JavaScript S3 Client for Node.js, Browser and React Native. Latest version: 3.400.0, last published: 4 days ago. Start using @aws-sdk/client-s3 in your project by running `npm i @aws-sdk/client-s3`. There are 1959 other projects in the npm registry using @aws-sdk/client-s3.
@aws-sdk/lib-storage
Storage higher order operation. Latest version: 3.400.0, last published: 4 days ago. Start using @aws-sdk/lib-storage in your project by running `npm i @aws-sdk/lib-storage`. There are 327 other projects in the npm registry using @aws-sdk/lib-storage.
@aws-sdk/s3-request-presigner
[![NPM version](https://img.shields.io/npm/v/@aws-sdk/s3-request-presigner/latest.svg)](https://www.npmjs.com/package/@aws-sdk/s3-request-presigner) [![NPM downloads](https://img.shields.io/npm/dm/@aws-sdk/s3-request-presigner.svg)](https://www.npmjs.com/. Latest version: 3.400.0, last published: 4…
pino
super fast, all natural json logger. Latest version: 8.15.0, last published: 25 days ago. Start using pino in your project by running `npm i pino`. There are 4255 other projects in the npm registry using pino.

optional:

pino-pretty
Prettifier for Pino log lines. Latest version: 10.2.0, last published: a month ago. Start using pino-pretty in your project by running `npm i pino-pretty`. There are 1670 other projects in the npm registry using pino-pretty.

Basically if you run that script you'll get something like that:

First attempts, file not found, uploading it
Second time file exist returning signed url for download

That's sit for today I hope you enjoyed it !