npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

aws-sdk-v3-nest

v1.0.1

Published

Downloads

42,962

Readme

AWS SDK V3 Nest

AWS SDK Javascript V3 dynamic module for NestJS

TypeScript Npm package version npm NPM GitHub Repo stars

Let's build a S3 client and inject it into the nest app.

npm install aws-sdk-v3-nest @aws-sdk/client-s3
  1. Register the module with a S3 Client, in app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AwsSdkModule } from 'aws-sdk-v3-nest';
import { S3Client } from '@aws-sdk/client-s3';

@Module({
  imports: [
    // register S3 client
    AwsSdkModule.register({
      client: new S3Client({
        region: 'us-west-2',
      }),
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
  1. use the S3 client in app.controller.ts
import { ListBucketsCommand, S3Client } from '@aws-sdk/client-s3';
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { InjectAws } from 'aws-sdk-v3-nest';

@Controller()
export class AppController {
  constructor(
    private readonly appService: AppService,
    // inject the client
    @InjectAws(S3Client) private readonly s3: S3Client 
  ) {}
  @Get()
  async helloAws() {
    const listCommand = new ListBucketsCommand({});
    const res = await this.s3.send(listCommand);
    return res;
  }
}
  1. done!

Installation

  • Add aws-sdk-v3-nest to your project
     npm install aws-sdk-v3-nest
  • Make sure at least one AWS SDK for JavaScript V3 is in your project. Please skip this step if you already have it installed
    npm install @aws-sdk/client-s3

Add Environment Variables

Ensure the following environment variables are present in your project. These variables are critical for authentication and communication with AWS services.

Security Note: Treat these keys as sensitive information. Do not commit them to public repositories and ensure they are securely stored and accessed.

Setting Up: Typically, these variables are placed in a .env file in your project's root directory or configured directly in your deployment platform's environment settings.

AWS_ACCESS_KEY_ID=your_access_key_id
AWS_SECRET_ACCESS_KEY=your_secret_access_key

Register a Client

You can register any AWS SDK client you want. As long as it's a AWS SDK V3 client

A good example: S3Client

import { S3Client } from '@aws-sdk/client-s3';
import { AwsSdkModule } from 'aws-sdk-v3-nest';

// ... your code ...

AwsSdkModule.register({
  client: new S3Client({
    region: 'us-west-2',
  }),
});

Async Register

The library provides an async useFactory that allows you to add more logics before setting up the client instance.

import { S3Client, ListBucketsCommand } from '@aws-sdk/client-s3';
import { AwsSdkModule } from 'aws-sdk-v3-nest';

//... your code ...

AwsSdkModule.registerAsync({
  clientType: S3Client,
  useFactory: async () => {
    const s3 = new S3Client({
      region: 'us-west-2',
    });

    try {
      const listCommand = new ListBucketsCommand({});
      const res = await s3.send(listCommand);
      console.log('Connected to S3');
    } catch (e) {
      console.log('Unable to connect to S3', e);
    }

    return s3;
  },
});

Use @InjectAws(Client)

With a registered S3 client, you can now inject the instance to your service and controller.

Make sure the Client is the type you registered in module.

/** Use S3 client in AppController */
import { ListBucketsCommand, S3Client } from '@aws-sdk/client-s3';
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { InjectAws } from 'aws-sdk-v3-nest';

@Controller()
export class AppController {
  constructor(
    private readonly appService: AppService,
    @InjectAws(S3Client) private readonly s3: S3Client,
  ) {}
  @Get()
  async helloAws() {
    const listCommand = new ListBucketsCommand({});
    const res = await this.s3.send(listCommand);
    return res;
  }
}

Multiple Injection/Instances

To add more instances is easy, just register more! If you have same type of clients, please use the key attribute as the identifier.

Example for multiple S3 client instances

Register the S3 Client with a unique key

AwsSdkModule.register({
  // register the S3 Client with key `US-WEST-2-CLIENT`
  key: 'US-WEST-2-CLIENT',
  client: new S3Client({
    region: 'us-west-2',
  }),
}),
AwsSdkModule.register({
  // register the S3 Client with key `US-EAST-1-CLIENT`
  key: 'US-EAST-1-CLIENT',
  client: new S3Client({
    region: 'us-east-1',
  }),
}),

Inject and refer clients by @InjectAws(Client, key)

@InjectAws(S3Client, "US-WEST-2-CLIENT") private readonly s3west2: S3Client,
@InjectAws(S3Client, "US-EAST-1-CLIENT") private readonly s3east1: S3Client,

Global Module

By default, a client is only available at where it is registered. You have an option to make it global, isGlobal: true

AwsSdkModule.register({
  isGlobal: true,
  client: new S3Client({
    region: 'us-west-2',
  }),
});

Get client token

If you need a client key for testing purpose. Please pass the AWS SDK V3 client and key to getClientToken

getClientToken(S3Client, key = "")

Credit

Inspired by: nest-aws-sdk