Javascript AWS SDK v3 S3 guide
Anthony Frehner
Posted on August 20, 2021
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()
// The "new" way
const { S3Client, GetObjectCommand } = require('@aws-sdk/client-s3')
const s3Client = new S3Client(s3Config)
const object = await s3Client.send(new GetObjectCommand(objConfig))
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.
Posted on August 20, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.