DEV Community

Anthony Frehner
Anthony Frehner

Posted on

Javascript AWS SDK v3 S3 guide

That is way too many acronyms in the title, yet they almost seem necessary. I promise I'm not trying to out-SEO you.

With the new v3 update to the Javascript AWS-SDK, you can use the sdk quite a bit differently. You don't have to, but the new way can reduce the amount of code you import at runtime, and thus can be "more performant." At the moment of writing this article, there aren't a lot of docs on it either. So this article is just as much for me as is it for you!

The major difference is that you now import/require a very bare "client", and send your commands through that client.

// The "old" way
const { S3 } = require('awk-sdk')
const s3 = new S3(s3Config)
const object = await s3.getObject(objConfig).promise()
Enter fullscreen mode Exit fullscreen mode
// The "new" way
const { S3Client, GetObjectCommand } = require('@aws-sdk/client-s3')
const s3Client = new S3Client(s3Config)
const object = await s3Client.send(new GetObjectCommand(objConfig))
Enter fullscreen mode Exit fullscreen mode

So, some things to point out:

  • Note that you can install only the client you need, not the whole AWS SDK library. In this case, you only have to npm install @aws-sdk/client-s3. This reduces the space on disk and the install time 👍
  • You only need to import/require the exact functions you need from the client, e.g. GetObjectCommand. This reduces the amount of code you need at runtime 👍
  • You send the command you want through the client, and a promise is returned by default instead of needing to call .promise() 👍

Here's the v3 documentation homepage if you want to explore it as well. I wouldn't say it's bad, per se, but I wouldn't say it's good either. It does seem extensive though.

Discussion (0)