File

shared/common/aws-s3.service.ts

Description

AWS S3 related service

Index

Properties
  • Private Readonly logger
  • Private Readonly s3
Methods

Constructor

constructor()

Methods

Async deleteFile
deleteFile(bucket: string, path: string)

Delete file from to the AWS bucket

Parameters :
Name Type Optional
bucket string No
path string No
Returns : Promise<S3.Types.DeleteObjectOutput>
getAwsUrl
getAwsUrl(bucket: string, path: string)

Get AWS S3 url to the file

Parameters :
Name Type Optional
bucket string No
path string No
Returns : string
getDomainUrl
getDomainUrl(bucket: string, path: string)

Get 99math domain url to the file

Parameters :
Name Type Optional
bucket string No
path string No
Returns : string
Async getFile
getFile(bucket: string, path: string)

Get file from to the AWS bucket

Parameters :
Name Type Optional
bucket string No
path string No
Returns : Promise<S3.Types.GetObjectOutput>
Async putFile
putFile(bucket: string, fileBuffer: Buffer, path: string, contentType: string, metadata?: S3.Metadata, acl?: ObjectCannedACL)

Add file to the AWS bucket

Parameters :
Name Type Optional
bucket string No
fileBuffer Buffer No
path string No
contentType string No
metadata S3.Metadata Yes
acl ObjectCannedACL Yes
Returns : Promise<S3.Types.PutObjectOutput>

Properties

Private Readonly logger
Type : Logger
Private Readonly s3
Type : S3
import { Injectable, Logger } from '@nestjs/common';
import * as AWS from 'aws-sdk';
import * as S3 from 'aws-sdk/clients/s3';
import { ObjectCannedACL } from 'aws-sdk/clients/s3';

/**
 *  AWS S3 related service
 */

@Injectable()
export class AwsS3Service {
  private readonly logger: Logger;
  private readonly s3: S3;

  constructor() {
    this.logger = new Logger(AwsS3Service.name);
    this.s3 = new AWS.S3({
      region: process.env.AWS_REGION,
      accessKeyId: process.env.AWS_ACCESS_KEY_ID,
      secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
    });
  }

  /**
   * Get file from to the AWS bucket
   *
   * @returns {S3.Types.GetObjectOutput}
   * @throws {AWSError}
   */
  async getFile(
    bucket: string,
    path: string
  ): Promise<S3.Types.GetObjectOutput> {
    return this.s3
      .getObject({
        Bucket: bucket,
        Key: path,
      })
      .promise();
  }

  /**
   * Add file to the AWS bucket
   *
   * @returns {S3.Types.PutObjectOutput}
   * @throws {AWSError}
   */
  async putFile(
    bucket: string,
    fileBuffer: Buffer,
    path: string,
    contentType: string,
    metadata?: S3.Metadata,
    acl?: ObjectCannedACL,
  ): Promise<S3.Types.PutObjectOutput> {
    return this.s3
      .putObject({
        Bucket: bucket,
        Body: fileBuffer,
        Key: path,
        ContentType: contentType,
        Metadata: metadata,
        ACL: acl,
      })
      .promise();
  }

  /**
   * Delete file from to the AWS bucket
   *
   * @returns {S3.Types.DeleteObjectOutput}
   * @throws {AWSError}
   */
  async deleteFile(
    bucket: string,
    path: string
  ): Promise<S3.Types.DeleteObjectOutput> {
    return this.s3
      .deleteObject({
        Bucket: bucket,
        Key: path,
      })
      .promise();
  }

  /**
   * Get AWS S3 url to the file
   *
   * @returns {string}
   */
  getAwsUrl(bucket: string, path: string): string {
    return `https://${bucket}.s3.${this.s3.config.region}.amazonaws.com/${path}`;
  }

  /**
   * Get 99math domain url to the file
   *
   * @returns {string}
   */
  getDomainUrl(bucket: string, path: string): string {
    return `https://${bucket}/${path}`;
  }
}

results matching ""

    No results matching ""