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

@appotter/nestjs-s3

v3.1.0

Published

NestJS provider to integrates with AWS S3

Downloads

262

Readme

Introduction

This is a simple wrapper of Aws S3 client library for NestJS.

Installation (AWS-SDK V3)

npm install --save @appotter/nestjs-s3 @aws-sdk/client-s3 @aws-sdk/lib-storage @aws-sdk/s3-request-presigner uuid multer

Installation (AWS-SDK V2) (Not recommended)

npm install --save @appotter/[email protected] aws-sdk uuid multer

Usage

Importing module

import { S3Module, S3Service } from '@appotter/nestjs-s3';

@Module({
  imports: [
    S3Module.register({
      accessKeyId: 'Random key',
      secretAccessKey: 'Random secret',
      region: 'Region',
      bucket: 'Bucket name',
      acl: 'ACL', // optional, default is public-read
      endpoint: '', // optional
    }),
  ],
  providers: [S3Service],
  exports: [S3Service],
})
export class S3ProviderModule {}

Importing module Async

import { S3Module, S3Service } from '@appotter/nestjs-S3';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
  imports: [
    S3Module.registerAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => ({
        accessKeyId: configService.get('S3_ACCESS_KEY_ID'),
        secretAccessKey: configService.get('S3_SECRET_ACCESS_KEY'),
        region: configService.get('S3_REGION'),
        bucket: configService.get('S3_BUCKET'),
      }),
    }),
  ],
  providers: [S3Service],
  exports: [S3Service],
})
export class S3ProviderModule {}

Calling Method

import { S3Service, S3ModuleUploadedFile } from '@appotter/nestjs-S3';

@Injectable()
export class YourService {
  constructor(private s3Service: S3Service) {}

  async put(file: S3ModuleUploadedFile): Promise<void> {
    await this.s3Service.put(file, 'path-or-custom-file-name');
  }

  async putAsUniqueName(file: S3ModuleUploadedFile): Promise<void> {
    const { url } = await this.s3Service.putAsUniqueName(file);

    console.log(url);
    // https://bucket-name.s3.region.amazonaws.com/71d07956-26bb-4554-bb37-d00a7865ae29.png
  }

  async listAllFiles(): Promise<void> {
    const items = await this.s3Service.lists();

    console.log(items);
    // [
    //   {
    //     key: 'file.png',
    //     size: 123,
    //     lastModified: 2022-08-08T07:28:40.000Z,
    //     bucket: 'test',
    //   },
    //   {
    //     key: 'file2.png',
    //     size: 456,
    //     lastModified: 2022-08-08T07:45:46.000Z,
    //     bucket: 'test',
    //   },
    // ]
  }

  async getFile(file: string): Promise<void> {
    const item = await this.s3Service.get(file);

    console.log(item);
    // {
    //   key: 'file.jpg',
    //   contentLength: 123,
    //   contentType: 'application/octet-stream',
    //   body: '<Buffer ff d8 ff e0 00 ... 74938 more bytes>',
    // }
  }

  async deleteFile(file: string): Promise<boolean> {
    const { status } = await this.s3Service.delete(file);

    return status;
  }

  // Also available with all S3 instance methods
  // this.s3Service.getClient().[all-method-of-S3-instance]();

  // Signed Url (support V3 only)
  async signedUrl(file: string): Promise<string> {
    // expires in 1 hour
    const signed = await this.s3Service.signedUrl(file, 60*60);

    console.log(signed);
    // https://test.s3.ap-southeast-1.amazonaws.com/fake.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=test%2F20240131%2Fap-southeast-1%2Fs3%2Faws4_request&X-Amz-Date=20240131T110201Z&X-Amz-Expires=60&X-Amz-Signature=25875526097b1f0182b27009005e70f9e92cd67294fb60583de9bd0b5f1cc5a7&X-Amz-SignedHeaders=host&x-id=GetObject
  }
}

Contributing

Contributions welcome!

Author

Phitsanu Chuamuangphan (GitHub)

LICENSE

MIT